我试图将查询中的记录保存到 中DataRow
,但没有成功。我可能需要一些帮助。这是我的代码:
private DataSet CreateDataSet()
{
DataSet ds = new DataSet();
// create Countries table and columns
DataTable dtApplications = ds.Tables.Add("Applications");
DataColumn dtApplicationID = dtApplications.Columns.Add("ApplicationID", typeof(int));
DataColumn dtApplicationName= dtApplications.Columns.Add("ApplicationName", typeof(string));
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(MoahannedConnectionString);
SqlCommand cmd1 = new SqlCommand("select AppID,AppName from Application", con);
SqlDataAdapter da = new SqlDataAdapter();
SqlDataReader sdr = null;
dt.TableName = "Application";
con.Open();
sdr = cmd1.ExecuteReader();
while (sdr.Read())
{
AddDataToTable(dtApplicationID, dtApplicationName, dtApplications, ds);
}
}
private void AddDataToTable(DataColumn ID, DataColumn Name, DataTable myTable,DataSet ds)
{
DataRow row;
row = myTable.NewRow();
row["ApplicationID"] = ID;
row["ApplicationName"] = Name;
myTable.Rows.Add(row);
}