尽管有SqlDbType设置,但SQLiteconnection dbType始终是Int32

时间:2016-09-14 22:30:35

标签: c# sql sqlite

我有一段代码在这里准备查询:

var AssetTagParam = new SQLiteParameter("@AssetTagParam", SqlDbType.Int) { Value = item.AssetTag };
var VendorParam = new SQLiteParameter("@VendorParam", SqlDbType.Text) { Value = item.Vendor };
var DeviceParam = new SQLiteParameter("@DeviceParam", SqlDbType.Text) { Value = item.Device };
var AttributeParam = new SQLiteParameter("@AttributeParam", SqlDbType.Text) { Value = item.Attribute };
var DeviceTypeParam = new SQLiteParameter("@DeviceTypeParam", SqlDbType.Text) { Value = item.DeviceType };
var SystemParam = new SQLiteParameter("@SystemParam", SqlDbType.Text) { Value = item.System };
var LocationParam = new SQLiteParameter("@LocationParam", SqlDbType.Text) { Value = item.Location };
var OnLoanParam = new SQLiteParameter("@OnLoanParam", SqlDbType.Binary) { Value = item.OnLoan };
var NotesParam = new SQLiteParameter("@NotesParam", SqlDbType.Text) { Value = item.Notes };
var LastModifiedTimeParam = new SQLiteParameter("@LastModifiedTimeParam", SqlDbType.Text) { Value = item.LastModifiedTime };
var LastModifiedPersonParam = new SQLiteParameter("@LastModifiedPersonParam", SqlDbType.Text) { Value = item.LastModifiedPerson };
var IsDeletedParam = new SQLiteParameter("@IsDeletedParam", SqlDbType.Binary) { Value = item.IsDeleted };

SQLiteCommand insertSQL = new SQLiteCommand("INSERT INTO Inventory(AssetTag, Vendor, Device, Attribute, DeviceType, System, Notes, OnLoan, Location)" +
" VALUES (@AssetTagParam, @VendorParam, @DeviceParam, @AttributeParam, @DeviceTypeParam, @SystemParam, @NotesParam, @OnLoanParam, @LocationParam)", conn);

insertSQL.Parameters.Add(AssetTagParam);
insertSQL.Parameters.Add(VendorParam);
insertSQL.Parameters.Add(DeviceParam);
insertSQL.Parameters.Add(AttributeParam);
insertSQL.Parameters.Add(DeviceTypeParam);
insertSQL.Parameters.Add(SystemParam);
insertSQL.Parameters.Add(LocationParam);
insertSQL.Parameters.Add(OnLoanParam);
insertSQL.Parameters.Add(NotesParam);
insertSQL.Parameters.Add(LastModifiedTimeParam);
insertSQL.Parameters.Add(LastModifiedPersonParam);
insertSQL.Parameters.Add(IsDeletedParam);

对于Reference,Item类看起来像这样:

public class Item
{
    public int AssetID { get; set; }
    public int AssetTag { get; set; }
    public string Vendor { get; set; }
    public string Device { get; set; }
    public string Attribute { get; set; }
    public string DeviceType { get; set; }
    public string System { get; set; }
    public string Location { get; set; }
    public bool OnLoan { get; set; }
    public string Notes { get; set; }
    public string LastModifiedTime { get; set; }
    public string LastModifiedPerson { get; set; }
    public bool IsDeleted { get; set; }
}

当我运行此代码时,我将始终遇到一般错误:

  

输入字符串的格式不正确。

在尝试找出此问题的根源后,假设我在此过程中键入了错误的内容,我遇到的问题是我的所有 Param vars都将dbType设置为Int32。我以为我使用参数SqlDbType.Text进行设置,但我一定是误解了这个?

如何将输入的dbtype设置为Text而不是Int32?

1 个答案:

答案 0 :(得分:0)

我犯的错误是使用SqlDbType来定义数据类型。我应该一直使用System.Data.DbType来定义数据,所以这个:

var VendorParam = new SQLiteParameter("@VendorParam", SqlDbType.Text) { Value = item.Vendor };

应改为:

var VendorParam = new SQLiteParameter("@VendorParam", System.Data.DbType.String) { Value = item.Vendor };