//增加
DataBase dataBase = new DataBase();
SqlConnection sqlConnection = dataBase.getConn();
//打开连接
sqlConnection.Open();
//定义sql语句
string sql = "insert into test1007.guest.person values('"+personIdText.Text+"','"+personNameText.Text+"','"+personPasswordText.Text+"','"+personRoleText.SelectedItem.ToString()+"')";
//执行sqlCommand对象
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
MessageBox.Show("员工信息保存成功!");
//关闭连接
sqlConnection.Close();
//修改
DataBase dataBase = new DataBase();
SqlConnection sqlConnection = dataBase.getConn();
//打开连接
sqlConnection.Open();
//定义sql语句
string sql = "update test1007.guest.person set name='"+personNameText.Text+"',password='"+personPasswordText.Text+"',role='"+personRoleText.SelectedItem.ToString()+"' where id='"+personIdText.Text+"'";
//执行sqlCommand对象
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
MessageBox.Show("员工信息修改成功!");
//关闭连接
sqlConnection.Close();
//查询
DataBase dataBase = new DataBase();
SqlConnection sqlConnection = dataBase.getConn();
//打开连接
sqlConnection.Open();
//定义sql语句
string sql = "select * from test1007.guest.person where role = '"+checkPerson.Text+"'";
//执行sqlCommand对象
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
//创建数据适配器
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
//执行sql语句
sqlDataAdapter.SelectCommand = sqlCommand;
//创建数据集
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet, "test1007.guest.person");
personData.DataSource = dataSet.Tables[0];
//关闭连接
sqlConnection.Close();
//删除
DataBase dataBase = new DataBase();
SqlConnection sqlConnection = dataBase.getConn();
//打开连接
sqlConnection.Open();
//定义sql语句
string sql = "delete from test1007.guest.person where id = '"+key+"'";
//执行sqlCommand对象
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
MessageBox.Show("员工信息删除成功!");
//关闭连接
sqlConnection.Close();
标签:dataBase,C#,DataBase,改查,new,sql,增删,sqlConnection,sqlCommand
From: https://www.cnblogs.com/liuzijin/p/17776591.html