请教关于 c# dataGridView 更新、插入 如何做 请问我想用C# dataGridView 对数据库进行数据 update ,delete ,insert 操作,如何实现!谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 delete------------------------------------------------SqlConnection conn = new SqlConnection("server=WHOAMI;uid=sa;pwd=;database=tb_01"); conn.Open(); string strselect = "Select COUNT(*) from tb_01 where BookNumber='" + textBox1.Text + "'"; SqlCommand cmd = new SqlCommand(strselect, conn); int intConut=Convert.ToInt32(cmd.ExecuteScalar().ToString()); if(intConut!=0) { string SqlIns = "delete from tb_01 where BookNumber='" + textBox1.Text + "'"; SqlCommand commad = new SqlCommand(); commad.CommandText = SqlIns; commad.Connection = conn; commad.ExecuteNonQuery(); MessageBox.Show("删除成功!"); filldataview(); } else { MessageBox.Show("编号不正确!"); }---------------------------------------------------------update------------------------------------------------------- SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Server=WHOAMI;uid=sa;pwd=;database=tb_01"; conn.Open(); string strselect = "select COUNT(*) from tb_01 where BookNumber='" + textBox1.Text + "'"; SqlCommand cmd = new SqlCommand(strselect, conn); int intConut = Convert.ToInt32(cmd.ExecuteScalar().ToString()); if (intConut != 0) { string SqlIns = "update tb_01 set BookName='" + textBox2.Text + "',BookP='" + textBox3.Text + "',BookPrice='" + textBox4.Text + "' where BookNumber='" + textBox1.Text + "'"; SqlCommand command = new SqlCommand(); command.CommandText = SqlIns; command.Connection = conn; command.ExecuteNonQuery(); MessageBox.Show("修改成功!"); fillDataview(); } else { MessageBox.Show("编号输入错误,无法修改记录!"); } }--------------------------------------------------------------------------insert------------------------------------------------------------------------ SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Server=WHOAMI;uid=mas;pwd=mas;database=tb_01"; conn.Open(); string SqlIns = "insert into tb_01 values('')"; SqlCommand command = new SqlCommand(); command.CommandText = SqlIns; command.Connection = conn; command.ExecuteNonQuery(); SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_01", conn); DataTable dt = new DataTable(); AdapterSelect.Fill(dt); dataGridView1.DataSource = dt.DefaultView; Entity Framework 的connectionString意思?? c# 访问 oracle 数据库的问题 怀疑一个using的问题 textbox 绑定到 app.config的问题 “鼠标悬浮”效果怎么实现? Winform动态打印问题,请高手帮忙 如何取昨天日期 HttpRequest 的命名空间问题 关于发邮件 ASP.net Starter Kit 国内官方论坛网址多少? C# datagridview问题 我自己写的C#实体类CodeSmith模板
------------------------------------------------
SqlConnection conn = new SqlConnection("server=WHOAMI;uid=sa;pwd=;database=tb_01");
conn.Open();
string strselect = "Select COUNT(*) from tb_01 where BookNumber='" + textBox1.Text + "'";
SqlCommand cmd = new SqlCommand(strselect, conn);
int intConut=Convert.ToInt32(cmd.ExecuteScalar().ToString());
if(intConut!=0)
{
string SqlIns = "delete from tb_01 where BookNumber='" + textBox1.Text + "'";
SqlCommand commad = new SqlCommand();
commad.CommandText = SqlIns;
commad.Connection = conn;
commad.ExecuteNonQuery();
MessageBox.Show("删除成功!");
filldataview(); }
else
{
MessageBox.Show("编号不正确!");
}
---------------------------------------------------------
update
-------------------------------------------------------
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=WHOAMI;uid=sa;pwd=;database=tb_01";
conn.Open();
string strselect = "select COUNT(*) from tb_01 where BookNumber='" + textBox1.Text + "'";
SqlCommand cmd = new SqlCommand(strselect, conn);
int intConut = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (intConut != 0)
{
string SqlIns = "update tb_01 set BookName='" + textBox2.Text + "',BookP='" + textBox3.Text + "',BookPrice='" + textBox4.Text + "' where BookNumber='" + textBox1.Text + "'";
SqlCommand command = new SqlCommand();
command.CommandText = SqlIns;
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show("修改成功!");
fillDataview();
}
else
{
MessageBox.Show("编号输入错误,无法修改记录!"); }
}
--------------------------------------------------------------------------
insert
------------------------------------------------------------------------
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=WHOAMI;uid=mas;pwd=mas;database=tb_01";
conn.Open();
string SqlIns = "insert into tb_01 values('')"; SqlCommand command = new SqlCommand();
command.CommandText = SqlIns;
command.Connection = conn;
command.ExecuteNonQuery();
SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_01", conn);
DataTable dt = new DataTable();
AdapterSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;