zsy001 发表于 2016-11-3 09:22:27

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

  

  
  
页: [1]
查看完整版本: C# Sql Server连接(增、删、改、查)