我正在运行C#.NET代码,我需要测试某些已输入网页表单并传递给服务器的值。服务器然后在查询字符串中使用这些值 以返回值。我在try-catch块中包含这些查询指令以追踪任何Sql异常。
问题是这样的: 一旦应用程序启动,连接字符串被设置,并且运行查询,我没有从catch块的SQL Exception中得到堆栈跟踪,而是 我只是得到一个空白/空的运行查询的方法中的值。该方法将返回一个布尔变量来指示是否有任何从查询中读取的值,如果是,则返回true,但它总是返回false,这应该不会发生,因为我已通过将它粘贴到MS SQL 2008中来检查查询字符串查询控制台并运行它。运行粘贴的SQL指令测试的结果确实会从查询中生成非空数据。非常感谢您的帮助。数据库查询不会运行,但SQL异常也不会引发
我运行VS2003与IIS 6.0,并使用MS SQL 2008企业工作室
下面是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;
}
如果您替换'sdr = cmd.ExecuteReader(); (sdr.Read()) { strStateCountry = sdr.GetString(0);(sdr.Read()) 'strStateCountry = cmd.ExecuteScalar();',会发生什么? – 2012-07-24 15:08:05
尝试运行MS Sql配置文件,看看sql认为代码正在做什么。 – 2012-07-24 15:08:36
将sql代码作为字符串接受而不提供参数的方法是一种邪恶的反模式。 – 2012-07-24 15:11:19