使用 SqlDataReader 填充 ArrayList?

发布于 2024-08-18 05:21:30 字数 1344 浏览 2 评论 0原文

我正在尝试实现一种方法,该方法将采用给定的连接字符串并返回包含 SQL 视图内容的 ArrayList。

我已经验证了连接字符串和视图本身的有效性。但是我不明白下面的代码中有什么问题。在调试中,当它运行 ExecuteReader 方法,然后尝试进入 while 循环以迭代视图中的记录时,它会立即退出,因为由于某种原因 sqlReader.Read() 不会。

    public ArrayList GetEligibles(string sConnectionString)
    {
        string sSQLCommand = "SELECT field1, field2 FROM ViewEligible";

        ArrayList alEligible = new ArrayList();

        using (SqlConnection sConn = new SqlConnection(sConnectionString))
        {
            // Open connection.
            sConn.Open();

            // Define the command.
            SqlCommand sCmd = new SqlCommand(sSQLCommand, sConn);

            // Execute the reader.
            SqlDataReader sqlReader = sCmd.ExecuteReader(CommandBehavior.CloseConnection);

            // Loop through data reader to add items to the array.
            while (sqlReader.Read()) 
            {
                EligibleClass Person = new EligibleClass();

                Person.field1 = sqlReader["field1"].ToString();
                Person.field2 = sqlReader["field2"].ToString();

                alEligible.Add(Person);
            }

            // Call Close when done reading.
            sqlReader.Close();
        }

        return alEligible;
    }

请注意,EligibleClass 只是代表视图结果的一行的类对象。

I'm trying to implement a method which will take a given connection string and return an ArrayList containing the contents of a SQL view.

I've verified the validity of the connection string and the view itself. However I don't see what the problem is in the code below. In debug, when it runs the ExecuteReader method and then try to enter the while loop to iterate through the records in the view, it immediately bails because for some reason sqlReader.Read() doesn't.

    public ArrayList GetEligibles(string sConnectionString)
    {
        string sSQLCommand = "SELECT field1, field2 FROM ViewEligible";

        ArrayList alEligible = new ArrayList();

        using (SqlConnection sConn = new SqlConnection(sConnectionString))
        {
            // Open connection.
            sConn.Open();

            // Define the command.
            SqlCommand sCmd = new SqlCommand(sSQLCommand, sConn);

            // Execute the reader.
            SqlDataReader sqlReader = sCmd.ExecuteReader(CommandBehavior.CloseConnection);

            // Loop through data reader to add items to the array.
            while (sqlReader.Read()) 
            {
                EligibleClass Person = new EligibleClass();

                Person.field1 = sqlReader["field1"].ToString();
                Person.field2 = sqlReader["field2"].ToString();

                alEligible.Add(Person);
            }

            // Call Close when done reading.
            sqlReader.Close();
        }

        return alEligible;
    }

Note, EligibleClass is just a class object representing one row of the view's results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-08-25 05:21:30

我要检查的几件事:

  1. 连接字符串是否正常
  2. 连接字符串中的用户是否有权访问数据库/视图
  3. 您能否从您所在的电脑访问该数据库
  4. ViewEligable 视图是否存在
  5. 该视图是否包含 field1 和 field2柱子。

这里有一种方法可以稍微清理该代码(假设您有.net 2.0)

    public List<EligibleClass> GetEligibles(string sConnectionString)
    {
        List<EligibleClass> alEligible = null;

        try
        {
            using (SqlConnection sConn = new SqlConnection(sConnectionString))
            {
                sConn.Open();
                using (SqlCommand sCmd = new SqlCommand())
                {
                    sCmd.Connection = sConn;
                    sCmd.CommandText = "SELECT field1, field2 FROM ViewEligible";

                    using (SqlDataReader sqlReader = sCmd.ExecuteReader())
                    {
                        while (sqlReader.Read())
                        {
                            EligibleClass Person = new EligibleClass();

                            Person.field1 = sqlReader.GetString(0);
                            Person.field2 = sqlReader.GetString(1);

                            if (alEligible == null) alEligible = new List<EligibleClass>();
                            alEligible.Add(Person);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
           // do something.
        }

        return alEligible;
    }

A couple of things I would check:

  1. Is the connection string ok
  2. Does the user in your connection string have access to the database/view
  3. Can you access that database from the pc your at
  4. Does the ViewEligable view exist
  5. Does the view contain a field1 and field2 column.

Here one way you could possibly clean up that code somewhat (assuming you have .net 2.0)

    public List<EligibleClass> GetEligibles(string sConnectionString)
    {
        List<EligibleClass> alEligible = null;

        try
        {
            using (SqlConnection sConn = new SqlConnection(sConnectionString))
            {
                sConn.Open();
                using (SqlCommand sCmd = new SqlCommand())
                {
                    sCmd.Connection = sConn;
                    sCmd.CommandText = "SELECT field1, field2 FROM ViewEligible";

                    using (SqlDataReader sqlReader = sCmd.ExecuteReader())
                    {
                        while (sqlReader.Read())
                        {
                            EligibleClass Person = new EligibleClass();

                            Person.field1 = sqlReader.GetString(0);
                            Person.field2 = sqlReader.GetString(1);

                            if (alEligible == null) alEligible = new List<EligibleClass>();
                            alEligible.Add(Person);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
           // do something.
        }

        return alEligible;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文