Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自SQL Server的系列_C#_Sql Server_Dotnethighcharts - Fatal编程技术网

C# 来自SQL Server的系列

C# 来自SQL Server的系列,c#,sql-server,dotnethighcharts,C#,Sql Server,Dotnethighcharts,我正在尝试将SQL Server数据库中特定列的值存储到列表中 我使用的是dotNet海图 下面是我的代码。它有一些问题 using (SqlConnection cnn = new SqlConnection("Data Source=INBDQ2WK2LBCD2S\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI")) { SqlDataAdapter da = new SqlDataAdapter("select t

我正在尝试将SQL Server数据库中特定列的值存储到
列表中

我使用的是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());
    }
}
使用(SqlConnection cnn=newsqlconnection(“数据源=INBDQ2WK2LBCD2S\\SQLEXPRESS;初始目录=MCAS;集成安全=SSPI”))
{
SqlDataAdapter da=新的SqlDataAdapter(“从表4中选择top(100)x,按Id描述排序”,cnn);
数据集ds=新数据集();
da.填写(ds,“表4”);
List xValues=新列表();
foreach(ds.Tables[“Table4”].Rows中的DataRow行)
{
添加(行[“x”].ToString());
}
}

我认为您应该在访问数据库数据之前打开连接,在访问数据之后关闭连接

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());
    }
}
使用(SqlConnection cnn=newsqlconnection(“数据源=INBDQ2WK2LBCD2S\\SQLEXPRESS;初始目录=MCAS;集成安全=SSPI”))
{
cnn.Open();
SqlDataAdapter da=新的SqlDataAdapter(“从表4中选择top(100)x,按Id描述排序”,cnn);
数据集ds=新数据集();
da.填写(ds,“表4”);
cnn.Close();
List xValues=新列表();
foreach(ds.Tables[“Table4”].Rows中的DataRow行)
{
添加(行[“x”].ToString());
}
}

检索数据的代码非常好。但是,将其添加到列表中时会出错

您想将数据存储在
列表中。现在我们不知道对象
Serie
是什么类型,也不知道它有什么属性。但您尝试将字符串添加到列表中

有两种方法可以解决此问题:

将列表类型更改为
list xValues=new list()

xValues.Add()
行更改为添加类型为
Serie
的有效对象的内容

//you need to check Serie constructors to see which properties need to passed.
xValues.Add(new Serie(row["x"]));
:“与SELECT语句关联的连接对象必须有效,但不需要打开。如果在调用Fill之前关闭了连接,则会打开该连接以检索数据,然后关闭”“它有一些问题。”不是很具体。您是否收到错误消息?如果是,请添加详细信息。是否加载的行数不超过预期的行数?
表4中的数据是什么样的?