0
我試圖將查詢中的記錄保存到DataRow
中,但沒有成功。我可能需要一些幫助。這是我的代碼:無法將類型爲'System.Data.DataColumn'的對象轉換爲鍵入'System.IConvertible'
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);
}
是的它的SqlDataReader –
當我嘗試你的代碼有錯誤,編譯器顯示我(ApplicationID)! –
當你嘗試哪些代碼?你應該使用'DataAdapter.Fill(DataTable)'的第一種方法。 –