SQL Server 2008 的 .NET 异常
此代码在 Windows Server 2008 R2 和 SQL Server 2008 R2 Express 中的 IIS.7 上运行 ASP.NET。
这是一个简单的函数,它根据 3 个参数从数据库中进行选择并返回包含所选行的对象。
此代码在大多数情况下运行正常,但在某些情况下,我会收到一个名为 serial_number
的异常,它是数据库表中的列名称。
这是完全的例外:
[IndexOutOfRangeException:序列号]
System.Data.ProviderBase.FieldNameLookup.GetOrdinal(字符串字段名称)+2674398
System.Data.SqlClient.SqlDataReader.GetOrdinal(字符串名称)+249
System.Data.SqlClient.SqlDataReader.get_Item(字符串名称) +23
PhoneSerialNumber.GetByPhoneNumber(字符串电话号码,字符串国家,字符串应用程序)+423
UpdateMeClass.GenereteVersionTag(字符串版本,字符串电话号码,字符串应用程序名称)+534
c:\inetpub\wwwroot\me\Handlers\UpdateMe.ashx:63 中的 UpdateMe.ProcessRequest(HttpContext context) System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,Boolean&completedSynchronously)+171
虽然我记录了异常情况下的 3 个参数并且它们不为空,但请注意,当我在同一服务器中使用相同参数模拟相同请求时,运行正常。
我认为在异常的情况下,查询运行良好,但是当通过此异常获取列的值时,我可能是错的。
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["meConnectionString"].ConnectionString);
connection.Open();
SqlCommand cmd;
using (connection)
{
cmd = new SqlCommand("select * from serialnumber_table join countries on country_id=countryid join applications on ApplicationID=App_ID where phone_number= @phoneNumber and country_name=@Country and app_name=@app", connection);
cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
cmd.Parameters.AddWithValue("@Country", Country);
cmd.Parameters.AddWithValue("@app", app);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
sn = new PhoneSerialNumber();
sn.SerialNumber = rdr["serial_number"].ToString();
sn.PhoneNumber = rdr["phone_Number"].ToString();
sn.PhoneLang = rdr["Lang"].ToString();
sn.PhoneModel = rdr["ModelName"].ToString();
sn.ApplicationVersion = rdr["App_Version"].ToString();
sn.DealerCode = rdr["dealer_code"].ToString();
sn.Size = rdr["size"].ToString();
TimeSpan Time = DateTime.Parse(rdr["Renewal_Date"].ToString()) - new DateTime(1970, 1, 1, 0, 0, 0);
sn._renewal_Date = (long)(Time.TotalMilliseconds);
rdr.Dispose();
connection.Dispose();
return sn;
}
}
sn = new PhoneSerialNumber();
sn.SerialNumber = null;
sn.PhoneNumber = null;
sn.PhoneModel = null;
sn.PhoneLang = null;
sn.ApplicationVersion = null;
sn.DealerCode = null;
connection.Dispose();
return sn;
}
This code runs ASP.NET on IIS.7 in windows server 2008 R2 and SQL Server 2008 R2 Express.
This is a simple function which selects from the database based on 3 parameters and returns object containing the selected row.
This code runs normally in most time, but in some cases i get an exception called serial_number
which is a column name in the database table.
This is the complete exception:
[IndexOutOfRangeException: serial_number]
System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) +2674398
System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) +249
System.Data.SqlClient.SqlDataReader.get_Item(String name) +23
PhoneSerialNumber.GetByPhoneNumber(String phoneNumber, String Country, String app) +423
UpdateMeClass.GenereteVersionTag(String Version, String PhoneNumber, String appName) +534
UpdateMe.ProcessRequest(HttpContext context) in c:\inetpub\wwwroot\me\Handlers\UpdateMe.ashx:63
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+599
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
Although I logged the 3 parameters in case of the exception and they are not null, note that when I simulate the same request with the same parameters in the same server, it runs normally.
I think in the case of the exception the query runs fine but when getting values of the columns it through this exception, I might be wrong.
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["meConnectionString"].ConnectionString);
connection.Open();
SqlCommand cmd;
using (connection)
{
cmd = new SqlCommand("select * from serialnumber_table join countries on country_id=countryid join applications on ApplicationID=App_ID where phone_number= @phoneNumber and country_name=@Country and app_name=@app", connection);
cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
cmd.Parameters.AddWithValue("@Country", Country);
cmd.Parameters.AddWithValue("@app", app);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
sn = new PhoneSerialNumber();
sn.SerialNumber = rdr["serial_number"].ToString();
sn.PhoneNumber = rdr["phone_Number"].ToString();
sn.PhoneLang = rdr["Lang"].ToString();
sn.PhoneModel = rdr["ModelName"].ToString();
sn.ApplicationVersion = rdr["App_Version"].ToString();
sn.DealerCode = rdr["dealer_code"].ToString();
sn.Size = rdr["size"].ToString();
TimeSpan Time = DateTime.Parse(rdr["Renewal_Date"].ToString()) - new DateTime(1970, 1, 1, 0, 0, 0);
sn._renewal_Date = (long)(Time.TotalMilliseconds);
rdr.Dispose();
connection.Dispose();
return sn;
}
}
sn = new PhoneSerialNumber();
sn.SerialNumber = null;
sn.PhoneNumber = null;
sn.PhoneModel = null;
sn.PhoneLang = null;
sn.ApplicationVersion = null;
sn.DealerCode = null;
connection.Dispose();
return sn;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
唯一想到的是,由于某种原因,当您运行查询时,不会返回
serial_number
列。不要执行SELECT *
,而是尝试对您要返回的每个单独列进行选择。您的序列号列是否在任何其他表中重复?有任何消息来源的观点吗?如果
serial_number
来自视图,并且您更改了基础表,则可能需要重新构建它才能显示视图的正确列。我唯一一次看到列名异常是当指定的列在当前数据读取器中不存在时,就像它没有首先被选择或根本不存在一样
The only thing that comes to mind, is that for some reason when you run your query, the
serial_number
column is not being returned. Rather than doing aSELECT *
, try running doing a select on each individual columns that you're trying to return.Is your serial number column duplicated across any of the other tables? Are any of the sources views?! If the
serial_number
comes from a view and you've changed an underlying table, it could need to be rebuilt in order to display the proper columns for the view.the only time I've ever seen the exception as the column name is when the column specified doesn't exist in the current data reader, like it wasn't selected in the first place or simply didn't exist
这只是一个猜测,但也有可能您将 rdr 处理了两次。一次使用 using 语句,该语句将转换为 try/finally 块,并在 return 语句之前显式使用您的 PhoneSerialNumber。当您处置已经处置的内容时,.NET 会引发异常。由于没有发布异常,我只是在这里猜测。
This is just a guess, but it's also possible you're disposing rdr twice. Once with the using statement, which translates into a try/finally block, and once explicitly before the return statement with your PhoneSerialNumber. .NET throws exceptions when you dispose something that's already disposing. Since no exception was posted, I'm just guessing here.
鉴于您只提供了“serial_number”作为异常原因(它不可能是异常本身),我相信罪魁祸首是这一行:
该值很可能为空。
如果问题是该列实际上不存在,我会询问该表是否是动态生成的,并且有人没有正确完成他们的工作。
Given that you've only provided "serial_number" as the cause of the exception (it can't possibly be the exception itself), I believe the culprit line is this one:
Chances are really good that this value is null.
If the problem is that the column doesn't actually exist, I'd ask if this table was being generated dynamically, and someone isn't doing their job right.
很有可能,serial_number 包含一个 null,它返回为 DBNull.Value。尝试使用 rdr["serial_number"] 作为字符串而不是 rdr["serial_number"].ToString()。
另外,避免使用 SELECT *。它返回的数据多于所需的数据,这会增加网络流量,并且 SELECT 中的命名列会产生更好的错误消息。
Odds are good that serial_number contains a null which comes back as DBNull.Value. Try using rdr["serial_number"] as string instead of rdr["serial_number"].ToString().
Also, avoid using SELECT *. It returns more data than necessary which increases network traffic and named columns in your SELECT produce better error messages.
TimeSpan 时间“时间是一个关键字,使用更有意义的变量名称,您是否使用选择查询中的所有字段..?如果没有,请不要执行选择 *
TimeSpan Time "Time is a KEY WORD use more meaningful variable names also are you using all the fields from the Select query..? if not do not do Select *