DataGridView的添加一行数据
public partial class add : Form
{
private string server = "127.0.0.1";
private string user = "sa";
private string pwd = "123456";
private string connstr = "";
private string sql = "";
public add()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
connstr = "server="+ server +";uid="+ user +";pwd="+ pwd +";database=ado";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "insert into 员工管理表(name,sex,age,job) values(@name,@sex,@age,@job)";
SqlCommand comm = new SqlCommand(sql,conn);
comm.Parameters.AddWithValue("@name",txtName.Text);
comm.Parameters.AddWithValue("@sex",txtSex.Text);
comm.Parameters.AddWithValue("@age", txtAge.Text);
comm.Parameters.AddWithValue("@job", txtJob.Text);
if (comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("添加员工成功!");
}
else
{
MessageBox.Show("error");
}
conn.Close();
}
}
删除DataGridView的当前选中行
private void btnDelete_Click(object sender, EventArgs e)
{
//删除选中行
if (this.dataGridView1.SelectedRows.Count > 0)
{
int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value.ToString());
connstr = "server=" + server + ";uid=" + user + ";pwd=" + pwd + ";database=ado";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "delete from 员工管理表 where id=@id";
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.Add(new SqlParameter("@id", id));
if (comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("ok");
}
conn.Close();
conn.Dispose();
}
else
MessageBox.Show("请先选中再操作");
}
修改当前选中行的数据
- 在form1当前选中的行的id传过去
- 用form1传过来的值去数据库中查找并显示在TextBox中
- 修改TextBox中的值并保存
form1中的代码如下:
private void btnUpset_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
int index = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value.ToString());
change change = new change(index.ToString());
change.ShowDialog();
}
else
MessageBox.Show("请先选中再操作");
}
form2中的代码如下:
public partial class change : Form
{
private string server = "127.0.0.1";
private string user = "sa";
private string pwd = "123456";
private string connstr = "";
private string sql = "";
public change(string index)//在这里接收index
{
InitializeComponent();
var id = index;
connstr = "server=" + server + ";uid=" + user + ";pwd=" + pwd + ";database=ado";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "select * from 员工管理表 where id ="+ id +"";
SqlCommand comm = new SqlCommand(sql, conn);
用dataset
SqlDataAdapter ada = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
ada.Fill(ds, "员工管理表");
this.txtId.Text = ds.Tables["员工管理表"].Rows[0]["id"].ToString();
this.txtName.Text = ds.Tables["员工管理表"].Rows[0]["name"].ToString();
this.txtSex.Text = ds.Tables["员工管理表"].Rows[0]["sex"].ToString();
this.txtAge.Text = ds.Tables["员工管理表"].Rows[0]["age"].ToString();
this.txtJob.Text = ds.Tables["员工管理表"].Rows[0]["job"].ToString();
//用reader
//SqlDataReader reader = comm.ExecuteReader();
//if (reader.Read())
//{
// txtName.Text = reader["name"].ToString();
// txtSex.Text = reader["sex"].ToString();
// txtAge.Text = reader["age"].ToString();
// txtJob.Text = reader["job"].ToString();
// txtId.Text = reader["id"].ToString();
// reader.Close();
//}
conn.Close();
conn.Dispose();
}
private void btnChange_Click(object sender, EventArgs e)
{
connstr = "server=" + server + ";uid=" + user + ";pwd=" + pwd + ";database=ado";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "update 员工管理表 set name=@name,sex=@sex,age=@age,job=@job where id="+ txtId.Text +"";
SqlCommand comm = new SqlCommand(sql,conn);
comm.Parameters.AddWithValue("@name", txtName.Text);
comm.Parameters.AddWithValue("@sex", txtSex.Text);
comm.Parameters.AddWithValue("@age", txtAge.Text);
comm.Parameters.AddWithValue("@job", txtJob.Text);
if(comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("修改成功!");
}
conn.Close();
conn.Dispose();
}
}