我正在尝试将本地数据库中的列显示到下拉列表中。问题是我需要拆分数据,以便它们不会全部显示在一行中。我用过“;”分离数据然后使用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);
}
答案 0 :(得分:2)
您只从表格中返回一列
没有理由在字段计数上使用for循环(它始终为1)
相反,您可以使用List(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());
答案 1 :(得分: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
语句的原因是为了确保DataReader
和Connection
中存在的非托管资源得到妥善处理。离开using
语句时,它将自动调用对象上的Dispose
。对于实现IDisposable
的对象,此语句仅用于。
答案 2 :(得分: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;
}