简体   繁体   English

错误-将数据插入C#中的Access数据库

[英]Error - Insert data into Access database in c#

Pls, am having an error code when inserting data into Access database. 请将数据插入Access数据库时出现错误代码。 It keeps saying there's sytanx error in my INSERT INTO statement. 它总是说我的INSERT INTO语句中有sytanx错误。 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 密码是MSACCESS中的关键字,因此您需要将其括在[]括号中

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

Note: always use parameterized queries to avoid SQL Injection 注意:始终使用参数化查询来避免SQL注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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