如何从紧凑数据库中读取列

发布于 2024-10-18 10:03:02 字数 454 浏览 1 评论 0原文

我正在使用 C# 2010 Express 和 Sql Compact。我有一个名为“记录”的表和名为“名称”的列,我想在列表框中列出这些名称。

我编写了该代码,但最后一行是“ExecuteReader:连接属性尚未初始化。”例外。

  SqlCeConnection Baglan = new SqlCeConnection("Data Source=|DataDirectory|CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True");

        Baglan.Open();

    SqlCeCommand BarlariAl = new SqlCeCommand("SELECT Names FROM Barlar");

    SqlCeDataReader BarlariOku = BarlariAl.ExecuteReader();

I'm using C# 2010 Express and Sql Compact. I have a table named as "Records" and column named as "Names" I want to list that names in a listbox.

I wrote that code but last line is thorws "ExecuteReader: Connection property has not been initialized." exception.

  SqlCeConnection Baglan = new SqlCeConnection("Data Source=|DataDirectory|CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True");

        Baglan.Open();

    SqlCeCommand BarlariAl = new SqlCeCommand("SELECT Names FROM Barlar");

    SqlCeDataReader BarlariOku = BarlariAl.ExecuteReader();

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

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

发布评论

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

评论(2

冷月断魂刀 2024-10-25 10:03:02

至于接下来要写什么,假设有一个名为 listbox 的列表框(给定变量名称的大胆假设),您可以这样写:

while(BarlariOku.Read())
    listbox.Items.Add(BarlariOku["Names"]);

顺便说一句,您没有正确处理对象。它应该看起来像这样:

using(var conn = new SqlCeConnection("Data Source=|DataDirectory|CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True"))
{
    conn.Open();

    var comm = new SqlCeCommand("SELECT Names FROM Barlar", conn);
    SqlCeDataReader reader = comm.ExecuteReader();

    while(reader.Read())
        listbox.Items.Add(reader["Names"]);
}

As to what to write next, assuming there's a list box named listbox (bold assumption given your variable names), you'd write:

while(BarlariOku.Read())
    listbox.Items.Add(BarlariOku["Names"]);

As an aside, you're not disposing your objects properly. It should look like this:

using(var conn = new SqlCeConnection("Data Source=|DataDirectory|CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True"))
{
    conn.Open();

    var comm = new SqlCeCommand("SELECT Names FROM Barlar", conn);
    SqlCeDataReader reader = comm.ExecuteReader();

    while(reader.Read())
        listbox.Items.Add(reader["Names"]);
}
明月夜 2024-10-25 10:03:02

您没有将连接与命令关联起来。尝试下面的代码。

SqlCeCommand BarlariAl = new SqlCeCommand("SELECT Names FROM Barlar", Baglan);

要列出列,请尝试从 MSDN(添加右大括号)。

string query = "SELECT [Order ID], [Customer] FROM Orders";
SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand(query, conn);

conn.Open();
SqlCeDataReader rdr = cmd.ExecuteReader();

try
{
    // Iterate through the results
    //
    while (rdr.Read())
    {
        int val1 = rdr.GetInt32(0);
        string val2 = rdr.GetString(1);
    }
}
finally
{
    // Always call Close when done reading
    //
    rdr.Close();

    // Always call Close when done reading
    //
    conn.Close();
}

You're not associating the connection with the command. Try the code below.

SqlCeCommand BarlariAl = new SqlCeCommand("SELECT Names FROM Barlar", Baglan);

For listing the columns, try this code borrowed from MSDN (with closing braces added).

string query = "SELECT [Order ID], [Customer] FROM Orders";
SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand(query, conn);

conn.Open();
SqlCeDataReader rdr = cmd.ExecuteReader();

try
{
    // Iterate through the results
    //
    while (rdr.Read())
    {
        int val1 = rdr.GetInt32(0);
        string val2 = rdr.GetString(1);
    }
}
finally
{
    // Always call Close when done reading
    //
    rdr.Close();

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