0
讓我先告訴你我現在正在做什麼以及我面臨什麼問題。如何使用列名數據表獲得數據行單元值
現在我使用SqlDataReader
用於獲取數據從數據庫中我的函數看起來像
public List<TOPIC_REPORT> gettopicreports()
{
query = "SELECT * FROM [Topic Reports]";
List<TOPIC_REPORT> rpl = new List<TOPIC_REPORT>();
try
{
con.Open();
com = new SqlCommand(query, con);
sdr = com.ExecuteReader();
while (sdr.Read())
{
rt = new TOPIC_REPORT();
rt.ContentId = sdr.GetString(0);
rt.TimesReported = sdr.GetInt64(1);
rt.IsBanned = sdr.GetInt32(2);
rpl.Add(rt);
}
con.Close();
return rpl;
}
catch (Exception e)
{
con.Close();
throw e;
}
}
與上面的代碼中存在的問題
- 需要由一個在賦值類變量和多問題選擇查詢。
- 需要照顧的ResultSet,while循環等
上述問題的解決方案使用的SqlDataAdapter。
public DataSet getdata()
{
com.CommandText = "GetMasterPageData";
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adapter.Fill(ds);
con.Close();
return ds;
}
這解決了上述問題,但
- 減少我的PROG可讀性,因爲我的前端的人不要在爲了我在DataSet中的DataTable發送知道。
- 不知道DataTable中選定值的順序。
- 前端人員需要處理最後的列索引,這是有問題的。
建議我該怎麼做才能解決上述問題。
您是說在第一個代碼塊中要返回多個結果集?您的查詢看起來好像只會返回一個。如果您想要返回多個結果集,它們是否都具有相同的列? –
我需要返回具有不同列的多個結果集。 –
@HotCoolStud請分享您的StoredProcedure –