现有两段程序
public void UpdateClient()
{
string strSql = "UPDATE [ClientInfo] SET Tel1='" + this.Tel1 + "',"
+ "Tel2='" + this.Tel2 + "',"
+ "ProvinceID=" + this.ProvinceID + ","
+ "CityID=" + this.CityID + ","
+ "Addr='" + this.Addr + "',"
+ "LinkMan='" + this.LinkMan + "',"
+ "Memo='" + this.Memo+ "' WHERE ClientName='" + this.Name + "'";
try
{
ExecuteSQLCmd(strSql);
}
catch
{
throw new Exception("更新失败!请重试!");
}
}
和
protected void btModify_Click1(object sender, EventArgs e)
{
Session["ClientName"]="Name";
currentClient.Tel1 = this.tbTel1.Text.Trim();
currentClient.Tel2 = this.tbTel2.Text.Trim();
currentClient.ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
currentClient.CityID = Convert.ToInt32(ddlCity.SelectedValue);
currentClient.Addr = this.tbAddr.Text.Trim();
currentClient.LinkMan = this.tbLinkMan.Text.Trim();
currentClient.Memo = this.tbMemo.Text.Trim();
if (Session["ClientName"] != null)
{
currentClient.Name = Session["ClientName"].ToString();
}
currentClient.UpdateClient();
this.lbModify.Text = "修改成功!";
}执行的时候能显示“修改成功”这几个字,但是数据库里面的数据就是没有改变,不知道为什么,希望大家指导一下,谢谢
public void UpdateClient()
{
string strSql = "UPDATE [ClientInfo] SET Tel1='" + this.Tel1 + "',"
+ "Tel2='" + this.Tel2 + "',"
+ "ProvinceID=" + this.ProvinceID + ","
+ "CityID=" + this.CityID + ","
+ "Addr='" + this.Addr + "',"
+ "LinkMan='" + this.LinkMan + "',"
+ "Memo='" + this.Memo+ "' WHERE ClientName='" + this.Name + "'";
try
{
ExecuteSQLCmd(strSql);
}
catch
{
throw new Exception("更新失败!请重试!");
}
}
和
protected void btModify_Click1(object sender, EventArgs e)
{
Session["ClientName"]="Name";
currentClient.Tel1 = this.tbTel1.Text.Trim();
currentClient.Tel2 = this.tbTel2.Text.Trim();
currentClient.ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
currentClient.CityID = Convert.ToInt32(ddlCity.SelectedValue);
currentClient.Addr = this.tbAddr.Text.Trim();
currentClient.LinkMan = this.tbLinkMan.Text.Trim();
currentClient.Memo = this.tbMemo.Text.Trim();
if (Session["ClientName"] != null)
{
currentClient.Name = Session["ClientName"].ToString();
}
currentClient.UpdateClient();
this.lbModify.Text = "修改成功!";
}执行的时候能显示“修改成功”这几个字,但是数据库里面的数据就是没有改变,不知道为什么,希望大家指导一下,谢谢
this.lbModify.Text = "修改成功!";
这样做本身就有问题。如果UpdateClient();连接成功插入失败。那你前台不照样显示修改成功吗?你应该把UpdateClient();方法改成有返回值的。
另外,你设置一个断点,我想你就能找出原因了。
这里断点,然后把strSql放到查询分析器中,看看影响了几条记录?
你的语句是UPDATE,可能由于查询条件不匹配导致一条记录也没影响到,
所以你的插入失败但是Lable里还是成功的,你可以象3楼了说的
public bool UpdateClient()
{
string strSql = "UPDATE [ClientInfo] SET Tel1='" + this.Tel1 + "',"
+ "Tel2='" + this.Tel2 + "',"
+ "ProvinceID=" + this.ProvinceID + ","
+ "CityID=" + this.CityID + ","
+ "Addr='" + this.Addr + "',"
+ "LinkMan='" + this.LinkMan + "',"
+ "Memo='" + this.Memo+ "' WHERE ClientName='" + this.Name + "'";
try
{
ExecuteSQLCmd(strSql);
ruturn rrue;
}
catch
{
throw new Exception("更新失败!请重试!");
ruturn false;
}
}
protected void btModify_Click1(object sender, EventArgs e)
{
Session["ClientName"]="Name";
currentClient.Tel1 = this.tbTel1.Text.Trim();
currentClient.Tel2 = this.tbTel2.Text.Trim();
currentClient.ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
currentClient.CityID = Convert.ToInt32(ddlCity.SelectedValue);
currentClient.Addr = this.tbAddr.Text.Trim();
currentClient.LinkMan = this.tbLinkMan.Text.Trim();
currentClient.Memo = this.tbMemo.Text.Trim();
if (Session["ClientName"] != null)
{
currentClient.Name = Session["ClientName"].ToString();
}
if(currentClient.UpdateClient())
{
this.lbModify.Text = "修改成功!";
}
else
{
this.lbModify.Text = "修改失败!"; }
}
+ "Tel2='" + this.Tel2 + "',"
+ "ProvinceID=" + this.ProvinceID + ","
+ "CityID=" + this.CityID + ","
+ "Addr='" + this.Addr + "',"
+ "LinkMan='" + this.LinkMan + "',"
+ "Memo='" + this.Memo+ "' WHERE ClientName='" + this.Name + "'到查询分析器里面查,说是第 1 行: 'Tel2' 附近有语法错误。
可我怎么也找不出错误来啊
Session["ClientID"] = "ID";
if (Session["ClientID"] != null)
{
currentClient.ID = Convert.ToInt32(Session["ClientID"].ToString());
}
currentClient.UpdateClient();
this.lbModify.Text = "修改成功!";
报的错是currentClient.ID = Convert.ToInt32(Session["ClientID"].ToString());
输入字符串的格式不正确。
你这个不是字符串吗?字符串"ID"怎么能转成int型呢?