Dapper.Contrib + MS Access:错误 - 在 SQL 语句结束后找到字符
Dapper.Contrib + MS Access: Error - Characters found after the end of the SQL statement
我正在使用带有 Contrib 包的 Dapper ORM。 SELECT
查询完美运行,但我的问题是当我尝试 INSERT
数据时。
Visual Studio 2017 returns 此消息:
Characters found after the end of the SQL statement
使用 Dapper(没有 Dapper.Contrib)执行的基本查询工作正常。但我需要数据库中最后插入的 ID。
在MS Access 2007数据库中插入数据的函数代码:
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;
}
}
class 客户:
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; }
}
}
您正在使用 Dapper.Contrib 并且您的数据库是 MS Access。您的 INSERT 调用正在生成两个 SQL 查询。首先,如您所料,插入记录。其次是在屏幕后面获取新生成的id。
所以,生成的查询看起来像这样:
INSERT INTO Table (......) VALUES (....);
SELECT @@IDENTITY";
这两个查询都是在单次往返中执行的。而这不受 MS Access 支持。 MS Access 不理解分号后的字符,即 SELECT @@IDENTITY";
.
请参考this link.
The Jet database engine does not support the execution of multiple statements in a batch or the use of output parameters, so it is not possible to use either of these techniques to return the new Autonumber
value assigned to an inserted row.
解决方案是分别执行这两个查询或者不执行第二个查询;以不同的方式处理它。但是,您正在使用 Contrib,它 会为您生成 查询。因此,您在这里可以做的事情非常少(自己修改 Contrib 代码)。
坦率地说,我不知道解决这个问题的方法。我从来没有用过 Dapper.Contrib。可能这是在较新版本中修复的。或者可能是 Contrib 不支持 MS Access。我只是想向你解释这个问题。请参考另一个 answer,它讨论了相同的问题,但使用的是 Dapper Extensions。
我正在使用带有 Contrib 包的 Dapper ORM。 SELECT
查询完美运行,但我的问题是当我尝试 INSERT
数据时。
Visual Studio 2017 returns 此消息:
Characters found after the end of the SQL statement
使用 Dapper(没有 Dapper.Contrib)执行的基本查询工作正常。但我需要数据库中最后插入的 ID。
在MS Access 2007数据库中插入数据的函数代码:
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;
}
}
class 客户:
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; }
}
}
您正在使用 Dapper.Contrib 并且您的数据库是 MS Access。您的 INSERT 调用正在生成两个 SQL 查询。首先,如您所料,插入记录。其次是在屏幕后面获取新生成的id。
所以,生成的查询看起来像这样:
INSERT INTO Table (......) VALUES (....);
SELECT @@IDENTITY";
这两个查询都是在单次往返中执行的。而这不受 MS Access 支持。 MS Access 不理解分号后的字符,即 SELECT @@IDENTITY";
.
请参考this link.
The Jet database engine does not support the execution of multiple statements in a batch or the use of output parameters, so it is not possible to use either of these techniques to return the new
Autonumber
value assigned to an inserted row.
解决方案是分别执行这两个查询或者不执行第二个查询;以不同的方式处理它。但是,您正在使用 Contrib,它 会为您生成 查询。因此,您在这里可以做的事情非常少(自己修改 Contrib 代码)。
坦率地说,我不知道解决这个问题的方法。我从来没有用过 Dapper.Contrib。可能这是在较新版本中修复的。或者可能是 Contrib 不支持 MS Access。我只是想向你解释这个问题。请参考另一个 answer,它讨论了相同的问题,但使用的是 Dapper Extensions。