如何将对象转换为列表<string> </string>

时间:2014-02-06 13:04:07

标签: c# sql-server

我不知道如何将object()转换为List<string>而我收到错误。

这有效,但我继续将List<string>作为null

     public List<string> DspStrg()
     {
        List<string> dspString = new List<string>();

        conn(); //opens connection to database
        StringBuilder dspStrgQuery = new StringBuilder();
        dspStrgQuery.Append("Select String From DisplayToJ");

        OdbcCommand commDspQuery = new OdbcCommand(displayStringQuery.ToString(), oConnection);
        OdbcDataReader RDisplay = commDspQuery.ExecuteReader();

        string[] d ;
        Object doh = new object();
        while(RDisplay.Read() )
        {
            try
            {
                doh = RDisplay[0];
               //-----------
               // convert object to List<string> here

            }
            catch(OdbcException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        RDisplay.Close();
        return dspString;

     }

1 个答案:

答案 0 :(得分:3)

您无需转换任何内容,只需将每个项目添加到集合

即可
List<string> dspString = new List<string>();
...
while(RDisplay.Read())
{
    try
    {
        dspString.Add(RDisplay.GetString(0));
    }
    catch(OdbcException ex)
    {
        MessageBox.Show(ex.Message);
    }
}