简体   繁体   中英

Error - Insert data into Access database in c#

Pls, am having an error code when inserting data into Access database. It keeps saying there's sytanx error in my INSERT INTO statement. Can any one help me to solve this. Here is the code

try {  
            OleDbConnection connection = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\DELL\Documents\EmployeesData.accdb;  
            Persist Security Info = false;");  
            connection.Open();  
            OleDbCommand cmd = new OleDbCommand("insert into EmployeeInfo (UserName, Password) values('" + UserText.Text + "', '" + PassText.Text + "')", connection);  
            cmd.ExecuteNonQuery();  
            MessageBox.Show("Inserted");  
        }  
        catch (Exception ex)  
        {  
            MessageBox.Show("Failed" + ex.ToString());  
        }  

Password is a keyword in MSACCESS so u need to enclosed in [] bracket

 OleDbCommand cmd = new OleDbCommand("insert into EmployeeInfo ([UserName], [Password])
 values('" + UserText.Text + "', '" + PassText.Text + "')", connection);  

Note: always use parameterized queries to avoid SQL Injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM