SQL Server 2012升级:使用ExecuteReader()的StackOverflowException

时间:2013-07-18 20:50:07

标签: c# sql-server ado.net sql-server-2012 stack-overflow

已解决:这不是SQL Server的问题,而是一个不断调用自身的递归函数。 /捂脸

我目前正在运行SQL Server 2005并使用下面的代码。它只是从SQL Server获取日期和时间,它完美地工作。

protected internal static string GetDateAndTime()
{
string strDate = null;
string strSQL = null;
// connect to the database and execute our SQL command
strSQL = "SELECT GETDATE() AS THEDATE";
using (SqlConnection sqlConn = new SqlConnection(Constants.CONNECTION_STRING))
{
       using (SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn))
       {
              sqlConn.Open();
              using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())
              {
                     if (sqlReader.HasRows)
                         {
                         while (sqlReader.Read())
                         {
                                // assign query returned row values
                                strDate = sqlReader["THEDATE"].ToString();
                         }
                     }
              }
        }
}
}

升级到SQL Server 2012,现在出现错误。 (惊喜!)代码停止在此行运行。

using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())

错误是:

-StackOverflowException was unhandled
-An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll
-Make sure you do not have an infinite loop or infinite recursion.

请注意,除了将连接字符串指向新的SQL Server 2012数据库之外,我没有更改任何代码。

请求信息,堆栈跟踪:

[External Code] 
     MyProject.exe!MyProject.Database.GetDateAndTime() Line 173 C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.frmSwitchboard.VerifyUserExists() Line 230 + 0x9 bytes  C#
MyProject.exe!MyProject.frmSwitchboard.MyProjectPreLoad() Line 136 + 0xd bytes  C#
MyProject.exe!MyProject.frmSwitchboard.frmSwitchboard_Load(object sender = {MyProject.frmSwitchboard}, System.EventArgs e = {System.EventArgs}) Line 68 C#
[External Code] 
MyProject.exe!MyProject.Program.Main() Line 18  C#
[External Code] 

请求的信息,Constants.CONNECTION_STRING: *尝试使用两者......

1. protected internal static string DATABASE_CONNECTION_STRING = @"Data Source=DBSTEST;Initial Catalog=restored 02-04-2013;User ID=sa; UID=sa;PWD=blah;Persist Security Info=True";
2. protected internal static string DATABASE_CONNECTION_STRING = @"Data Source=DBSTEST;Initial Catalog=restored 02-04-2013;User ID=sa;UID=sa;PWD=blah;Connect Timeout=300";

REQUESTED INFO,函数VerifyUserExists():

private DataTable VerifyUserExists()
{
        string strSQL = null;
        DataTable dt = null;
            strSQL = "SELECT DISTINCT [Employees].EmployeeID FROM [Employees] WHERE [Employees].Logon = 'User1'";
            dt = new DataTable();
            dt = Database.RunQueryAndReturnAsDataTable(strSQL);
}

REQUESTED INFO,RunQueryAndReturnAsDataTable():

protected internal static DataTable RunQueryAndReturnAsDataTable(string strSQL)
    {
        DataTable dt = null;
            // put our query into a datatable and return it
            using (SqlConnection sqlConn = new SqlConnection(Constants.DATABASE_CONNECTION_STRING))
            {
                using (SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn))
                {
                    sqlConn.Open();
                    using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())
                    {
                        if (sqlReader.HasRows)
                        {
                            dt = new DataTable();
                            dt.Load(sqlReader, LoadOption.OverwriteChanges);
                        }
                    }
                }
            }
    }

当我使用代码时代码停止在同一行运行,产生相同的错误。

using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())

有没有人遇到过这个问题?我怎样才能解决这个问题?请指教,谢谢!

1 个答案:

答案 0 :(得分:1)

你到那儿的路线有点不对劲。 stacktrace显示以下函数正在执行递归:

MyProject.Database.GetDateAndTime

它在没有中介的情况下调用自己(假设你在调试模式下运行,没有函数可以因内联而消失)。

如果您发布其代码,我会看看。也许,你现在可以解决这个问题。