我正在运行C# .NET代码,需要测试在网页表单中输入的特定值,然后将其传递给服务器。然后,服务器在查询字符串中使用这些值来返回值。我将这些查询指令封装在try-catch块中,以便跟踪任何Sql异常。
问题是这样的:一旦应用程序启动、连接字符串设置完毕并运行查询,我就不会从catch块的SQL异常中获得堆栈跟踪,而只是在运行查询的方法中获得一个空白/空值。该方法将返回一个布尔变量,以指示是否从查询中读取了任何值,如果有,则返回true,但它总是返回false,这不应该发生,因为我已经检查了它构建的查询字符串,方法是将其粘贴到MS SQL 2008的查询控制台并运行它。运行粘贴的SQL指令测试的结果确实会从查询中生成非空数据。非常感谢你的帮助。
我正在使用IIS6.0和MS SQL2008 Enterprise Studio运行VS2003
下面是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;
}发布于 2012-07-24 23:10:53
这应该是可行的,但我认为您应该对单结果查询使用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);
}发布于 2012-07-24 23:20:47
我会尽量避免任何不适用于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();
}
}您永远不会想要编写希望完全格式的代码作为参数的方法。
https://stackoverflow.com/questions/11633746
复制相似问题