Visual Studio 2010 - 更新 C# / OledB 中访问数据库的语句语法错误

本文关键字:数据库 访问 语句 错误 语法 OledB 2010 Studio 更新 Visual | 更新日期: 2023-09-27 17:55:21

我正在尝试使用 OledB 连接通过 C# 更新 Access 2010 数据库上的数据/记录,并尝试创建一个能够插入、更新、删除数据库数据的应用程序。到目前为止,我可以正常插入数据库并使用 ComboBox 选择记录,但尚未更新。

它出现以下错误:

类型为"System.Data.OleDb.OleDbException"的未处理异常 发生在类库 2 中.dll

其他信息:UPDATE 语句中的语法错误。

注意:我尝试使用方括号,但没有太大变化,而是出现了致命错误

这是代码:

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#,所以可能需要更多解释

不介意提供任何进一步的细节,所以请随时询问

Visual Studio 2010 - 更新 C# / OledB 中访问数据库的语句语法错误

方括号封装带有空格的列名,类似于您在 INSERT 语句中的做法。

..., [Home Phone Number] = '" + newCustomer.HomePhone1 + "', ...

此外,请考虑参数化查询。它更安全,更易于维护。

..., [Home Phone Number] = @HomePhoneNumber, ...
command.Parameters.AddWithValue("@HomePhoneNumber", newCustomer.HomePhone1);

尽可能避免在列名称中使用空格。您可以同样轻松地使用下划线,然后您不必记住在引用它们的所有位置都用括号包围它们。