asp.net更新数据提示成功,但是没有更新数据库

t564436844 2017-03-06 01:06:51
string name1 = TextBox1.Text.ToString();
string sex1 = TextBox2.Text.ToString();
string email1 = TextBox3.Text.ToString();

try
{

string sql1 = "update usertable set usersex='" + sex1 + "',useremail='" + email1 + "' where username='" + name1 + "'";
SqlConnection conn = new SqlConnection();
SqlCommand cmd = DB.command(sql1);
int i = DB.command(sql1).ExecuteNonQuery();
if (i > 0)
Response.Write("<script>alert('信息修改成功');window.location='gerenzhongxin.aspx'</script>");
else
Response.Write("<script>alert('失败!')</script>");

}
catch
{
Response.Write("<script>alert('失败!')</script>");//提示错误
}
大神们帮忙看看,是哪里写错了吗
...全文
358 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
熙风 2017-11-15
  • 打赏
  • 举报
回复
引用 5 楼 yyf6767667 的回复:
我也遇到类似问题,我是用VB写的代码,代码如下: Dim connstr As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString Dim conn As New SqlConnection(connstr) conn.Open() Dim Mycmd As New SqlCommand("update config set school='" & txtSchool.Text & "',email='" & txtEmail.Text & "',tel='" & txtTel.Text & "',xueke='" & txtXueke.Text & "',mianmao='" & txtMianmao.Text & "',shenfen='" & txtShenfen.Text & "',zhicheng='" & txtZhicheng.Text & "',xueli='" & txtXueli.Text & "',zhengshu='" & txtZhengshu.Text & "'", conn) Mycmd.ExecuteNonQuery() MsgBox("更新完成!", MsgBoxStyle.OkOnly, "系统提示") Mycmd.Clone() conn.Close() 提示成功,但是数据库没有更新。求大神们帮忙!!
把脚本输出到sql里面直接执行看下效果
yyf6767667 2017-11-15
  • 打赏
  • 举报
回复
我也遇到类似问题,我是用VB写的代码,代码如下: Dim connstr As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString Dim conn As New SqlConnection(connstr) conn.Open() Dim Mycmd As New SqlCommand("update config set school='" & txtSchool.Text & "',email='" & txtEmail.Text & "',tel='" & txtTel.Text & "',xueke='" & txtXueke.Text & "',mianmao='" & txtMianmao.Text & "',shenfen='" & txtShenfen.Text & "',zhicheng='" & txtZhicheng.Text & "',xueli='" & txtXueli.Text & "',zhengshu='" & txtZhengshu.Text & "'", conn) Mycmd.ExecuteNonQuery() MsgBox("更新完成!", MsgBoxStyle.OkOnly, "系统提示") Mycmd.Clone() conn.Close() 提示成功,但是数据库没有更新。求大神们帮忙!!
浮沉与木 2017-03-06
  • 打赏
  • 举报
回复
你这儿是有问题的,cmd的对象和执行execute的对象不是一个,而且你没发现 你的conn 和 cmd 都没用上,获取值一点意义都没有,如果那个DB是封装好的基类的话直接调用就行的吧,不需要创建连接啥的。而且最重要的是要调试。。。看看你生成的SQL语句,看看你执行的查询。
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection(); SqlCommand cmd = DB.command(sql1); int i = DB.command(sql1).ExecuteNonQuery(); 看起来你只要最后一句就行了…… 你还是帖下你那个DB封装的代码吧
xuggzu 2017-03-06
  • 打赏
  • 举报
回复
把conn和cmd关掉后再看数据库

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧