1

我正在尝试将本地数据库中的列显示到下拉列表中。问题是我需要拆分数据,以便它们不会全部显示在一行中。我用过“;” 分离数据,然后使用 split(";") 方法拆分它们。我已经尝试了我在下面编写的代码,但它不起作用。任何帮助将不胜感激。

public string DisplayTopicNames()
{
    string topicNames = "";

    // declare the connection string 
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True";

    // Initialise the connection 
    OleDbConnection myConn = new OleDbConnection(database);
    //Query
    string queryStr = "SELECT TopicName FROM Topics";
    // Create a command object 
    OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
    // Open the connection 
    myCommand.Connection.Open();
    // Execute the command 
    OleDbDataReader myDataReader = myCommand.ExecuteReader();

    // Extract the results 
    while (myDataReader.Read())
    {
        for (int i = 0; i < myDataReader.FieldCount; i++)
            topicNames += myDataReader.GetValue(i) + " ";
        topicNames += ";";
    }

    //Because the topicNames are seperated by a semicolon, I would have to split it using the split()
    string[] splittedTopicNames = topicNames.Split(';');
    // close the connection 
    myCommand.Connection.Close();

    return Convert.ToString(splittedTopicNames);
}
4

3 回答 3

2

您只返回表中的一列。
没有理由对字段计数使用 for 循环(它始终为 1)
相反,您可以使用 aList(Of String)来保存找到的行返回的值。
然后返回此列表以用作 DropDownList 的数据源

List<string> topicNames = new List<string>();
// Extract the results 
while (myDataReader.Read())
{
    topicNames.Add(myDataReader.GetValue(0).ToString();
}
....
return topicNames;

但是,不清楚该字段是否TopicName包含由分号分隔的自身字符串。
在这种情况下,您可以编写:

List<string> topicNames = new List<string>();
// Extract the results 
while (myDataReader.Read())
{
    string[] topics = myDataReader.GetValue(0).ToString().Split(';')
    topicNames.AddRange(topics);
}
...
return topicNames;

如果您更喜欢返回一个字符串数组,那么只需将列表转换为数组即可

return topicNames.ToArray();

编辑
当然返回数组或 List(Of String) 需要更改方法的返回值

 public List<string> DisplayTopicNames()
 {
     ......
 }

或者

 public string[] DisplayTopicNames()
 {
     ......
 }

如果您仍然喜欢返回用分号分隔的字符串,那么以这种方式更改 return 语句

 return string.Join(";", topicNames.ToArra());
于 2013-04-12T20:06:50.173 回答
0

除非我失去理智,否则这样的事情应该有效:

while (myDataReader.Read())
{
    for (int i = 0; i < myDataReader.FieldCount; i++)
        ddl.Items.Add(myDataReader.GetValue(i))
}

ddl你的名字在哪里DropDownList。如果您ddl在此处不可用,请将它们添加到List<string>集合中并返回。然后这段代码现在可能变得无关紧要:

//Because the topicNames are seperated by a semicolon, I would have to split it using the split()
string[] splittedTopicNames = topicNames.Split(';');
// close the connection 
myCommand.Connection.Close();

return Convert.ToString(splittedTopicNames);

但是,除此之外,我想为您稍微重构一下代码,因为您需要利用using.

public string DisplayTopicNames()
{
    string topicNames = "";

    // declare the connection string 
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True";

    // Initialise the connection 
    using (OleDbConnection myConn = new OleDbConnection(database))
    {
        myConn.Open();

        // Create a command object 
        OleDbCommand myCommand = new OleDbCommand("SELECT TopicName FROM Topics", myConn);

        // Execute the command 
        using (OleDbDataReader myDataReader = myCommand.ExecuteReader())
        {
            // Extract the results 
            while (myDataReader.Read())
            {
                for (int i = 0; i < myDataReader.FieldCount; i++)
                {
                    ddl.Items.Add(myDataReader.GetValue(i));
                }
            }
        }
    }

    // not sure anything needs returned here anymore
    // but you'll have to evaluate that
    return "";
}

您想要利用该using语句的原因是确保存在于 中的非托管资源DataReaderConnection得到正确处理。离开using语句时,它将自动调用Dispose对象。此语句仅用于实现IDisposable.

于 2013-04-12T20:08:58.297 回答
0

我认为这应该有效:

public List<string> DisplayTopicNames()
{
    List<string> topics = new List<string>();

    // Initialise the connection 
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True");
    OleDbCommand cmd = new OleDbCommand("SELECT TopicName FROM Topics");
    using(conn)
    using(cmd)
    {
        cmd.Connection.Open();
        // Execute the command 
        using(OleDbDataReader myDataReader = cmd.ExecuteReader())
        {
            // Extract the results 
            while(myDataReader.Read())
            {
            topics.Add(myDataReader.GetValue(0).ToString());
        }
    }
}

return topics;

}

于 2013-04-12T20:09:46.970 回答