尽管SqlDbType设置,sqlitecontion_dbtype总是Int32
本文关键字:dbtype 总是 Int32 sqlitecontion SqlDbType 设置 尽管 | 更新日期: 2023-09-27 18:07:06
我有一个代码块,在这里准备一个查询:
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);
作为参考,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; }
}
当我运行这段代码时,我总是会遇到一般错误:
输入字符串格式不正确。
在试图找出这个问题的来源之后,假设我在这个过程中输入了一些错误的东西,我遇到了我所有的参数变量将它们的dbType
设置为Int32
的问题。我以为我是设置参数SqlDbType.Text
,但我一定是误解了这一点?
如何将输入的dbtype设置为Text而不是Int32?
我所犯的错误是使用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 };