I am using Dapper ORM with the Contrib package. The SELECT
query works perfectly but my problem is when I try to INSERT
data.
Visual Studio 2017 returns this message:
Characters found after the end of the SQL statement
The basic query executed with Dapper (no Dapper.Contrib) works fine. But I need the last inserted id from the database.
The code of the function to insert data in a MS Access 2007 database:
public string AddCustomer(string lastName, string firstName)
{
using (var connection = new OleDbConnection(connectionString))
{
try
{
connection.Open();
// Inserts data into the database.
var insertion = connection.Insert(
new Customer { Customer_lastName = LastNameManipulation(lastName), Customer_firstName = FirstNameManipulation(firstName) }
);
// Defines new customer.
Customer customer = new Customer
{
Customer_id = Convert.ToInt32(insertion),
Customer_lastName = LastNameManipulation(lastName),
Customer_firstName = FirstNameManipulation(firstName)
};
// Insertion into data List.
data.AddCustomer(customer);
message = "Customer added with success.";
}
catch (Exception e)
{
message = e.Message.ToString();
}
finally
{
connection.Close();
}
return message;
}
}
The class Customer :
using System;
using Dapper.Contrib.Extensions;
namespace DataLibrary
{
[Serializable]
[Table("Customer")]
public class Customer
{
[Key]
[Computed]
public int Customer_id { get; set; }
[Write(true)]
public string Customer_lastName { get; set; }
[Write(true)]
public string Customer_firstName { get; set; }
}
}