Я пытаюсь сделать заявку на регистрацию, и мне нужен запрос на обновление. Я пробовал 2 способа добавить параметры, но в итоге база данных MS Access не обновляется. У меня нет сообщений об ошибках.
У меня есть требование использовать файл MS Access (.accdb) в качестве базы данных. В противном случае я бы использовал SQL -базу.
Путь 1:
public void UpdateClient(Client client, string path)
{
string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "\\HvdW_DB.accdb; Persist Security Info=False;";
string Query = "UPDATE ClientTbl SET Name = @Name, Address = @Address, PostalCode = @PostalCode, Gender = @Gender, MobilePhone = @MobilePhone, Phone = @Phone, DateOfBirth = @DateOfBirth, IBAN = @IBAN, IsBenefits = @IsBenefits, CEPA = @CEPA, InformalCare_Name = @InformalCare_Name, InformalCare_Tel = @InformalCare_Tel, InformalCare_Relation = @InformalCare_Relation WHERE ID = @ID";
try
{
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(Query, conn))
{
cmd.Parameters.AddWithValue("@ID", client.ID);
cmd.Parameters.AddWithValue("@Name", client.Name);
cmd.Parameters.AddWithValue("@Address", client.Address);
cmd.Parameters.AddWithValue("@PostalCode", client.PostalCode);
cmd.Parameters.AddWithValue("@Gender", client.Gender);
cmd.Parameters.AddWithValue("@MobilePhone", client.MobilePhone);
cmd.Parameters.AddWithValue("@Phone", client.Phone);
cmd.Parameters.AddWithValue("@DateOfBirth", client.DateOfBirth);
cmd.Parameters.AddWithValue("@IBAN", client.IBAN);
cmd.Parameters.AddWithValue("@IsBenefits", client.IsBenefits);
cmd.Parameters.AddWithValue("@CEPA", client.CEPA);
cmd.Parameters.AddWithValue("@InformalCare_Name", client.InformalCareName);
cmd.Parameters.AddWithValue("@InformalCare_Tel", client.InformalCareTel);
cmd.Parameters.AddWithValue("@InformalCare_Ralation", client.InformalCareRelation);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
catch (OleDbException ex)
{
throw ex;
}
}
Путь 2:
public void UpdateClient(Client client, string path)
{
string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "\\HvdW_DB.accdb; Persist Security Info=False;";
string Query = "UPDATE ClientTbl SET Name = @Name, Address = @Address, PostalCode = @PostalCode, Gender = @Gender, MobilePhone = @MobilePhone, Phone = @Phone, DateOfBirth = @DateOfBirth, IBAN = @IBAN, IsBenefits = @IsBenefits, CEPA = @CEPA, InformalCare_Name = @InformalCare_Name, InformalCare_Tel = @InformalCare_Tel, InformalCare_Relation = @InformalCare_Relation WHERE ID = @ID";
try
{
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(Query, conn))
{
List<OleDbParameter> p = new List<OleDbParameter>()
{
new OleDbParameter("ID",OleDbType.Integer, client.ID),
new OleDbParameter("@Name",(object)client.Name ?? DBNull.Value),
new OleDbParameter("@Address",(object)client.Address ?? DBNull.Value),
new OleDbParameter("@PostalCode",(object)client.PostalCode ?? DBNull.Value),
new OleDbParameter("@Gender",(object)client.Gender ?? DBNull.Value),
new OleDbParameter("@MobilePhone",(object)client.MobilePhone ?? DBNull.Value),
new OleDbParameter("@Phone",(object)client.Phone ?? DBNull.Value),
new OleDbParameter("@DateOfBirth",(object)client.DateOfBirth ?? DBNull.Value),
new OleDbParameter("@IBAN",(object)client.IBAN ?? DBNull.Value),
new OleDbParameter("@IsBenefits",(object)client.IsBenefits ?? DBNull.Value),
new OleDbParameter("@CEPA",(object)client.CEPA ?? DBNull.Value),
new OleDbParameter("@InformalCare_Name",(object)client.InformalCareName ?? DBNull.Value),
new OleDbParameter("@InformalCare_Tel",(object)client.InformalCareTel ?? DBNull.Value),
new OleDbParameter("@InformalCare_Relation",(object)client.InformalCareRelation ?? DBNull.Value),
};
cmd.Parameters.AddRange(p.ToArray());
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
catch (OleDbException ex)
{
throw ex;
}
}
Идентификатор - это PrimaryKey в ClientTbl Таблица и определяется как AutoNumber в MS Access.