C# executrenquery和Update语句
尝试保存时收到此错误 单词update附近的语法不正确 似乎是一个明显的解决办法,但我似乎找不到。希望新鲜的眼睛会有帮助!谢谢C# executrenquery和Update语句,c#,asp.net,C#,Asp.net,尝试保存时收到此错误 单词update附近的语法不正确 似乎是一个明显的解决办法,但我似乎找不到。希望新鲜的眼睛会有帮助!谢谢 protected void btnSave_Click(object sender, EventArgs e) { Button EditButton = (Button)EditLoginView.FindControl("EditButton"); Button SaveButton = (Button)EditLoginView.FindCont
protected void btnSave_Click(object sender, EventArgs e)
{
Button EditButton = (Button)EditLoginView.FindControl("EditButton");
Button SaveButton = (Button)EditLoginView.FindControl("SaveButton");
TitleLanguage.ActiveViewIndex = 0;
LanguageView.ActiveViewIndex = 0;
EditButton.Visible = true;
SaveButton.Visible = false;
//update the file in the database
string strQuery = "UPDATE pages SET en_content = @en_Content, fr_Content = @fr_content, fr_Title=@fr_title, en_Title=@en_title, update=@update WHERE link_title = @link_title";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@en_title", SqlDbType.VarChar).Value = Edit_EnglishT.Text;
cmd.Parameters.Add("@fr_title", SqlDbType.VarChar).Value = Edit_FrenchT.Text;
cmd.Parameters.Add("@en_content", SqlDbType.VarChar).Value = Edit_English.Text;
cmd.Parameters.Add("@fr_content", SqlDbType.VarChar).Value = Edit_French.Text;
cmd.Parameters.Add("@update", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@link_", SqlDbType.VarChar).Value = linktitle;
UpdateData(cmd);
}
private Boolean UpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["randolphConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
更新是T-SQL中的一个重要部分。您应该将其与方括号一起使用,如[UPDATE]
喜欢
作为一般建议,不要在数据库中为标识符和对象名使用保留关键字
也改变你的生活
cmd.Parameters.Add("@link_", SqlDbType.VarChar).Value = linktitle;
到
因为您在strQuery中将参数名声明为@link\u title而不是@link\u
编辑:为了澄清,您不需要为这样一个流程使用UpdateData方法。就这样使用,
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["randolphConnectionString"].ConnectionString;
using(SqlConnection con = new SqlConnection(strConnString))
using(SqlCommand cmd = con.CreateCommand())
{
string strQuery = @"UPDATE pages SET en_content = @en_Content, fr_Content = @fr_content, fr_Title=@fr_title, en_Title=@en_title, [update]=@update WHERE link_title = @link_title";
cmd.CommandText = strQuery;
cmd.Parameters...;
.....
.....
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
您可以定义字段update,它是一个保留关键字
请尝试[update],如本示例中所示:
UPDATE pages
SET en_content = @en_Content
, fr_Content = @fr_content
, fr_Title=@fr_title
, en_Title=@en_title
, [update]=@update
WHERE link_title = @link_title
在最后一个参数中,使用@link\u title 但在Parameter.Add中,您使用:
cmd.Parameters.Add("@link_", SqlDbType.VarChar).Value = linktitle;
尝试更改此选项:
cmd.Parameters.Add("@link_title", SqlDbType.VarChar).Value = linktitle;
多么愚蠢的错误。。。它修复了我所有的错误消息。。。但现在它没有更新我的数据库?我的updatedata函数是更新数据库的合适方法吗?仍然没有骰子和错误。。。它似乎正在运行,但没有更新。@t您确定您的连接字符串是正确的吗?你调试代码了吗?cmd.ExecuteNonQuery之前的cmd是什么样子的;行?是的,我使用相同的连接字符串从数据库中引入数据进行编辑。真是一个假错误。。。它修复了我所有的错误消息。。。但现在它没有更新我的数据库?我的updatedata函数是更新数据库的合适方法吗?也许这个答案会对你有所帮助!检查第一个答案的更新查询有多假错误。。。它修复了我所有的错误消息。。。但现在它没有更新我的数据库?我的updatedata函数是更新数据库的合适方法吗?@trowse:你的方法不是问题所在。可能是尾随空格还是前导空格?更新是否直接在数据库上工作?它应该直接更新数据库,但不想太多。@trowse:我的意思是使用诸如SSMS之类的数据库工具。我真的不知道如何去做?我可以在VisualStudio中完成吗?
cmd.Parameters.Add("@link_", SqlDbType.VarChar).Value = linktitle;
cmd.Parameters.Add("@link_title", SqlDbType.VarChar).Value = linktitle;