插入访问数据库

时间:2017-11-19 06:15:27

标签: c# database ms-access

我正在尝试创建一个项目,我需要使用类注册用户,但是当我运行代码时,它会在INSERT STATEMENT中给出SYNTAX ERROR。

class LG 
{
    Connection MainConnection = new Connection();
    public static string _password;
    public static string _username;
    string username;
    string password;

    public LG (string username, string password)
    { 
        this.username = username;
        this.password = password;
    }

    public void add()
    {
        string query = "insert into LG (Username,Password)values('" +                                  
         username + "','" + password + "')";
        OleDbCommand com = new OleDbCommand(query,  
        MainConnection.getConnect());
        com.ExecuteNonQuery();
     }

3 个答案:

答案 0 :(得分:-1)

//Try this out...  
  class LG 
    {
        Connection MainConnection = new Connection();
        public static string _password;
        public static string _username;
        string username;
        string password;

        public LG (string username, string password)
        { 
            this.username = username;
            this.password = password;
        }

        public void add()
        {
            string query = "insert into LG (Username,[Password])values('" +                                  
             username + "','" + password+ "')";
            OleDbCommand com = new OleDbCommand(query,  
           MainConnection.getConnect());
           com.ExecuteNonQuery();
          //close connection here
           MainConnection.Dispose();
         }

答案 1 :(得分:-1)

尝试转义序列:

string query = "insert into LG (Username,Password)values(\'\" + username + \"\',\'\" + password + \"\')";

答案 2 :(得分:-1)

尝试使用类似

的字符串格式
 string query = string.Format("insert into LG (Username,Password) values('{0}','{1}', username, Password)";