下面的代码片段从我的表中提取了一个包大小的列,但是我使用了getString,我得到了一个错误,然后现在看到是一个问题,因为我拉的列是十进制类型。如何将我的代码段转换为接受小数值?
try
{
string connectionString = "Data Source=CMDLAP121\\SQLEXPRESS;Initial Catalog=TESTBAR;Integrated Security=True";
string query = "SELECT * FROM BCODEREF WHERE BAR_CODE = '" + comboBox1.Text + "';";
string mystring = query;
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string spo = dr.GetString(dr.GetOrdinal("PACKSIZE"));
ponum.Text = spo;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
答案 0 :(得分:1)
首先解析为十进制并转换为具有所需格式的字符串:
decimal packSize= 0;
decimal.TryParse(dr["PACKSIZE"].ToString(), out packSize);
ponum.Text = packSize.ToString("0.00"); //specify format you need
有关如何设置适当字符串格式的详细信息,请参阅此post