不要关闭SQL连接,直到读完为止

时间:2014-07-01 18:32:32

标签: c# sql

我有以下代码,它可以工作,但只是读取数据库的顶行然后终止。数组应该包含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();

4 个答案:

答案 0 :(得分:8)

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

循环显示结果

答案 1 :(得分:1)

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

如何使用ExecuteReader,您可以查看here

static void HasRows(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}

答案 2 :(得分:1)

conn1.Open();
string query = "select feedurl from [dbo].[feeds]";
DataSet DS = new DataSet();
SqlDataAdapter adapt = new SqlDataAdapter(query,conn1);
adapt.Fill(DS);
if (DS != null)
{
     if (DS.Tables[0].rows.Count > 0 )
    {
        foreach(DataRow DR in DS.Tables[0].Rows)
        {
            string temp = DR['columnname'];
        }
    }
{

答案 3 :(得分:0)

试试这个:

//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();

更多详情:http://support.microsoft.com/kb/314145