UPDATE子句中的语法错误

时间:2017-01-10 18:55:50

标签: c# ms-access

我正在尝试更新MS-Access数据库中的值。

直接输入Access时查询工作正常,但是当我尝试在C#中更新它时,我得到“UPDATE子句中的语法错误”异常。

conMngr.ConnectionStr = ConfigurationManager.ConnectionStrings[_connectionStr].ConnectionString;
conMngr.OpenConnection();
try {

    string query = "select Id, counter from tabAppCounter where userId=" + _userId.ToString() + " and appPath = '" + path + "'";
    OleDbDataAdapter adapter = new OleDbDataAdapter(query, conMngr.DbCon);

    DataSet ds = new DataSet();
    adapter.Fill(ds);
    dataGridView.DataSource = ds.Tables[0];

    if (ds.Tables[0].Rows.Count != 0) {
        int count = int.Parse(ds.Tables[0].Rows[0].ItemArray[1].ToString())+ 1;
        string query2 = "UPDATE tabAppCounter set counter=@count where Id=@Id";

        conMngr.OleDbcmd = new OleDbCommand(query2, conMngr.DbCon);
        conMngr.OleDbcmd.Parameters.AddWithValue("@Id", ds.Tables[0].Rows[0].ItemArray[0].ToString());
        conMngr.OleDbcmd.Parameters.AddWithValue("@count", count.ToString());
        conMngr.OleDbcmd.ExecuteNonQuery();
    }        
} catch (Exception e) {
    MessageBox.Show(e.Message + "\n\n" + e.StackTrace);

}
conMngr.CloseConnection();

在ExecuteNonQuery之后,此代码失败,并在UPDATE子句异常中出现语法错误。

我尝试使用预建的查询字符串,但这也不起作用。

2 个答案:

答案 0 :(得分:5)

访问驱动程序(和Ole)不使用命名参数,它们是基于位置的。您传递参数的顺序和更新语句中的参数不匹配,这会给您一个错误。

您确实应该在更新字符串中使用* { margin: 0; padding: 0; } p { font-family: 'Source Sans Pro', sans-serif; font-size: 16px; line-height: 19px; color: #1e4164; margin: 10px 10px; } .content-section-heading { font-family: 'Source Sans Pro', sans-serif; font-size: 12px; line-height: 12px; color: #5c5c5c; margin: 10px 10px; font-weight: 600; } h1 { font-size: 36px; font-family: 'Source Sans Pro', sans-serif; line-height: 122px; color: #1e4164; font-weight: 800; text-align: center; margin-top: 25px; margin-bottom: 70px; } header { height: 127px; width: 100%; background-color: #569ABD; box-shadow: rgba(80, 80, 80, 0.7) 1px 1px 2px 0px; position: relative; z-index: 2; } #header-fixedWidth { width: 1253px; margin: 0 auto; } #header-fixedWidth img { margin-top: 3px; } #banner { width: 100%; height: 772px; display: block; } #main-content { display: block; width: 100%; height: 100%; border: 1px solid black; } #mainContent-fixedWidth { width: 1253px; height: 100%; margin: 0 auto; border: 1px solid #ccc; } .content { height:340px; width: 220px; background-color: white; box-shadow: rgba(192, 192, 192, 0.8) 0px 10px 10px 0px; float: left; margin: 0px 0px 40px 30px; } .content #tempContentImage { height: 180px; width: 222px; }作为占位符。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>OECTA Template</title>
        <link rel="stylesheet" href="style.css" />
        <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet" />
    </head>
    <body>
        <header>
            <div id="header-fixedWidth">
                <img src="Images/header/navigationMenu.png" alt="mainNavImage" id="mainNav" />
            </div>
        </header>
        <div id="banner">
            <img src="Images/Banner/BenefitsBanner.jpg" alt="bannerImage" id="bannerTemplate" />
        </div>
        <div id="main-content">
            <div id="mainContent-fixedWidth">
                <h1>Intranet</h1>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
                </div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
                <div class="content">
                    <img src="Images/contentItems/briefNewsImage.jpg" alt="tempContentImage" id="tempContentImage" />
                    <p class="content-section-heading">Section Heading</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis, obcaecati.</p>
</div>
            </div>
        </div>
    </body>
</html>

最后确保类型正确,传入的类型必须与数据库中的类型匹配。因此,如果?在两个地方都是int,则传递string query2 = "UPDATE tabAppCounter set counter=? where Id=?"; conMngr.OleDbcmd = new OleDbCommand(query2, conMngr.DbCon); conMngr.OleDbcmd.Parameters.AddWithValue("@count", count.ToString()); conMngr.OleDbcmd.Parameters.AddWithValue("@Id", ds.Tables[0].Rows[0].ItemArray[0].ToString()); conMngr.OleDbcmd.ExecuteNonQuery(); 而不是count

答案 1 :(得分:1)

希望这会对其他人有所帮助:这对我有用。

而不是

string query2 = "UPDATE tabAppCounter set counter=@count where Id=@Id";

我用过

string query2 = "UPDATE [tabAppCounter] set [counter]=@count where [Id]=@Id";

而且神奇地说,一切都开始正常运作了。