根据空数据表定义创建一行定义

问题描述:

执行DataAdapter并获得一个空的DataTable后。我需要根据空的数据表定义创建一个新行。请帮助这一点。根据空数据表定义创建一行定义

SqlConnection con = CreateCon(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    da.SelectCommand = new SqlCommand(); 
    da.SelectCommand.CommandType = CommandType.StoredProcedure; 
    da.SelectCommand.Connection = con; da.SelectCommand.CommandText = "spGET_RackZone"; 

    if (con.State == ConnectionState.Closed) 
    { 
    con.Open(); 
    } 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    if (con.State == ConnectionState.Open){ 
    con.Close(); 
    } 

DataTable dt = ds.Tables[0]; 

现在我想在datatable为空时创建一行。

+0

您可以使用DataRow对象创建它 – Koderzzzz

+1

我可以使用此DataRow newBlankRow1 = dataTable.NewRow()。但我需要根据表定义创建数据行。 –

+0

检查它是否为空然后添加新行if(dt.Rows.Count == 0){DataRow newRow = dataTable.NewRow(); //添加你的值}' – Lifewithsun

  DataTable table = new DataTable(); 
     if(table.Rows.Count==0) 
      { 
      DataRow row = table.NewRow(); 
      table.Rows.Add(row); 
      } 
     //if you already know the column of the table  
     table.Columns.Add("sl.No", typeof(int)); 
     table.Columns.Add("Name", typeof(string)); 

     // Here we add a DataRow. 
     table.Rows.Add(57, "Amin");