我正在运行 C# .NET 代码,我需要测试已在网页表单中输入然后传递到服务器的某些值。然后,服务器在查询字符串中使用这些值来返回一个值。我将这些查询指令封装在一个 try-catch 块中,以便跟踪任何 Sql 异常。
问题是这样的:一旦应用程序启动,连接字符串被设置,查询运行,我没有从 catch 块的 SQL 异常中得到堆栈跟踪,而是在方法中得到一个空白/空值运行查询。该方法将返回一个布尔变量以指示是否从查询中读取了任何值,如果是,则返回 true 但是它总是返回 false 这不应该发生,因为我已经检查了它通过将其粘贴到 MS SQL 2008 中构建的查询字符串查询控制台并运行它。运行粘贴的 SQL 指令测试的结果确实会从查询中生成非空数据。非常感谢您的帮助。
我正在使用 IIS 6.0 运行 VS2003 并使用 MS SQL 2008 Enterprise Studio
这是 web.config 和 C# 代码的代码段。非常感谢您的帮助。:
<system.web>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation about
debugging ASP.NET files.
-->
<compilation defaultLanguage="c#" debug="true" />
=====================
//This method takes one string and returns either country name or Canadian state as a string, according to query.
private string candStateCountryCheck(string strQuery)
{
string strStateCountry = "";
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader sdr = null;
try
{
string strConnection = ConfigurationSettings.AppSettings["OLEDBConnectionString"];
con = new SqlConnection(strConnection);
con.Open();
cmd = new SqlCommand(strQuery, con);
sdr = cmd.ExecuteReader();
if(sdr.Read())
{
strStateCountry = sdr.GetString(0);
}
}
catch (Exception exc)
{
ErrorLabel.Text = "ERROR:" + exc.Message;
}
finally
{
if (sdr != null)
sdr.Close();
if (cmd != null)
cmd.Dispose();
if (con != null)
con.Close();
}
return strStateCountry;
}