码迷,mamicode.com
首页 > 数据库 > 详细

C# 连接 SQLServer

时间:2015-05-27 18:47:06      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

随笔:

       // 将tb_User表数据添加到DataGridView中
string sqlconn = "Data Source=localhost;Initial Catalog=db_QQ;Persist Security Info=True;User ID=sa;Password=99990000"; SqlConnection conn = new SqlConnection(sqlconn); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM tb_User"; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet();
       // "user" 可改为任意字符串(-空串) sda.Fill(ds,
"user"); conn.Close(); dataGridView1.DataSource = ds.Tables["user"];

向SQLServer插入数据(占位符和不使用占位符):

       // 不使用占位符
       cmd.CommandText = "INSERT INTO tb_User(IP, Port, Name, Password, State) VALUES(1, 1, 1, 1, 1)"; cmd.ExecuteNonQuery();
       // 使用占位符 cmd.CommandText
= "INSERT INTO tb_User(IP, Port, Name, Password, State) VALUES(@IP, @Port, @Name, @Password, @State)"; cmd.Parameters.AddWithValue("@IP", "2"); cmd.Parameters.AddWithValue("@Port", "2"); cmd.Parameters.AddWithValue("@Name", "2"); cmd.Parameters.AddWithValue("@Password", "2"); cmd.Parameters.AddWithValue("@State", "2"); cmd.ExecuteNonQuery();

 

C# 连接 SQLServer

标签:

原文地址:http://www.cnblogs.com/bovenson/p/4533915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!