C# Sql Server连接(增、删、改、查)

网友投稿 306 2022-09-20

C# Sql Server连接(增、删、改、查)

//增加、删除、更新、查询公用

string MyConn = "server=127.0.0.1;uid=数据库登录名;pwd=密码;database=数据库的名字;Trusted_Connection=no";SqlConnection MyConnection = new SqlConnection(MyConn);

1、增加

string MyInsert = "想要执行的sql语句"; SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection); MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close();

2、删除

string MyDelete = "想要执行的sql语句"; SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection); MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close();

3、更新

string MyUpdate = "想要执行的sql语句"; SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection); MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close();

4、查询

MyConn.Open(); SqlDataAdapter dap = new SqlDataAdapter("想要执行的sql语句",MyConn); DataSet ds = new DataSet();//实例化DataSet类 dap.Fill(ds, "Table");//添加SQL语句并执行 dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示数据

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:获取Xml中指定子节点值
下一篇:DEV Gridcontrol 多选
相关文章

 发表评论

暂时没有评论,来抢沙发吧~