C#连接SQL 更新语句怎么写 ?急`在线等!

来源:百度知道 编辑:UC知道 时间:2024/05/17 20:46:21
string updata = "update Table1 set Name='" + TextBox6.Text + "',Ege=Convert.ToInt32(TextBox7.Text),Biithday='" + Convert.ToDateTime(TextBox8.Text) + "'where";
if (TextBox7.Text.Trim().Equals("") == false)
{
update +=
}
我现在不知道改如何写了!麻烦谁可以给个代码`感谢!!!(我把Name 设为主键了!所以只要更新Ege和Biithday)!
我是SQL和C#的初学者`麻烦可以说的详细点!!谢谢!!
2楼`为什么empty这里会出错!?

string updata = "update Table1 set Name='" + TextBox6.Text + "',Ege=Convert.ToInt32(TextBox7.Text),Biithday='" + Convert.ToDateTime(TextBox8.Text) + "'where";
if (TextBox7.Text.Trim().Equals("") == false) //这里有错误吧!
{
update +=
}
//你不是只要更新Ege和Biithday吗?哪就不要set Name='" + TextBox6.Text 了

建议你这样写

System.Text.StringBuilder sql = new System.Text.StringBuilder();
sql.Append("update Table1");
if(!TextBox8.Text.Trim().Equals(string.empty)) //这里判断是否跟新生日
{
sql.Append(" set Biithday='"+Convert.ToDateTime(TextBox7.Text.Trim())+"'");
}

if(!TextBox8.Text.Trim().Equals(string.empty)) //这里判断是否更新年龄

sql.Append(" set Ege="+Convert.ToInt32(TextBox8.Text.Trim()));



…………
sql.Append( "where+你的筛选条件);

最后把sql.ToString();传出去执行就可以了

建议这样写: