【发布时间】:2015-12-01 20:41:14
【问题描述】:
我有一个 ASP.NET 网站(运行 v2.0)和一个 SQL Server 数据库(2012),当很多人点击该页面时,我遇到了一个奇怪的错误。错误是:
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 - 我预计错误会发生在
adapter.Fill(tbl);行上。 -
架构确实包含请求的列。否则我会在测试期间收到此错误,但我不会。测试服务器和生产服务器都使用相同的数据库服务器。此外,在生产环境中,只有一些页面加载会导致错误,但每个页面请求都会运行此代码。
-
我没有看到 conn.Open 语句。
标签: c# asp.net .net sql-server