错误 - 在 c# 中将数据插入 Access 数据库

本文关键字:数据 插入 Access 数据库 错误 | 更新日期: 2023-09-27 17:56:13

Pls,在将数据插入 Access 数据库时遇到错误代码。它一直说我的 INSERT INTO 语句中有 sytanx 错误。任何人都可以帮我解决这个问题。这是代码

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());  
        }  

错误 - 在 c# 中将数据插入 Access 数据库

密码是MSACCESS中的一个关键字,因此您需要括在[]括号中

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

注意:始终使用参数化查询以避免 SQL 注入