我正在使用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 OK:由于帮助我提高了对这一点的了解,我已经迈出了一步。现在的结构是:
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:“将数据类型nvarchar转换为数字时出错。”
所以这里的问题是它仍然无法将数据发送到SQL表。
答案 0 :(得分:3)
这是因为您在模型中使用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; }
}
根据您提供的文档:
请注意,MyObject 属性名称 A和B与SQL参数名称@A和@B匹配。
完成此操作后,@OrderDate
可以映射回模型的property OrderDate
。
答案 1 :(得分:1)
这是因为您没有正确使用查询参数化。从以下示例开始:
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);
}
}
}
您可以阅读有关此主题的信息here