我试图将特定列的值从Server数据库存储到List<Serie>
我使用的是dotNet高级图表。
下面是我的密码。它有一些问题。
using (SqlConnection cnn = new SqlConnection("Data Source=INBDQ2WK2LBCD2S\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI"))
{
SqlDataAdapter da = new SqlDataAdapter("select top(100) x from Table4 order by Id desc", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Table4");
List<Serie> xValues = new List<Serie>();
foreach (DataRow row in ds.Tables["Table4"].Rows)
{
xValues.Add(row["x"].ToString());
}
}
发布于 2014-01-27 09:02:20
检索数据的代码非常好。但是,当将它添加到列表中时,会出现错误。
您希望将数据存储在List<Serie>
中。现在我们不知道Serie
是什么类型的对象,也不知道它具有什么属性。但是您尝试在列表中添加一个字符串。
解决这一问题的方法有两种:
将列表类型更改为List<String> xValues = new List<String>();
或
将xValues.Add()
行更改为添加Serie
类型的有效对象的内容。
//you need to check Serie constructors to see which properties need to passed.
xValues.Add(new Serie(row["x"]));
发布于 2013-11-22 10:02:50
我认为您在访问DB数据之前打开连接,在访问数据之后关闭。
using (SqlConnection cnn = new SqlConnection("Data Source=INBDQ2WK2LBCD2S\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI"))
{
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter("select top(100) x from Table4 order by Id desc", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Table4");
cnn.Close();
List<String> xValues = new List<String>();
foreach (DataRow row in ds.Tables["Table4"].Rows)
{
xValues.Add(row["x"].ToString());
}
}
https://stackoverflow.com/questions/20138326
复制相似问题