C#修改数据
我用下面程序用于修改数据库中的数据 ,但是却达不到效果,有高手请指导下try
{
if (this.textBox1 .Text =="")
{
MessageBox .Show ("请输入要修改的联系人的姓名!");
}
else
{
string sqlconn = "Data Source=.;Initial Catalog=telephone;Integrated Security=True";
string sql;
sql =string .Format ( "select * from telephone where name='"+this.textBox1 .Text.Trim ()+"'");
SqlConnection conn = new SqlConnection(sqlconn);
SqlDataAdapter da = new SqlDataAdapter(sql,sqlconn);
DataSet ds = new DataSet();
da.Fill(ds,"telephone");
if (ds.Tables [0].Rows.Count >0)
{
sql = string.Format(@"update telephone set mb='{3}',ph='{4}',unit='{5}',addr='{6}'where name='{1}'",this .textBox2 .Text .Trim (),
this.textBox3.Text.Trim(), this.textBox4.Text.Trim(), this.textBox5.Text.Trim(), this.textBox1.Text.Trim());
SqlConnection con = new SqlConnection(sqlconn);
SqlCommand cmd = new SqlCommand(sql ,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch
{ }
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}
MessageBox.Show("修改成功!");
//刷新数据
DataSet dss = new DataSet();
sql = "select * from telephone order by id";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
da.Fill(ds,"telephone");
if (ds.Tables["telephone"].Rows.Count > 0)
{
this.dataGridView1.DataSource = ds.Tables["telephone"];
}
}
}
}
catch
{}