C# 根据空数据表定义创建一行
执行DataAdapter后,获取一个空DataTable。我需要根据空数据表定义创建一个新行。请在这一点上给予帮助C# 根据空数据表定义创建一行,c#,sql-server-2012,ado.net,C#,Sql Server 2012,Ado.net,执行DataAdapter后,获取一个空DataTable。我需要根据空数据表定义创建一个新行。请在这一点上给予帮助 SqlConnection con = CreateCon(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(); da.SelectCommand.CommandType = CommandType.StoredProcedure
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为空时创建一行。您可以使用DataRow对象创建它。我可以使用这个DataRow NewLankRow1=datatable.NewRow()。但是我需要根据表定义创建datarow。检查它是否为空,然后添加新行
if(dt.Rows.Count==0){datarow newRow=dataTable.newRow();//添加值}
您可以使用datarow对象创建它。我可以使用这个datarow NewLankRow1=dataTable.newRow()。但是我需要根据表定义创建datarow。检查它是否为空,然后添加新行if(dt.Rows.Count==0){datarow newRow=dataTable.newRow();//添加值}
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");