2017-07-27 68 views
0

執行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

回答

1
  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"); 
相關問題