日期:2014-05-16  浏览次数:20953 次

字段名怎么得到?代码写了部分不知道怎么写了。。。。谢谢

        public List<string> title(string tableName)
        {            
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.;Initial Catalog=Apriori;Integrated Security=True";
            con.Open();
            SqlDataAdapter ad = new SqlDataAdapter();
            string sql = "select name from syscolumns where id=object_id('"+tableName+"')";
            SqlCommand com = new SqlCommand(sql, con);
            ad.SelectCommand = com;
            DataSet ds = new DataSet();
            ad.Fill(ds);
            /*不知道怎么写了想返回字段名的数组*/
            return names;
        }

------解决方案--------------------
List<string> result = new List<string>();
foreach (var row in ds.Tables[0].Rows)
{
    result.Add(row["name"].ToString());
}
return result;
------解决方案--------------------
 var names = new List<string>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                names.Add(row[0].ToString());
            }
            return names;