当前位置:Gxlcms > 数据库问题 > C# ado.net基础 更新一行数据 在sqlsever中的一个表中

C# ado.net基础 更新一行数据 在sqlsever中的一个表中

时间:2021-07-01 10:21:17 帮助过:9人阅读

using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace shujuku 8 { 9 class SqlInformation 10 { 11 /// <summary> 12 /// 服务器的名称 13 /// </summary> 14 public string DataSource; 15 /// <summary> 16 /// 数据库的名称 17 /// </summary> 18 public string InitialCatalog; 19 /// <summary> 20 /// true为使用windows验证 21 /// </summary> 22 public bool IntegratedSecurity; 23 /// <summary> 24 /// SqlServer身份验证所需要的用户名 25 /// </summary> 26 public string UserID; 27 /// <summary> 28 /// SqlServer身份验证所需要的密码 29 /// </summary> 30 public string Password; 31 /// <summary> 32 /// 返回连接字符串 33 /// </summary> 34 public SqlInformation() 35 { 36 IntegratedSecurity = false; 37 } 38 public string LoginInformation() 39 { 40 if (IntegratedSecurity) 41 { 42 return string.Format(@"Data Source = {0}; Initial Catalog = {1}; Integrated Security = true", DataSource, InitialCatalog); 43 } 44 else 45 { 46 return string.Format(@"Data Source = {0};Initial Catalog = {1};User ID = {2};Password = {3}", DataSource, InitialCatalog, UserID, Password); 47 } 48 } 49 } 50 }

 

  second .cs file

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data.SqlClient;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace shujuku
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             SqlInformation test = new SqlInformation();
15             test.DataSource = "USER-20170116MG";
16             test.InitialCatalog = "helloworld";
17             test.IntegratedSecurity = true;
18             string conStr=test.LoginInformation();
19 
20             using (SqlConnection connection = new SqlConnection(conStr))
21             {
22                 //编写sql语句(可以在sql server中创建脚本尝试成功后,复制粘贴)
23                 string sql = "update TeacherClass set ShengHao=‘舍名利‘ where Id=6";
24                 //创建一个执行sql语句的对象
25                 using (var cmd =new SqlCommand())
26                 {
27                     cmd.CommandText = sql;
28                     cmd.Connection = connection;
29                     //打开连接
30                     connection.Open();
31                     Console.WriteLine("数据库连接成功");
32                     int count=cmd.ExecuteNonQuery();
33                     Console.WriteLine("受影响的行数是:{0}",count);
34                     Console.WriteLine("sql语句执行成功");
35                 }
36                 //关闭连接,释放资源.因为用了 using,所以这里不用写语句
37             }
38             Console.WriteLine("数据库断开成功");
39             Console.ReadKey();
40 
41         }
42     }
43 }

 

 3 result:

  sql:

技术分享

 

  

  console:

技术分享

 


——————————————————————————————————————————
博文的精髓,在技术部分,更在镇场一诗。版本:VS2015 SqlServer2014 系统:Windows 7
C#是优秀的语言,值得努力学习。我是跟随 传智播客\黑马 的.Net视频教程学习的。
如果博文的内容有可以改进的地方,甚至有错误的地方,请留下评论,我一定努力改正,争取铸成一个良心博客。
注:此文仅作为科研学习,如果我无意中侵犯了您的权益,请务必及时告知,我会做出改正。

C# ado.net基础 更新一行数据 在sqlsever中的一个表中

标签:成功   set   images   错误   cat   身份验证   影响   更新   ado.net   

人气教程排行