2013-04-12 96 views
1

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

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

你真的应该从数据库获取数据,关闭您的连接,那么操纵它。 – Codeman

+0

你可以直接返回拆分字符串数组而不是将它们转换为字符串吗? – Tim

+0

@Tim nope,它不会让我 – user123

回答

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()); 
+0

嗯,你的代码是明确的,它是有道理的。但我想写“return Convert.ToString(topicNames);”在行结尾? – user123

+0

这取决于调用代码的期望以及代码是否可以更改。列表(字符串)是比数组更好的方法,但有些情况下数组是预期的,并且您无法更改。 – Steve

+0

它不会让我只是做“返回topicNames”或“返回topicNames.ToArray() – user123

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的对象,此声明仅为,仅用于

+0

感谢您的时间和帮助。其他答案比较清楚 – user123

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; 

}

+0

你能否解释一下使用“使用(conn)”和“使用(cmd)”的请 – user123

+0

@ user123肯定。使用(conn)会在连接超出范围时自动处理连接。这意味着你不必显式地调用conn.Close()。从本质上讲,在最后用conn.Dispose()来包装它。 – Codeman

+0

好的,谢谢你的帮助。但另一篇文章略显清晰。再次感谢:)真的很感激它 – user123