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

来源:互联网 发布:香港中文大学数据库 编辑:程序博客网 时间:2024/04/30 00:45

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

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;//显示数据

      

0 0
原创粉丝点击