c# – Dapper使用list插入数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Dapper使用list插入数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3250字,纯文字阅读大概需要5分钟。
内容图文
![c# – Dapper使用list插入数据库](/upload/InfoBanner/zyjiaocheng/907/98bc7fa724b14d0cb5c4448014f83f77.jpg)
我正在使用dapper,我正在尝试使用以下教程将列表插入数据库.
我首先从这个例子中想到,这意味着@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);
}
我的销售订单类如下,您可以看到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;
}
但是从截图中可以看出,这是我收到的消息:
编辑2
好的:由于有助于提高我对此的了解,我已经更进了一步.现在的结构是:
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; }
}
我的导出方法如下:
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);
}
我的sql表如下,但现在我得到以下内容:
System.Data.SqlClient.SqlException: ‘Error converting data type nvarchar to numeric.’
所以这里的问题是它仍然无法将数据发送到SQL表.
解决方法:
这是因为您在模型中使用的是fields而不是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; }
}
从您提供的文档:
Note that the MyObject property names A and B match the SQL parameter names @A and @B.
一旦你这样做,@ OrderDate可以映射回模型的property OrderDate.
内容总结
以上是互联网集市为您收集整理的c# – Dapper使用list插入数据库全部内容,希望文章能够帮你解决c# – Dapper使用list插入数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。