Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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# Don';在完成读取之前,不要关闭SQL连接_C#_Sql - Fatal编程技术网

C# Don';在完成读取之前,不要关闭SQL连接

C# Don';在完成读取之前,不要关闭SQL连接,c#,sql,C#,Sql,我有下面的代码,它可以工作,但是只读取数据库的顶行,然后终止。数组应该包含3条数据,但它只包含一条 我认为这是因为它不是循环 你怎么说让代码继续运行直到没有更多的数据可以读取 SqlConnection conn1 = new SqlConnection(ssConnectionString); conn1.Open(); SqlCommand command1 = conn1.CreateCommand(); command1.CommandText = "SELECT FeedURL FR

我有下面的代码,它可以工作,但是只读取数据库的顶行,然后终止。数组应该包含3条数据,但它只包含一条

我认为这是因为它不是循环

你怎么说让代码继续运行直到没有更多的数据可以读取

SqlConnection conn1 = new SqlConnection(ssConnectionString);
conn1.Open();

SqlCommand command1 = conn1.CreateCommand();
command1.CommandText = "SELECT FeedURL FROM [dbo].[Feeds]";

rssFeeds.Add(command1.ExecuteScalar());

conn1.Close();

默认情况下,
ExecuteScalar()
将只返回一个值。您需要创建一个
DataReader
,然后使用
command1循环遍历结果。默认情况下
ExecuteScalar()
将只返回一个值。您需要创建一个
DataReader
,然后使用
command1.ExecuteReader()

循环查看结果。您可以使用ExecuteReader解决您的问题。在这个示例中,我从MSDN获取的是使用using语句的连接,因为SqlConnection类有一些非托管资源。如果您对使用和终结器还有更多问题,请检查

如何使用ExecuteReader,您可以检查:


您可以使用ExecuteReader解决您的问题。在这个示例中,我从MSDN获取的是使用using语句的连接,因为SqlConnection类有一些非托管资源。如果您对使用和终结器还有更多问题,请检查

如何使用ExecuteReader,您可以检查:

试试这个:

//method to make this code reusable
//the returned data set is accessible even if the underlying connection is closed
//NB: that means data's held in memory; so beware of your resource usage
    public DataSet ExecuteSQLToDataSet(string ssConnectionString, string query, string name)
    {
        DataSet ds = new DataSet("Tables");
        using (SqlConnection conn1 = new SqlConnection(ssConnectionString))
        {
            conn1.Open();
            SqlDataAdapter sda = new SqlDataAdapter(query, objConn);
            sda.FillSchema(ds, SchemaType.Source, name);
            sda.Fill(ds, name);
        } //using statement will close and dispose the connection for you
        return ds;
    }


//example usage

    DataSet ds = ExecuteSQLToDataSet(ssConnectionString, "SELECT FeedURL FROM [dbo].[Feeds]", "Feeds"); //nb: name doesn't have to match table name; you can call it what you like; naming is useful if you wanted to add other result sets to the same data set

    //DataTable tblFeeds = ds.Tables["Feeds"]; //if you want to access the above query by name

    foreach (DataTable tbl in ds.Tables)
    {
        foreach (DataRow dr in tbl.Rows) //tblFeeds.Rows if you did that instead of looping through all tables
        {
            //Console.WriteLine(dr["FeedURL"].ToString()); //you can specify a named column
            Console.WriteLine(dr[0].ToString()); //or just use the index
        }
    }
    Console.WriteLine("Done");
    Console.ReadLine();
更多详细信息:

尝试以下操作:

//method to make this code reusable
//the returned data set is accessible even if the underlying connection is closed
//NB: that means data's held in memory; so beware of your resource usage
    public DataSet ExecuteSQLToDataSet(string ssConnectionString, string query, string name)
    {
        DataSet ds = new DataSet("Tables");
        using (SqlConnection conn1 = new SqlConnection(ssConnectionString))
        {
            conn1.Open();
            SqlDataAdapter sda = new SqlDataAdapter(query, objConn);
            sda.FillSchema(ds, SchemaType.Source, name);
            sda.Fill(ds, name);
        } //using statement will close and dispose the connection for you
        return ds;
    }


//example usage

    DataSet ds = ExecuteSQLToDataSet(ssConnectionString, "SELECT FeedURL FROM [dbo].[Feeds]", "Feeds"); //nb: name doesn't have to match table name; you can call it what you like; naming is useful if you wanted to add other result sets to the same data set

    //DataTable tblFeeds = ds.Tables["Feeds"]; //if you want to access the above query by name

    foreach (DataTable tbl in ds.Tables)
    {
        foreach (DataRow dr in tbl.Rows) //tblFeeds.Rows if you did that instead of looping through all tables
        {
            //Console.WriteLine(dr["FeedURL"].ToString()); //you can specify a named column
            Console.WriteLine(dr[0].ToString()); //or just use the index
        }
    }
    Console.WriteLine("Done");
    Console.ReadLine();

更多详细信息:

您需要
ExecuteReader()
和循环。如果您有更多记录,则需要在
循环时将rssFeeds或datareader包装在
中。什么
ExecuteScalar
返回一个值或
null
T.S
仔细阅读注释。。SLaks和我所说的完全一样。。请不要混淆问题“rsfeed”是什么?您应该使用“Using”结构并加载数据集或数据表。您需要
ExecuteReader()
和循环。如果您有更多记录,则需要将rssFeeds或datareader包装在
中,而
循环什么
ExecuteScalar
返回一个值或
null
T.S
仔细阅读注释。。SLaks和我所说的完全一样。。请不要混淆问题“rsfeed”是什么?您应该使用“Using”结构并加载一个数据集或数据表。这也适用于Op,前提是他理解最重要的读者。HasRows在遍历其余结果集之前先检查。。好例子+1如果Op了解最重要的读者,这也适用于Op。在遍历结果集的其余部分之前,请先检查行。。好例子+1同样,完成后,您可能希望使用adapt.dispose()和DS.dispose()处理SqlDataAdapter和Dataset。完成后,您可能希望使用adapt.dispose()和DS.dispose()处理SqlDataAdapter和Dataset
//method to make this code reusable
//the returned data set is accessible even if the underlying connection is closed
//NB: that means data's held in memory; so beware of your resource usage
    public DataSet ExecuteSQLToDataSet(string ssConnectionString, string query, string name)
    {
        DataSet ds = new DataSet("Tables");
        using (SqlConnection conn1 = new SqlConnection(ssConnectionString))
        {
            conn1.Open();
            SqlDataAdapter sda = new SqlDataAdapter(query, objConn);
            sda.FillSchema(ds, SchemaType.Source, name);
            sda.Fill(ds, name);
        } //using statement will close and dispose the connection for you
        return ds;
    }


//example usage

    DataSet ds = ExecuteSQLToDataSet(ssConnectionString, "SELECT FeedURL FROM [dbo].[Feeds]", "Feeds"); //nb: name doesn't have to match table name; you can call it what you like; naming is useful if you wanted to add other result sets to the same data set

    //DataTable tblFeeds = ds.Tables["Feeds"]; //if you want to access the above query by name

    foreach (DataTable tbl in ds.Tables)
    {
        foreach (DataRow dr in tbl.Rows) //tblFeeds.Rows if you did that instead of looping through all tables
        {
            //Console.WriteLine(dr["FeedURL"].ToString()); //you can specify a named column
            Console.WriteLine(dr[0].ToString()); //or just use the index
        }
    }
    Console.WriteLine("Done");
    Console.ReadLine();