当我尝试将数据库中项目表中的值读取到工具中时,我必须在编辑项目表单中阅读医药类型产品的数据信息作为textBoxes,日期时间选择器和数字上下异常出现在我的showInfo按钮单击事件中,即"转换varchar值时转换失败' System.Windows.Forms.TextBox,Text:'数据类型int"这些是我的代码
cmd = new SqlCommand("select itmBar,itmTrdName,itmScName,exdate,pill,tab,pack,piece,pillBP,tabBP,packBP,pieceBP,pillSP,tabSP,packSP,pieceSP,buyDflt,selDflt,packQuant,tabQuant,pillQuant,pieceQuant from item where itmTrdName='" + itemName + "' or itmBar='" + itemBarcode + "'", cn);
cn.Open();
dr = cmd.ExecuteReader();
dr.Read();
addItemBartxt.Text = dr["itmBar"].ToString();
addItemTrdNametxt.Text = dr["itmTrdName"].ToString();
addItemScNametxt.Text = dr["itmScName"].ToString();
addExDatedtp.Value = Convert.ToDateTime(dr["exdate"].ToString());
pillnum.Value = Convert.ToInt32(dr["pill"].ToString());
tabnum.Value = Convert.ToInt32(dr["tab"].ToString());
packnum.Value = Convert.ToInt32(dr["pack"].ToString());
piecenum.Value = Convert.ToInt32(dr["piece"].ToString());
pillBPtxt.Text = dr["pillBP"].ToString();
tabBPtxt.Text = dr["tabBP"].ToString();
packBPtxt.Text = dr["packBP"].ToString();
pieceBPtxt.Text = dr["pieceBP"].ToString();
pillSPtxt.Text = dr["pillSP"].ToString();
tabSPtxt.Text = dr["tabSP"].ToString();
packSPtxt.Text = dr["packSP"].ToString();
pieceSPtxt.Text = dr["pieceSP"].ToString();
buyDflt = dr["buyDflt"].ToString();
sellDflt = dr["selDflt"].ToString();
packQuanttxt.Text = dr["packQuant"].ToString();
tabQuanttxt.Text = dr["tabQuant"].ToString();
pillQuanttxt.Text = dr["pillQuant"].ToString();
pieceQuanttxt.Text = dr["pieceQuant"].ToString();
此代码中的问题是什么......问候
答案 0 :(得分:0)
我不会这样做。
请阅读此SqlDataReader Class以了解有关数据读者的所有信息。
如何使用数据读取器读取数据的一个很好的例子是:
using System;
using System.Data;
using System.Data.SqlClient;
class Program {
static void Main(string[] args) {
using (var connection = new SqlConnection(@"Data Source=(local);Initial Catalog=AdventureWorks2012;Integrated Security=SSPI")) {
var command = new SqlCommand("SELECT p.FirstName, p.MiddleName, p.LastName FROM HumanResources.Employee AS e" +
" JOIN Person.Person AS p ON e.BusinessEntityID = p.BusinessEntityID;", connection);
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read()) {
Console.Write(reader.GetString(reader.GetOrdinal("FirstName")));
// display middle name only of not null
if (!reader.IsDBNull(reader.GetOrdinal("MiddleName")))
Console.Write(" {0}", reader.GetString(reader.GetOrdinal("MiddleName")));
Console.WriteLine(" {0}", reader.GetString(reader.GetOrdinal("LastName")));
}
connection.Close();
}
}
}