2012-07-24 52 views
0

我正在运行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; 
    } 
+0

如果您替换'sdr = cmd.ExecuteReader(); (sdr.Read()) { strStateCountry = sdr.GetString(0);(sdr.Read()) 'strStateCountry = cmd.ExecuteScalar();',会发生什么? – 2012-07-24 15:08:05

+0

尝试运行MS Sql配置文件,看看sql认为代码正在做什么。 – 2012-07-24 15:08:36

+2

将sql代码作为字符串接受而不提供参数的方法是一种邪恶的反模式。 – 2012-07-24 15:11:19

回答

0

这应该工作,但我认为你应该对单个结果查询使用ExecuteScaler。我也鼓励您使用参数化查询:

while (sdr.Read()) 
{ 
    strStateCountry = sdr.GetString(0); 
} 

ExucuteScaler例如:

try 
    { 
     string strConnection = ConfigurationSettings.AppSettings["OLEDBConnectionString"]; 
     using(SqlConnection con = new SqlConnection(strConnection)) 
     { 
      con.Open(); 
      using(SqlCommand cmd = new SqlCommand(strQuery, con)) 
      { 
       strStateCountry = (String)cmd.ExecuteScalar(); 
      } 
     } 

    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
+0

而不是如果,你要做一段时间才能多次设置相同的变量? – 2012-07-24 15:12:01

+0

查看更新...... – 2012-07-24 15:13:05

+0

谢谢我会试试这个。 – user1548875 2012-08-07 19:18:28

0

我会尽量避免任何这里不会与VS2003工作,但它会是硬... VS2003现在变得相当老了,你知道VS2010的快车版是免费的,对吧?

另外,如果使用的是虚构数据库,则这是对代码的重写,以显示更好的应为的样子。你没有分享任何你的数据库结构,任何示例数据,或者是什么的查询可能看起来像的,所以这是我们现在做的最好的:

private string candStateCountryCheck(string toTest) 
{ 
    string sql = "SELECT countryStateName FROM SomeTable WHERE ItemKey LIKE @Query + '%';"; 

    try 
    { 
     string strConnection = ConfigurationSettings.AppSettings["OLEDBConnectionString"]; 

     con = new SqlConnection(strConnection); 
     cmd = new SqlCommand(sql, con); 

     cmd.Parameters.Add("@Query", SqlDbType.VarChar, 50).Value = toTest; 

     con.Open(); 
     return cmd.ExecuteScalar().ToString(); 
    } 
    catch (Exception exc) 
    { 
     ErrorLabel.Text = "ERROR:" + exc.Message; 
    } 
    finally 
    { 
     cmd.Dispose(); 
     con.Dispose(); 
    } 
} 

NEVER想写期待充分方法形成的sql代码作为参数。

相关问题