【问题标题】:Editing an Access Database using Multiple Identical Methods via C#/OledB通过 C#/OledB 使用多个相同的方法编辑 Access 数据库
【发布时间】:2014-04-03 21:39:56
【问题描述】:

我正在尝试使用 OledB 连接通过 C# 更新 Access 2010 数据库上的数据/记录,并尝试制作一个能够将数据插入和更新到数据库中的应用程序。

我的应用程序由 3 个表单组成,它们可以将数据插入和更新到记录中,到目前为止,我已经完成了其中一个工作,但正在努力完成另外两个

我收到的错误是:以下方法或属性之间的调用不明确。

在我看来,我认为错误是由于有多个更新方法都使用来自不同形式的数据,这导致了错误,但我不知道如何更正,所以我寻求一些帮助。

p>

代码如下:

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.Surname, [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.Parameters.AddWithValue("@Forename", newCustomer.Forename1);
                command.Parameters.AddWithValue("@Surname", newCustomer.Surname1);
                command.Parameters.AddWithValue("@Email Address", newCustomer.EAddress1);
                command.Parameters.AddWithValue("@Home Phone Number", newCustomer.HomePhone1);
                command.Parameters.AddWithValue("@Mobile Phone Number", newCustomer.MobNum1);
                command.Parameters.AddWithValue("@Address", newCustomer.Address1);
                command.Parameters.AddWithValue("@AreaTown", newCustomer.AreaTown1);
                command.Parameters.AddWithValue("@County", newCustomer.County1);
                command.Parameters.AddWithValue("@Postcode", newCustomer.Postcode1);
                command.Parameters.AddWithValue("@ID", oldCustomer.Id);

                command.CommandType = CommandType.Text;
                connection.Open();

                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        public void Update(Customer oldCustomer, Customer newCustomer)
        {
            try
            {
                command.CommandText = "UPDATE CustomerData SET [Work to be Completed]= @newCustomer.WorkTBC1, [Work Completed]= @newCustomer.WorkComp1 WHERE [ID] = @oldCustomer.Id";
                command.Parameters.AddWithValue("Work to be Completed", newCustomer.WorkTBC1);
                command.Parameters.AddWithValue("Completed Work", newCustomer.WorkComp1);
                command.Parameters.AddWithValue("@ID", oldCustomer.Id);

                command.CommandType = CommandType.Text;
                connection.Open();

                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        public void Update(Customer oldCustomer, Customer newCustomer)
        {
            try
            {
                command.CommandText = "UPDATE CustomerData SET [Money Received]= @newCustomer.MReceived1, [Money Spent]= @newCustomer.MSpent1, [Bank Number]= @newCustomer.BankNo1, [Sort Code]= @newCustomer.SortCode1, [Account Number]= @newCustomer.AccountNo1, [Commencement Date]= @newCustomer.ComDate1, [Completion Date]= @newCustomer.CompDate1 WHERE [ID] = @oldCustomer.Id";
                command.Parameters.AddWithValue("Money Received", newCustomer.MReceived1);
                command.Parameters.AddWithValue("Money Spent", newCustomer.MSpent1);
                command.Parameters.AddWithValue("Bank Number", newCustomer.BankNo1);
                command.Parameters.AddWithValue("Sort Code", newCustomer.SortCode1);
                command.Parameters.AddWithValue("Account Number", newCustomer.AccountNo1);
                command.Parameters.AddWithValue("Commencement Date", newCustomer.ComDate1);
                command.Parameters.AddWithValue("Completion Date", newCustomer.CompDate1);
                command.Parameters.AddWithValue("@ID", oldCustomer.Id);

                command.CommandType = CommandType.Text;
                connection.Open();

                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
             finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        public void Delete(Customer p)
        {
            try
            {
                command.CommandText = "DELETE FROM CustomerData WHERE [ID] = " + p.Id + "";
                command.CommandType = CommandType.Text;
                connection.Open();

                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
    }
}

代码有点长见谅

我才刚刚开始使用 C#,所以可能需要更多解释

不介意提供更多详细信息或表格中的代码,请随时询问

非常感谢

【问题讨论】:

    标签: c# oledb ms-access-2010


    【解决方案1】:

    您有两个名称为 Update 且签名相同的方法。重命名其中一个。
    也就是改变

    public void Update(Customer oldCustomer, Customer newCustomer)
    

    要调用的函数之一的函数名

    public void Update1(Customer oldCustomer, Customer newCustomer)
    

    更好的做法是始终为函数赋予有意义的函数名称,例如

    1. 插入客户(添加客户)
    2. 更新客户
    3. 更新CustomerWorkDetails
    4. 删除客户

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 2020-07-06
      • 2011-01-23
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多