Asp.net插入查询本地数据库(访问)

时间:2013-04-18 15:42:18

标签: asp.net database ms-access insert-into

我不是很擅长将SQL查询运行到本地数据库 - Microsoft Access。我正在尝试编写一个代码,当用户注册其详细信息时,它会将其插入数据库。我写了下面的代码,但它不起作用。我想我错过了什么。任何帮助将不胜感激。

 public void InsertUserDetails(string fname, string sname, string uid, string password, string email)
{
    //Connection string for the datbase
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True";
    OleDbConnection myConn = new OleDbConnection(database);


    //Execute the query
    string queryStr = "Insert into Users(Username, Password, Firstname, Surname, Email) values ('" + fname + "','" + sname + "','" + uid + "','" + password + "','" + email + "')";

     // Create a command object 
    OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
    // Open the connection 
    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();

}

1 个答案:

答案 0 :(得分:0)

Passwordreserved word。将该字段名称括在方括号中,以避免混淆数据库引擎。

string queryStr = "Insert into Users (Username, [Password], Firstname, Surname, Email) values ('" + fname + "','" + sname + "','" + uid + "','" + password + "','" + email + "')";