[英]Dapper insert into database using list
I am using dapper and I'm trying to use the following tutorial for inserting a list into the database. 我正在使用dapper,我正在尝试使用以下教程将列表插入数据库。
https://dapper-tutorial.net/knowledge-base/17150542/how-to-insert-a-csharp-list-to-database-using-dapper-net https://dapper-tutorial.net/knowledge-base/17150542/how-to-insert-a-csharp-list-to-database-using-dapper-net
I first thought from this example that it meant that @A @B had to be in my class, it was not obvious from the example that they had to be in my class. 我首先从这个例子中想到,这意味着@A @B必须在我的班级中,从示例中不明显他们必须在我的班级。
public void ExportTOSql()
{
string connectionString;
connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["Dapper"].ConnectionString.ToString();
_salesOrders = Program.SageDatabaseHelper.FetchSoPOrdersODBC().OrderByDescending(o => o.OrderDate).ToList();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string processQuery = "INSERT INTO SalesOrders VALUES (@OrderDate, @OrderNumber, @DespatchDate,@AccountReference,@CustomerOrderNumber,@Name,@TotalAmount,@Allocated,@Despatched,@Printed)";
conn.Execute(processQuery, _salesOrders);
}
My Sales Order class is as follows and you can see OrderDate
is there. 我的销售订单类如下,您可以看到
OrderDate
在那里。
public class SalesOrder
{
public DateTime OrderDate;
public int OrderNumber;
public byte OrderType;
public string DespatchDate;
public string AccountReference;
public string CustomerOrderNumber;
public string Name;
public double TotalAmount;
public string Allocated;
public string Despatched;
public bool Printed;
}
But as you can see from the screenshot, this is the message I got: 但是从截图中可以看出,这是我收到的消息:
Edit 2 OK: I have gotten a step further thanks to help improving my knowledge on this. 编辑2确定:由于帮助提高了我的知识,我已经更进了一步。 Now the structure is:
现在的结构是:
public class SalesOrder
{
public int OrderNumber { get; set; }
public DateTime OrderDate { get; set; }
public byte OrderType { get; set; }
public DateTime DespatchDate { get; set; }
public string AccountReference { get; set; }
public string CustomerOrderNumber { get; set; }
public string Name { get; set; }
public double TotalAmount { get; set; }
public string Allocated { get; set; }
public string Despatched { get; set; }
public bool Printed { get; set; }
}
And my export method is as follows: 我的导出方法如下:
public void ExportTOSql()
{
string connectionString;
connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["Dapper"].ConnectionString.ToString();
_salesOrders = Program.SageDatabaseHelper.FetchSoPOrdersODBC().OrderByDescending(o => o.OrderDate).ToList();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string processQuery = "INSERT INTO SalesOrders VALUES ( @OrderNumber,@OrderDate,@OrderType , @DespatchDate,@AccountReference,@CustomerOrderNumber,@Name,@TotalAmount,@Allocated,@Despatched,@Printed)";
conn.Execute(processQuery, _salesOrders);
}
And my sql table is as follows, but now I am getting the following: 我的sql表如下,但现在我得到以下内容:
System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.'
System.Data.SqlClient.SqlException:'将数据类型nvarchar转换为数字时出错。
So the issue here is that it still fails sending the data to the SQL table. 所以这里的问题是它仍然无法将数据发送到SQL表。
It is because you are using fields and not properties in your model. 这是因为您在模型中使用的是字段而不是属性 。 Try adding
{get;set;}
to each field to make them properties. 尝试将
{get;set;}
到每个字段以使其成为属性。
public class SalesOrder
{
public DateTime OrderDate { get; set; }
public int OrderNumber { get; set; }
public byte OrderType { get; set; }
public string DespatchDate { get; set; }
public string AccountReference { get; set; }
public string CustomerOrderNumber { get; set; }
public string Name { get; set; }
public double TotalAmount { get; set; }
public string Allocated { get; set; }
public string Despatched { get; set; }
public bool Printed { get; set; }
}
From the documentation you provided: 从您提供的文档:
Note that the MyObject property names A and B match the SQL parameter names @A and @B.
请注意,MyObject 属性名称 A和B与SQL参数名称@A和@B匹配。
Once you do that @OrderDate
can be mapped back to the model's property OrderDate
. 一旦你这样做,
@OrderDate
就可以映射回模型的属性 OrderDate
。
It is because you are not using query parameterization corretly. 这是因为您没有正确使用查询参数化。 Start from the following example:
从以下示例开始:
private static void UpdateDemographics(Int32 customerID,
string demoXml, string connectionString)
{
string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
+ "WHERE CustomerID = @ID;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
// You have to define the parameters, and give the input which it
// gets value from. This will be put into the query that the
// framework produces
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].Value = customerID;
// Use AddWithValue to assign Demographics.
// SQL Server will implicitly convert strings into XML.
command.Parameters.AddWithValue("@demographics", demoXml);
try
{
connection.Open();
Int32 rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.