【问题标题】:Error - Insert data into Access database in c#错误 - 在 c# 中将数据插入 Access 数据库
【发布时间】:2016-07-11 17:50:17
【问题描述】:

请,将数据插入 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());  
        }  

【问题讨论】:

  • 你可以比向 Little Bobby Tables 寻求建议更糟糕......

标签: c# database ms-access


【解决方案1】:

密码是MSACCESS中的关键字,所以需要用[]括号括起来

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

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

【讨论】:

  • 不工作错误代码显示“索引或主键不能包含 Null 值”
  • @OyetokeTobiEmmanuel 在你的表中有一个主键,你没有为它传递值。这就是它给出这个错误的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多