C#用datagridview怎么删除sql中的记录?
{SqlConnectionconn=ConnectionFactory.getconnection();SqlCommandcmd=newSqlCommand();cm...
{
SqlConnection conn = ConnectionFactory.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into GoodsTbl(商品名,商品编号) values(@商品名, @商品编号)";
cmd.Parameters.Add("@商品名", SqlDbType.VarChar, 10);
cmd.Parameters.Add("@商品编号", SqlDbType.VarChar, 10);
cmd.Parameters["@商品名"].Value = goodsTbx.Text;
cmd.Parameters["@商品编号"].Value = goodsnumTbx.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
this.readData();
}
public void readData()
{
SqlConnection conn = ConnectionFactory.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from GoodsTbl";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0].Value = dr["商品编号"].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[1].Value = dr["商品名"].ToString();
}
dr.Close();
dr.Dispose();
cmd.Dispose();
}
这是添加代码?怎么删除啊?求救高人.要代码的.谢谢! 展开
SqlConnection conn = ConnectionFactory.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into GoodsTbl(商品名,商品编号) values(@商品名, @商品编号)";
cmd.Parameters.Add("@商品名", SqlDbType.VarChar, 10);
cmd.Parameters.Add("@商品编号", SqlDbType.VarChar, 10);
cmd.Parameters["@商品名"].Value = goodsTbx.Text;
cmd.Parameters["@商品编号"].Value = goodsnumTbx.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
this.readData();
}
public void readData()
{
SqlConnection conn = ConnectionFactory.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from GoodsTbl";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0].Value = dr["商品编号"].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[1].Value = dr["商品名"].ToString();
}
dr.Close();
dr.Dispose();
cmd.Dispose();
}
这是添加代码?怎么删除啊?求救高人.要代码的.谢谢! 展开
3个回答
展开全部
行删除时
((DataTable) dataGridView1.DataSource).Rows[行号].Delete();
更新时把((DataTable)DataGridView.DataSource)作为DataTable去更新数据库就行。如下:
SqlConnection conn = ConnectionFactory.getconnection();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from GoodsTbl", conn);
dataAdapter.Update((DataTable)dataGridView1.DataSource);
((DataTable) dataGridView1.DataSource).Rows[行号].Delete();
更新时把((DataTable)DataGridView.DataSource)作为DataTable去更新数据库就行。如下:
SqlConnection conn = ConnectionFactory.getconnection();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from GoodsTbl", conn);
dataAdapter.Update((DataTable)dataGridView1.DataSource);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "delete GoodsTbl where id = @商品编号";
cmd.Parameters.Add("@商品编号", SqlDbType.VarChar, 10);
cmd.Parameters["@商品编号"].Value = goodsnumTbx.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd.Connection = conn;
cmd.CommandText = "delete GoodsTbl where id = @商品编号";
cmd.Parameters.Add("@商品编号", SqlDbType.VarChar, 10);
cmd.Parameters["@商品编号"].Value = goodsnumTbx.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方法很多啊。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询