C# C/OledB中Access数据库的Update语句语法错误
我正在尝试使用OledB连接通过C更新Access 2010数据库上的数据/记录,并尝试创建一个能够插入、更新和删除数据库数据的应用程序。到目前为止,我可以插入数据库,并使用组合框选择一条记录,但到目前为止尚未更新 它会出现以下错误: “System.Data.OleDb.OLEDBEException”类型的未处理异常 发生在ClassLibrary2.dll中 附加信息:UPDATE语句中的语法错误C# C/OledB中Access数据库的Update语句语法错误,c#,visual-studio-2010,ms-access,oledb,C#,Visual Studio 2010,Ms Access,Oledb,我正在尝试使用OledB连接通过C更新Access 2010数据库上的数据/记录,并尝试创建一个能够插入、更新和删除数据库数据的应用程序。到目前为止,我可以插入数据库,并使用组合框选择一条记录,但到目前为止尚未更新 它会出现以下错误: “System.Data.OleDb.OLEDBEException”类型的未处理异常 发生在ClassLibrary2.dll中 附加信息:UPDATE语句中的语法错误 ..., [Home Phone Number] = '" + newCustomer.Ho
..., [Home Phone Number] = '" + newCustomer.HomePhone1 + "', ...
注意:我尝试过使用方括号,但没有太大变化,而是出现了一个致命错误
代码如下:
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary;
using System.Data;
namespace ClassLibrary2
{
public class Class1
{
OleDbConnection connection;
OleDbCommand command;
private void ConnectTo()
{
connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\CMS\CustomerDatabase.accdb;Persist Security Info=False");
command = connection.CreateCommand();
}
public Class1()
{
ConnectTo();
}
public void Insert(Customer p)
{
try
{
command.CommandText = "INSERT INTO CustomerData ([Forename], [Surname], [Email Address], [Home Phone Number], [Mobile Phone Number], [Address], [AreaTown], [County], [Postcode]) VALUES('" + p.Forename1 + "', '" + p.Surname1 + "', '" + p.EAddress1 + "', '" + p.HomePhone1 + "' , '" + p.MobNum1 + "' , '" + p.Address1 + "', '" + p.AreaTown1 + "', '" + p.County1 + "', '" + p.Postcode1 + "')";
command.CommandType = CommandType.Text;
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
public List<Customer> FillComboBox()
{
List<Customer> CustomersList = new List<Customer>();
try
{
command.CommandText = "SELECT * FROM CustomerData";
command.CommandType = CommandType.Text;
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Customer p = new Customer();
p.Id = Convert.ToInt32(reader["ID"].ToString());
p.Forename1 = reader["Forename"].ToString();
p.Surname1 = reader["Surname"].ToString();
p.EAddress1 = reader["Email Address"].ToString();
p.HomePhone1 = reader["Home Phone Number"].ToString();
p.MobNum1 = reader["Mobile Phone Number"].ToString();
p.Address1 = reader["Address"].ToString();
p.AreaTown1 = reader["AreaTown"].ToString();
p.County1 = reader["County"].ToString();
p.Postcode1 = reader["Postcode"].ToString();
CustomersList.Add(p);
}
return CustomersList;
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
public void Update(Customer oldCustomer, Customer newCustomer)
{
try
{
command.CommandText = "UPDATE CustomerData SET Forename= '" + newCustomer.Forename1 + "', Surname= '" + newCustomer.Surname1 + "', Email Address= '" + newCustomer.EAddress1 + "', Home Phone Number= '" + newCustomer.HomePhone1 + "', Mobile Phone Number= '" + newCustomer.MobNum1 + "', Address= '" + newCustomer.Address1 + "', AreaTown= '" + newCustomer.AreaTown1 + "', County= '" + newCustomer.County1 + "', Postcode= '" + newCustomer.Postcode1 + "' WHERE ID= ' + oldCustomer.Id'";
command.CommandType = CommandType.Text;
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
}
}
抱歉,如果代码有点长
我刚刚开始使用C,所以可能需要更多的解释
不介意提供任何进一步的详细信息,因此请随意询问如何用方括号将带有空格的列名封装起来,类似于您在INSERT语句中所做的操作
..., [Home Phone Number] = '" + newCustomer.HomePhone1 + "', ...
此外,还要研究参数化查询。它更安全,更容易维护
..., [Home Phone Number] = @HomePhoneNumber, ...
command.Parameters.AddWithValue("@HomePhoneNumber", newCustomer.HomePhone1);
在列名中尽可能避免使用空格。您可以同样轻松地使用下划线,然后您不必记住在引用下划线的任何地方都用括号括起来。从sql注入的角度和数据类型的角度来看,使用参数(例如True)要安全得多,但参数肯定会安全得多?另外,使用没有空格的传统名称不是更好吗?是的,你们都是对的,只是需要对它们进行参数化。将来我不会用空格。谢谢你的帮助