C# 在ASP.NET中拆分数据

C# 在ASP.NET中拆分数据,c#,asp.net,database,drop-down-menu,split,C#,Asp.net,Database,Drop Down Menu,Split,我试图将本地数据库中的一列显示在下拉列表中。问题是,我需要分割数据,以便它们不会全部显示在一行中。我使用“;”分隔数据,然后使用split(;”)方法拆分它们。我试过下面写的代码,但它不起作用。任何帮助都将不胜感激 public string DisplayTopicNames() { string topicNames = ""; // declare the connection string string database = "Provider=Microso

我试图将本地数据库中的一列显示在下拉列表中。问题是,我需要分割数据,以便它们不会全部显示在一行中。我使用“;”分隔数据,然后使用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);
}
公共字符串DisplayTopicNames()
{
字符串topicNames=“”;
//声明连接字符串
string database=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |/Forum.accdb;Persist Security Info=True”;
//初始化连接
OleDbConnection myConn=新的OleDbConnection(数据库);
//质疑
string queryStr=“从主题中选择主题名称”;
//创建命令对象
OleDbCommand myCommand=新的OleDbCommand(queryStr,myConn);
//打开连接
myCommand.Connection.Open();
//执行命令
OleDbDataReader myDataReader=myCommand.ExecuteReader();
//提取结果
while(myDataReader.Read())
{
对于(int i=0;i
您只返回表中的一列。
没有理由在字段计数上使用for循环(它始终为1)
相反,您可以使用
列表(字符串)
保存找到的行返回的值。
然后返回此列表以用作DropDownList的数据源

List<string> topicNames = new List<string>();
// Extract the results 
while (myDataReader.Read())
{
    topicNames.Add(myDataReader.GetValue(0).ToString();
}
....
return topicNames;
如果您希望返回字符串数组,那么将列表转换为数组就是一个问题

return topicNames.ToArray();
编辑
当然,返回数组或列表(字符串)需要更改方法的返回值

 public List<string> DisplayTopicNames()
 {
     ......
 }
如果您仍然希望返回以分号分隔的字符串,那么可以用这种方式更改return语句

 return string.Join(";", topicNames.ToArra());

除非我已经失去理智,否则像这样的事情应该行得通:

while (myDataReader.Read())
{
    for (int i = 0; i < myDataReader.FieldCount; i++)
        ddl.Items.Add(myDataReader.GetValue(i))
}
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;
但是,最重要的是,我想为您重新构造一点代码,因为您需要利用
之类的东西使用

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 "";
}
公共字符串DisplayTopicNames()
{
字符串topicNames=“”;
//声明连接字符串
string database=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |/Forum.accdb;Persist Security Info=True”;
//初始化连接
使用(OleDbConnection myConn=新OleDbConnection(数据库))
{
myConn.Open();
//创建命令对象
OleDbCommand myCommand=新的OleDbCommand(“从主题中选择主题名称”,myConn);
//执行命令
使用(OleDbDataReader myDataReader=myCommand.ExecuteReader())
{
//提取结果
while(myDataReader.Read())
{
对于(int i=0;i

您希望利用
using
语句的原因是为了确保
DataReader
连接中存在的非托管资源得到正确处置。当离开
using
语句时,它将自动调用对象上的
Dispose
。此语句仅用于实现
IDisposable

的对象,我认为这应该可以:

while (myDataReader.Read())
{
    for (int i = 0; i < myDataReader.FieldCount; i++)
        ddl.Items.Add(myDataReader.GetValue(i))
}
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;
public List DisplayTopicNames()
{
列表主题=新列表();
//初始化连接
OleDbConnection conn=新的OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |/Forum.accdb;Persist Security Info=True”);
OleDbCommand cmd=新的OleDbCommand(“从主题中选择主题名称”);
使用(康涅狄格州)
使用(cmd)
{
cmd.Connection.Open();
//执行命令
使用(OleDbDataReader myDataReader=cmd.ExecuteReader())
{
//提取结果
while(myDataReader.Read())
{
topics.Add(myDataReader.GetValue(0.ToString());
}
}
}
返回主题;

}

你真的应该从数据库中获取数据,关闭连接,然后对其进行操作。你能不能只返回拆分字符串数组,而不是将其转换为字符串?@Tim nope,它不会让我知道,你的代码很清楚,很有意义。但是我是否应该在行尾写“return Convert.ToString(topicNames);”?这取决于调用代码的期望以及该代码是否可以更改。列表(字符串)是一种比数组更好的方法,但在某些情况下,数组是预期的,您无法更改。它不允许我只执行“返回topicNames”或“返回topicNames.ToArray()”操作。它给我一个错误“无法隐式将类型“System.Collections.Generic.List”转换为“String”“@user123,您必须将方法的返回类型更改为
字符串[]
。这里的问题是,您正在读取已经分离的数据,然后将其与
串在一起,然后在方法末尾再次拆分该字符串。这是毫无意义的。如果您的
DropDownList
在这里可用,请按照我所说的那样内联添加到它,如果不可用,则返回
列表
字符串[]
并绑定到调用代码中。您能解释一下“using(conn)”和“using(cmd)”的用法吗please@user123当然当连接超出范围时,使用(conn)会自动处理连接。这意味着你不需要
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;