如何从sql表中获取100列以上的数据并将其使用数据适配器加载到C#数据表中 [英] how to get more than 100 columns of data from sql table and load it to a C# datatable using data adapter

查看:120
本文介绍了如何从sql表中获取100列以上的数据并将其使用数据适配器加载到C#数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有这个并且正在工作,但是我想读取整个表,并且它具有不同的数据类型



i have this and is working but i want to read the whole table and it has different data types

while (rd.Read())
{
     if (!rd.IsDBNull(0))
   {
        ID = Convert.ToInt32(rd.GetValue(0));
    }
     if (!rd.IsDBNull(1))
     {
        Update_UserID = rd.GetString(1);
  }
     if (!rd.IsDBNull(2))
    {
       Review_UserID = rd.GetString(2);
     }
     if (!rd.IsDBNull(3))
     {
        Compile_UserID = rd.GetString(3);
     }
     if (!rd.IsDBNull(4))
   {
        Month_Name = rd.GetString(4);
    }
     if (!rd.IsDBNull(5))
    {
       Business_Case = rd.GetString(5);
    }
    if (!rd.IsDBNull(6))
    {
         BC_Description = rd.GetString(6);
      }
   if (!rd.IsDBNull(7))
    {
         Spend_Plan_YTD = Convert.ToDecimal(rd.GetValue(7));
     }
     if (!rd.IsDBNull(8))
    {
        Actual_YTD_Gross = Convert.ToDecimal(rd.GetValue(8));
    }
      if (!rd.IsDBNull(9))
     {
         Variance_Value = Convert.ToDecimal(rd.GetValue(9));
     }
      if (!rd.IsDBNull(10))
    {
         Update_Comments = rd.GetString(10);
      }
     if (!rd.IsDBNull(11))
      {
         Updated_By = rd.GetString(11);
      }
      if (!rd.IsDBNull(12))
    {
        Updated_On = rd.GetDateTime(12);
      }
    rd.Fill(Bc);
 }
  return Bc;

推荐答案

you need to get DataTable from Database? follow below steps 
//create SqlConnection
SqlConnection conn = new SqlConnection(yourconnectionString);
//open the connection 
conn.Open();
string query = "SELECT * FROM [YourTable]";
//create SqlCommand 
SqlCommand cmd = new SqlCommand(query, conn);
DataTable dt = new DataTable();
//Load the data to DataTable 
dt.Load(cmd.ExecuteReader());
//now you have all the data in database in dt DataTable


为什么不将其保存在数据表中作为字符串列,如果以后必须在显示之前做一些操作,只需要在显示之前转换相关列即可
Why dont you save it in the data table as string columns and if you have to do some manipulation later before displaying just convert the relevant columns before displaying


这篇关于如何从sql表中获取100列以上的数据并将其使用数据适配器加载到C#数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆