关于getvalue,getstring的问题
现在写了段代码,但是现在运行的时候总是在这一块出问题,现在又修改成了如下状况,但是在”DateMonth = dr.GetString(3);“这句错误是:“无法将System.Double强制转换为System.String"
,我该怎么改呢?
以下是我的代码:
private void getGoods()
{
DateMonth=textBox1.Text;
SqlConnection cn = new SqlConnection(Properties.Settings.Default.CSConnectionString);
string sql = "SELECT ID,DateMonth,SUM(GoodsAmount)AS GoodsAmount,SUM(Money) AS Money FROM GoodsOrder2 WHERE DateMonth like '%" + DateMonth + "%' GROUP BY ID,DateMonth ORDER BY Money";
SqlCommand com = new SqlCommand(sql, cn);
cn.Open();
SqlDataReader dr = com.ExecuteReader();
dr.Read();
ID = dr.GetString(1);
DateMonth = dr.GetString(3);
GoodsAmount = dr.GetInt32(1);
Money = dr.GetFloat(2);//括号里的1,2,3我是按照在datagridview中显示出来的列额的顺序写的,不知道对不对,求指点
cn.Close();
}
ID,DateMonth是public string,GoodsAmount是public int;Money是public float;
然后表GoodsOrder里的列是:
OrderID nvarchar
ID (同上)
GoodsName (同上)
UnitPrice float
GoodsAmount int
Money float
DateMonth nvarchar
------解决方案--------------------DateMonth = dr.GetString(2);才对啊。你这个查询语句
SELECT ID,DateMonth,SUM(GoodsAmount)AS GoodsAmount,SUM(Money) AS Money FROM GoodsOrder2 WHERE DateMonth like '%" + DateMonth + "%' GROUP BY ID,DateMonth ORDER BY Money"; DateMonth字段在第二位啊。