【问题标题】:ExecutreNonQuery and Update StatementExecuteNonQuery 和更新语句
【发布时间】:2014-03-03 00:34:13
【问题描述】:

当我尝试保存时收到此错误。

update 一词附近的语法不正确。

似乎是一个明显的修复,但我似乎无法找到它。希望新鲜的眼睛会有所帮助!谢谢

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();
    }

}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    UPDATE 是 T-SQL 中的 reserved keyword。您应该将它与方括号一起使用,例如[UPDATE]

    喜欢;

    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.Parameters.Add("@link_", SqlDbType.VarChar).Value = linktitle;
    

    cmd.Parameters.Add("@link_title", SqlDbType.VarChar).Value = linktitle;
    

    因为您在 strQuery 中将参数名称声明为 @link_title 而不是 @link_

    编辑:为了澄清,您不需要为这样的过程使用方法 (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();
    }
    

    【讨论】:

    • 多么愚蠢的错误......它修复了我所有的错误消息......但现在它没有更新我的数据库?我的 updatedata 函数是更新数据库的合适方法吗?
    • 仍然没有骰子,也没有错误......它似乎运行但没有更新。
    • @trowse 你确定你的连接字符串是正确的?你调试过你的代码吗?你的cmdcmd.ExecuteNonQuery(); 行之前是什么样子的?
    • 是的,我使用相同的连接字符串从数据库中调入数据进行编辑。
    【解决方案2】:

    您定义字段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
    

    【讨论】:

    • 多么愚蠢的错误......它修复了我所有的错误消息......但现在它没有更新我的数据库?我的 updatedata 函数是更新数据库的合适方法吗?
    • @trowse:你的方法不是问题。也许是尾随或前导空格?更新是否直接在数据库上工作?
    • 它应该是直接更新数据库,但不想要。
    • @trowse:我的意思是使用像 SSMS 这样的数据库工具。
    • 我不太确定该怎么做?我可以在 Visual Studio 中完成吗?
    【解决方案3】:

    在你的最后一个参数中,你使用@link_title 但在 Parameter.Add 中,您使用:

    cmd.Parameters.Add("@link_", SqlDbType.VarChar).Value = linktitle;
    

    尝试改变这个:

    cmd.Parameters.Add("@link_title", SqlDbType.VarChar).Value = linktitle;
    

    【讨论】:

    • 多么愚蠢的错误......它修复了我所有的错误消息......但现在它没有更新我的数据库?我的 updatedata 函数是更新数据库的合适方法吗?
    • 也许这个答案会对你有所帮助!检查第一个答案stackoverflow.com/questions/15246182/…的UPDATE查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2023-04-01
    • 2011-09-14
    相关资源
    最近更新 更多