我有一個ASP.NET網站(運行v2.0)和一個SQL Server數據庫(2012),當很多人打開頁面時,出現一個奇怪的錯誤。錯誤是:爲什麼我的SQL連接不能返回請求的列?
Column 'product_code' does not belong to table . // occurs on line 'if (!row.IsNull("product_code") && !row.IsNull("state"))' below
這似乎表明有某種連接共享線程之間的事,但我似乎無法找出原因。在後面的代碼我有:
private Dictionary<string, string> _saleStates = null;
protected Dictionary<string, string> SalesStates {
get {
if (_saleStates == null)
{
_saleStates = new Dictionary<string, string>();
string sql = @"SELECT [product_code], [state] FROM [jsb_store_items]";
using (DataTable tbl = new DataTable())
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Shop.DbConnection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{ adapter.Fill(tbl); }
}
}
if (tbl.Rows.Count > 0)
{
foreach (DataRow row in tbl.Rows)
{
if (!row.IsNull("product_code") && !row.IsNull("state")) // Error thrown here
{ _saleStates.Add(row["product_code"].ToString().ToLower(), salesStateToText(row["state"].ToString().ToLower())); }
}
}
}
}
return _saleStates;
}
}
protected string getSalesStates() {
string output = "";
int i = 0;
foreach (KeyValuePair<string, string> entry in SalesStates) {
output += ((i!=0) ? "," : "") + "\"" + entry.Key + "\":\"" + entry.Value + "\"";
i++;
}
output = "{" + output + "}";
return output;
}
,並在標記我:
<script type="text/javascript">var sales_states = <% Response.Write(this.getSalesStates()); %>;</script>
如果有人可以幫助我弄清楚爲什麼發生這種情況我會很感激。
請提供'jsb_store_items'的模式 –
看來您的jsb_store_items表沒有名爲product_code的列...請仔細檢查。 – GendoIkari
@GendoIkari - 我希望錯誤發生在'adapter.Fill(tbl);'然後。 – Igor