我正在获取一个记录对象列表中的数据并将其内容放入数据库中:
// Processes a Record and adds it to the database public bool addRecord(sqlConnection db,List<Record> recordsToAdd) { using (sqlCommand command = db.CreateCommand()) { foreach (Record record in recordsToAdd) { // Set the query command text command.CommandText = @"INSERT INTO SMDGROUP_STPRODMASTER (PRODCODE,TOTFREE,TOTPHYS,ITEMTYPE,PRODESC) VALUES ('@PRODCODE','@TOTFREE','@TOTPHYS','@ITEMTYPE','@PRODESC')"; sqlParameter param1 = new sqlParameter("@CURSTAT",record.curstat); sqlParameter param2 = new sqlParameter("@ITEMDESC",record.itemdesc); sqlParameter param3 = new sqlParameter("@PRODCODE",record.prodcode); sqlParameter param4 = new sqlParameter("@TOTFREE",record.totfree); sqlParameter param5 = new sqlParameter("@TOTPHYS",record.totphys); sqlParameter param6 = new sqlParameter("@ITEMTYPE",record.itemtype); sqlParameter param7 = new sqlParameter("@PRODESC",record.proddesc); command.Parameters.Add(param1); command.Parameters.Add(param2); command.Parameters.Add(param3); command.Parameters.Add(param4); command.Parameters.Add(param5); command.Parameters.Add(param6); command.Parameters.Add(param7); // Execute the query command.ExecuteNonQuery(); } return true; } }
这是我的记录类:
class Record { public string curstat { get; set; } public string itemtype { get; set; } public string itemdesc { get; set; } public string prodcode { get; set; } public string proddesc { get; set; } public string totfree { get; set; } public string totphys { get; set; } }
但其次,我甚至不确定我是否正确地完成了@PARAMETER值的替换.
另外,我在command.ExecuteNonQuery()上得到这个错误:
String or binary data would be truncated.
The statement has been terminated.
所以我的问题是: