From 59e7108e00d5ad871f9a220335997fd476ec9c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E8=89=BA=E7=BC=A4?= <> Date: Fri, 3 Jul 2020 23:20:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsoleApp2.sln" | 25 +++++++ .../ConsoleApp2/App.config" | 6 ++ .../ConsoleApp2/ConsoleApp2.csproj" | 53 ++++++++++++++ .../ConsoleApp2/Program.cs" | 71 +++++++++++++++++++ .../ConsoleApp2/Properties/AssemblyInfo.cs" | 36 ++++++++++ 5 files changed, 191 insertions(+) create mode 100644 "\346\242\201\350\211\272\347\274\244/ConsoleApp2.sln" create mode 100644 "\346\242\201\350\211\272\347\274\244/ConsoleApp2/App.config" create mode 100644 "\346\242\201\350\211\272\347\274\244/ConsoleApp2/ConsoleApp2.csproj" create mode 100644 "\346\242\201\350\211\272\347\274\244/ConsoleApp2/Program.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/ConsoleApp2/Properties/AssemblyInfo.cs" diff --git "a/\346\242\201\350\211\272\347\274\244/ConsoleApp2.sln" "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2.sln" new file mode 100644 index 0000000..132d66d --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1169 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp2", "ConsoleApp2\ConsoleApp2.csproj", "{7604E15D-0438-4C25-A21E-C04A052DC0B0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7604E15D-0438-4C25-A21E-C04A052DC0B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7604E15D-0438-4C25-A21E-C04A052DC0B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7604E15D-0438-4C25-A21E-C04A052DC0B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7604E15D-0438-4C25-A21E-C04A052DC0B0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3F643F57-19EA-467B-8F06-E6331B0D30B5} + EndGlobalSection +EndGlobal diff --git "a/\346\242\201\350\211\272\347\274\244/ConsoleApp2/App.config" "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/App.config" new file mode 100644 index 0000000..731f6de --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/App.config" @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/ConsoleApp2/ConsoleApp2.csproj" "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/ConsoleApp2.csproj" new file mode 100644 index 0000000..6957dd6 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/ConsoleApp2.csproj" @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {7604E15D-0438-4C25-A21E-C04A052DC0B0} + Exe + ConsoleApp2 + ConsoleApp2 + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/ConsoleApp2/Program.cs" "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/Program.cs" new file mode 100644 index 0000000..2c9de8d --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/Program.cs" @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp2 +{ + class Program + { + static void Main(string[] args) + { + //数据库连接 + var conString = "server=.;database=MySchool;uid=sa;pwd=123456"; + + SqlConnection sqlConnection = new SqlConnection(conString); + //启用数据库连接 + sqlConnection.Open(); + //数据库查询命令 + var cmdString = "select * from StuInfo"; + + SqlCommand sqlCommand = new SqlCommand(cmdString, sqlConnection); + + var reader = sqlCommand.ExecuteReader(); + //打印数据库 + while (reader.Read()) + { + Console.WriteLine("{0} {1} {2}", reader[0], reader[1], reader[2]); + } + //关闭数据库连接 + sqlConnection.Close(); + + //建立表格 + DataTable table = new DataTable(); + //导入数据库数据到表格 + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdString, sqlConnection); + + sqlDataAdapter.Fill(table); + //遍历打印 + foreach (DataRow row in table.Rows) + { + Console.WriteLine(row["StuId"]); + Console.WriteLine(row["StuName"]); + Console.WriteLine(row["StuSex"]); + } + + //添加数据 + var cmdString2 = "insert StuInfo(StuId,StuName,StuSex)values(3,'小张','男')"; + sqlConnection.Open(); + SqlCommand sqlCommand1 = new SqlCommand(cmdString2, sqlConnection); + var add = sqlCommand1.ExecuteNonQuery(); + sqlConnection.Close(); + + //更新数据 + var cmdString3 = "update StuInfo set StuName='小林' where StuId =1;"; + sqlConnection.Open(); + SqlCommand sqlCommand2 = new SqlCommand(cmdString3, sqlConnection); + var update = sqlCommand2.ExecuteNonQuery(); + sqlConnection.Close(); + + //删除数据 + var cmdString4 = "delete StuInfo where StuId =1"; + sqlConnection.Open(); + SqlCommand sqlCommand3 = new SqlCommand(cmdString4, sqlConnection); + var delete = sqlCommand3.ExecuteNonQuery(); + sqlConnection.Close(); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/ConsoleApp2/Properties/AssemblyInfo.cs" "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/Properties/AssemblyInfo.cs" new file mode 100644 index 0000000..08eddab --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/ConsoleApp2/Properties/AssemblyInfo.cs" @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ConsoleApp2")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ConsoleApp2")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("7604e15d-0438-4c25-a21e-c04a052dc0b0")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] -- Gitee