我正在尝试使用OledB连接通过C#更新Access 2010数据库上的数据/记录,并尝试创建一个能够将数据插入并更新到数据库中的应用程序。
我的应用程序包含3种可以插入&将数据更新为记录,到目前为止,我已经有其中一个工作,但正在努力做另外两个
我收到的错误是:以下方法或属性之间的调用不明确。
在我看来,我认为错误是由于要求多个更新方法都使用不同形式的数据,这导致错误,但我不知道如何纠正这个,所以我要求一些帮助。
以下是代码:
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#,所以可能需要更多解释
不要介意从表格中提供任何进一步的细节或代码,所以请随时提出
非常感谢
答案 0 :(得分:0)
您有两个名称Update的方法具有相同的签名。重命名其中一个。
也就是说,改变
public void Update(Customer oldCustomer, Customer newCustomer)
其中一个功能的函数名称
public void Update1(Customer oldCustomer, Customer newCustomer)
更好的练习总是为函数提供有意义的函数名称,如