System.Data.OleDb.OleDbException(0x80040E14):INSERT INTO语句中的语法错误。在

时间:2016-09-24 21:24:20

标签: c# sql insert oledb

尝试在我的asp.net Web应用程序中的表中插入值时遇到问题。我一直在收到错误 System.Data.OleDb.OleDbException(0x80040E14):INSERT INTO语句中的语法错误。在... 我不确定为什么,我希望它只是一个错字。但这是我的Datalayer类代码:

public void RegisterUser(string username, string city, string state, string favLang, string leastFavLang, string date, string password)
{      
    dbConnection.Open();
    string sqlStmt = "INSERT INTO tblUserInfo (Username, City, State, FavoriteLanguage, LeastFavoriteLanguage, lastComplete, Password)";
   // sqlStmt += "VALUES (" + username + ", " + city + ", " + state + ", " + favLang + ", " + leastFavLang + ", " + date + ", " + password + ")";
    sqlStmt += " VALUES (@username, @city, @state, @favLang, @leastFavLang, @compDate, @password)";
   OleDbCommand dbCommand = new OleDbCommand(sqlStmt, dbConnection);
    OleDbParameter param = new OleDbParameter("@username", username);
    dbCommand.Parameters.Add(param);        
    dbCommand.Parameters.Add(new OleDbParameter("@city",city));
    dbCommand.Parameters.Add(new OleDbParameter("@state",state));
    dbCommand.Parameters.Add(new OleDbParameter("@favLang",favLang));
    dbCommand.Parameters.Add(new OleDbParameter("@leastFavLang",leastFavLang));
    dbCommand.Parameters.Add(new OleDbParameter("@compDate",date));
    dbCommand.Parameters.Add(new OleDbParameter("@password",password));
     dbCommand.ExecuteNonQuery();
    dbConnection.Close();
}

这是页面背后的c#:

protected void AddToDB_Click(object sender, EventArgs e)
{

    bool userAddError = false;
    string tempPath = Server.MapPath("Programaholics.accdb");
    clsDataLayer myDataLayer = new clsDataLayer(tempPath);
    try
    {
        myDataLayer.RegisterUser(lblUsername.Text, lblCity.Text, lblState.Text, lblFavLang.Text, lblLeastFavLang.Text, lblDate.Text,lblpass.Text);
    }
    catch (Exception error)
    {
        userAddError = true;
        string message = "Error.";
       Master.MasterLabel.Text = message + "" + error;
    }
    if (!userAddError)
    {
        Master.MasterLabel.Text = "User added";
    }
}

是的,这是一个学校的项目我已经编写了代码,我只需要帮助解决它为什么不能正常工作。

0 个答案:

没有答案