也不是不执行,我已经用断点跟踪过了,都执行了,可是数据库的记录就是没更改,这是怎么
回事啊?(我用的acess数据库)
代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
OleDbConnection cn;
OleDbCommand cmd;
string sqlstr;
sqlstr="update worker set subject=@subject,content=@content,type=@type where id=@id";
cn=new OleDbConnection(Session["strConn"].ToString());
cmd=new OleDbCommand(sqlstr,cn);
string work_subject=subject.Text.Trim();
string content0=content.Text;
string work_content=content0.Replace("\n", "<br>");
cmd.Parameters.Add("@id",OleDbType.TinyInt,8).Value=Request["id"];
cmd.Parameters.Add("@subect",OleDbType.VarChar,50).Value=work_subject;
cmd.Parameters.Add("@content",OleDbType.VarChar,1000).Value=work_content;
cmd.Parameters.Add("@type",typeList.SelectedValue);
try
{
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Msg.Text="更新成功!";
}
catch(Exception ex)
{
Msg.Text="因以下原因,数据更新失败:<br>"+ex.Message;
}
}
}
我执行了之后,已经提示‘更新成功’,可是数据库里的记录就是没改?这是怎么回事啊?
回事啊?(我用的acess数据库)
代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
OleDbConnection cn;
OleDbCommand cmd;
string sqlstr;
sqlstr="update worker set subject=@subject,content=@content,type=@type where id=@id";
cn=new OleDbConnection(Session["strConn"].ToString());
cmd=new OleDbCommand(sqlstr,cn);
string work_subject=subject.Text.Trim();
string content0=content.Text;
string work_content=content0.Replace("\n", "<br>");
cmd.Parameters.Add("@id",OleDbType.TinyInt,8).Value=Request["id"];
cmd.Parameters.Add("@subect",OleDbType.VarChar,50).Value=work_subject;
cmd.Parameters.Add("@content",OleDbType.VarChar,1000).Value=work_content;
cmd.Parameters.Add("@type",typeList.SelectedValue);
try
{
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Msg.Text="更新成功!";
}
catch(Exception ex)
{
Msg.Text="因以下原因,数据更新失败:<br>"+ex.Message;
}
}
}
我执行了之后,已经提示‘更新成功’,可是数据库里的记录就是没改?这是怎么回事啊?
这一句sql放到access里面带上数值,看看能直接运行么?
单步调试,看看你的Request["id"]值是否和要更新的记录匹配!
string content0=content.Text;
string work_content=content0.Replace("\n", "<br>");跟踪一下是否得到值啦!
因为你是update咯
cmd.Parameters.Add("@id",OleDbType.TinyInt,8).Value = id;
看看你id的值是否与数据库对应。
感觉应该是找不到对应的id
所以更新失败
例如:
update worker set subject='" + work_subject + "',content=@content,type=@type where id=@id
response.write Request("id");
.........end
在cmd.Parameters.Add("@id",OleDbType.TinyInt,8).Value=Request["id"]处设断点逐步调试;
在“自动窗口”栏中查看调试后"@id"的值是否是你要修改记录的ID,如果id的值为空的话说明根本就没有id值传过来,这样你肯定修改不成功。另外再查看一下调试后"@subect","@content","@type"的值是否已经变为你所输入的值。
参数又不是很多!
改成sqlstr="update worker set subject='"+work_subject+"',content='"+work_content+"',type="+type+" where id=@id";
就好了看来是
cmd.Parameters.Add("@subject",OleDbType.VarChar,50).Value=work_subject;
cmd.Parameters.Add("@content",OleDbType.VarChar,1000).Value=work_content;
cmd.Parameters.Add("@type",typeList.SelectedValue);
这里的值没传进去啊,大家能帮我看看这几句哪里有毛病吗?