2011-09-07 160 views
1

代碼:語法錯誤INSERT INTO語句中-ASP.Net

public partial class deneme1 : System.Web.UI.Page 
{ 
OleDbConnection ConnectionString; 
OleDbCommand cmdText; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    ConnectionString = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Server.MapPath("~/App_Data\\Database.mdb")); 
} 
protected void btnEkle_Click(object sender, EventArgs e) 
{ 
    cmdText = new OleDbCommand("INSERT INTO Users(UserName, Password) values ('"+txtKAdi.Text+"','"+txtSifre.Text+"')", ConnectionString); 

    if (ConnectionString.State == ConnectionState.Closed) 
    { 
     ConnectionString.Open(); 
    } 

    cmdText.ExecuteNonQuery(); 
} 

當我嘗試上面的代碼運行,我得到了錯誤以下。

System.Data.OleDb.OleDbException was unhandled by user code 
    Message="Syntax error in INSERT INTO statement." 
    Source="Microsoft JET Database Engine" 
    ErrorCode=-2147217900 
    StackTrace: 
    konum: System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 
    konum: System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 
    konum: System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) 
    konum: System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) 
    konum: System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) 
    konum: System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 
    konum: deneme1.btnEkle_Click(Object sender, EventArgs e) c:\Users\Pc\Desktop\MasterWeb\deneme1.aspx.cs içinde: satır 32 
    konum: System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
    konum: System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
    konum: System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
    konum: System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
    konum: System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
    konum: System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

回答

2

在Access Password保留字,所以你要引用它,即

INSERT INTO Users(UserName, [Password]) values (... 

注意圍繞 「密碼」 的方括號。

相關問題