sql sever的连接以及使用

本文详细介绍了如何在Visual Studio中通过两种方式(本地计算机连接和Windows身份验证方式)连接并操作SQL Server数据库,包括查询、删除、修改和插入数据的操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Sql Server 在Visual Studio的连接有两种方法:


(1)本地计算机连接;

[c#]  view plain copy
  1. string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";  


(2)windows身份验证方式连接;

[c#]  view plain copy
  1. string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";  


二、在Visual Studio中使用:

例1:查询数据库中的数据并且显示出来

[c#]  view plain copy
  1. string s = "Data Source=计算机名称;Initial Catalog=数据库名称;Integrated Security=True";  //此处使用本地计算机连接方式  
  2.             SqlConnection conn = new SqlConnection(s);   //创建连接  
  3.             conn.Open();    //打开连接  
  4.             SqlCommand cmd = conn.CreateCommand();  
  5.             cmd.CommandText = "select * from T_User";   //使用命令  
  6.             SqlDataAdapter adapter=new SqlDataAdapter(cmd);  
  7.             DataTable dt=new DataTable();  
  8.             adapter.Fill(dt);  
  9.               
  10.             conn.Dispose();  //释放所以资源  
  11.             cmd.Dispose();  
  12.             conn.Close();  //关闭连接  
  13.             string realname="";  
  14.             string username="";  
  15.             string mobile="";  
  16.             string address="";  
  17.             for (int i=0;i<dt.Rows.Count;i++)  
  18.             {  
  19.                 realname=dt.Rows[i][3].ToString();  
  20.                 username=dt.Rows[i][1].ToString();  
  21.                 mobile=dt.Rows[i][4].ToString();  
  22.                 address=dt.Rows[i][5].ToString();  
  23.                 Console.WriteLine("姓名为{0},用户名为{1},手机为{2},地址为{3}", realname, username, mobile, address);  
  24.             }  
  25.             Console.ReadKey();  
例2:删除表中数据

[c#]  view plain copy
  1. string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";   //使用windows身份验证  
  2. SqlConnection conn = new SqlConnection(s);  
  3. conn.Open();  
  4. SqlCommand cmd = conn.CreateCommand();  
  5. cmd.CommandText = "delete from T_User where Id=5";  
  6. cmd.ExecuteNonQuery();  
  7.   
  8. cmd.Dispose();  
  9. conn.Close();  
  10. Console.WriteLine("删除成功");  
  11. Console.ReadKey();  

例3:修改表中数据

[c#]  view plain copy
  1. string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";  
  2. SqlConnection conn = new SqlConnection(s);  
  3. conn.Open();  
  4. SqlCommand cmd = conn.CreateCommand();  
  5. cmd.CommandText = "update T_User set Card=@card where ID=3";  
  6. cmd.Parameters.AddWithValue("@card""13000000000000");  
  7. cmd.ExecuteNonQuery();  
  8. cmd.Dispose();  
  9. conn.Close();  
  10. conn.Dispose();  
  11. Console.WriteLine("修改成功!");  
  12. Console.ReadKey();  

例4:向表中插入数据
[c#]  view plain copy
  1. string s = "data source=计算机名称;initial catalog=数据库名称;integrated security=true";  
  2. SqlConnection conn = new SqlConnection(s);  
  3. conn.Open();  
  4. SqlCommand cmd = conn.CreateCommand();  
  5. cmd.CommandText = "insert into T_User(UserName,Password,RealName,Mobile,Address) values(@username,@password,@realname,@mobile,@address)";  
  6. cmd.Parameters.AddWithValue("@username""xingxing");  
  7. cmd.Parameters.AddWithValue("@password""77777");  
  8. cmd.Parameters.AddWithValue("@realname""星星");  
  9. cmd.Parameters.AddWithValue("@mobile", 1300000000);  
  10. cmd.Parameters.AddWithValue("@address""河北省北京市");  
  11. cmd.ExecuteNonQuery();  
  12. cmd.Dispose();  
  13. conn.Close();  
  14. conn.Dispose();  
  15. Console.WriteLine("成功插入一行");  
  16. Console.ReadKey(); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值