Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# sql跳过读取器_C#_Sql - Fatal编程技术网

C# sql跳过读取器

C# sql跳过读取器,c#,sql,C#,Sql,您好,我正在尝试运行一个方法,该方法将返回账单列表。但是,该方法失败,应用程序进入中断模式 调试后,此行billDetail.InvoiceTotal=reader.GetInt64(0)后出现问题已读取。我没有得到任何异常,下一步将在不返回任何内容的情况下进入finally块。我有点不明白为什么会这样 我已经测试了这个查询,它运行良好,并返回它应该做的事情 我的代码: public List<BillDetails> getBillDetails(long id, long cl

您好,我正在尝试运行一个方法,该方法将返回账单列表。但是,该方法失败,应用程序进入中断模式

调试后,此行
billDetail.InvoiceTotal=reader.GetInt64(0)后出现问题已读取。我没有得到任何异常,下一步将在不返回任何内容的情况下进入finally块。我有点不明白为什么会这样

我已经测试了这个查询,它运行良好,并返回它应该做的事情

我的代码:

 public List<BillDetails> getBillDetails(long id, long clientid)
    {
        string SQLServerToUse = "blah blah";
        string SQLServerDB = "amaya";
        string SQLServerPWD = "pass";
        string SQLServerUser = "sa";

        //set the connection string
        string connString = "Data Source=" + SQLServerToUse + ";Initial Catalog=" + SQLServerDB + ";User ID=" + SQLServerUser + ";Password=" + SQLServerPWD + ";";

        SqlConnection connection = new SqlConnection(connString);

        connection.Open();

        List<BillDetails> bills = new List<BillDetails>();

        try
        {
            string query = @"SELECT InvoiceTotal, Tax, Transcount, Services, ORC
                            FROM history 
                            WHERE id = @id";

            using (SqlCommand cmd = new SqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@id", id);

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var billDetail = new BillDetails();
                        billDetail.InvoiceTotal = reader.GetInt64(0);
                        billDetail.Tax = reader.GetDouble(1);
                        billDetail.Transcount = reader.GetInt32(2);
                        billDetail.Services = reader.GetDouble(3);
                        billDetail.ORC = reader.GetDouble(4);
                        bills.Add(billDetail);
                    }

                    reader.Close();
                }
            }

            return bills;
        }

        catch (SqlException ex) // This will catch all SQL exceptions
        {
            string query = @"INSERT INTO Amaya_Errors(CustID, TimeStamp, ErrorMessage) 
                             VALUES(@CustID, @TimeStamp, @ErrorMessage)";

            using (SqlCommand cmd = new SqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@CustID", clientid);
                cmd.Parameters.AddWithValue("@TimeStamp", DateTime.Now);
                cmd.Parameters.AddWithValue("@ErrorMessage", ex.Message);

                cmd.ExecuteNonQuery();
            }
            return null;
        }
        catch (InvalidOperationException ex) // This will catch SqlConnection Exception
        {
            string query = @"INSERT INTO Amaya_Errors(CustID, TimeStamp, ErrorMessage) 
                             VALUES(@CustID, @TimeStamp, @ErrorMessage)";

            using (SqlCommand cmd = new SqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@CustID", clientid);
                cmd.Parameters.AddWithValue("@TimeStamp", DateTime.Now);
                cmd.Parameters.AddWithValue("@ErrorMessage", ex.Message);

                cmd.ExecuteNonQuery();
            }
            return null;
        }
        finally
        {
            connection.Close();
        }
    }
公共列表getBillDetails(长id、长clientid)
{
字符串SQLServerToUse=“诸如此类”;
字符串SQLServerDB=“amaya”;
字符串SQLServerPWD=“pass”;
字符串SQLServerUser=“sa”;
//设置连接字符串
string connString=“数据源=“+SQLServerToUse+”;初始目录=“+SQLServerDB+”;用户ID=“+SQLServerUser+”;密码=“+SQLServerPWD+”;”;
SqlConnection连接=新的SqlConnection(connString);
connection.Open();
列表账单=新列表();
尝试
{
字符串查询=@“选择发票总额、税务、转帐、服务、ORC
来自历史
其中id=@id”;
使用(SqlCommand cmd=newsqlcommand(查询、连接))
{
cmd.Parameters.AddWithValue(“@id”,id);
使用(SqlDataReader=cmd.ExecuteReader())
{
while(reader.Read())
{
var billDetail=新BillDetails();
billDetail.InvoiceTotal=reader.GetInt64(0);
billDetail.Tax=reader.GetDouble(1);
billDetail.Transcount=reader.GetInt32(2);
billDetail.Services=reader.GetDouble(3);
billDetail.ORC=reader.GetDouble(4);
账单。添加(账单明细);
}
reader.Close();
}
}
退票;
}
catch(SqlException ex)//这将捕获所有SQL异常
{
字符串查询=@“插入Amaya_错误(CustID、TimeStamp、ErrorMessage)
值(@CustID、@TimeStamp、@ErrorMessage)”;
使用(SqlCommand cmd=newsqlcommand(查询、连接))
{
cmd.Parameters.AddWithValue(“@CustID”,clientid);
cmd.Parameters.AddWithValue(“@TimeStamp”,DateTime.Now);
cmd.Parameters.AddWithValue(“@ErrorMessage”,例如Message);
cmd.ExecuteNonQuery();
}
返回null;
}
catch(invalidoOperationException ex)//这将捕获SqlConnection异常
{
字符串查询=@“插入Amaya_错误(CustID、TimeStamp、ErrorMessage)
值(@CustID、@TimeStamp、@ErrorMessage)”;
使用(SqlCommand cmd=newsqlcommand(查询、连接))
{
cmd.Parameters.AddWithValue(“@CustID”,clientid);
cmd.Parameters.AddWithValue(“@TimeStamp”,DateTime.Now);
cmd.Parameters.AddWithValue(“@ErrorMessage”,例如Message);
cmd.ExecuteNonQuery();
}
返回null;
}
最后
{
connection.Close();
}
}

看起来抛出的异常不是
SqlException
invalidooperationexception
类型

因此,您的程序流程如下所示:

  • catch
    块不会执行,因为抛出的异常不是
    SqlException
    invalidooperationexception
  • 最后执行
    。始终执行
    最终
  • 因此,您需要捕获异常

    try
    {    
        // the other code is omitted for the brevity
    }
    catch (SqlException ex) // This will catch only SqlException
    {   
    }
    catch (InvalidOperationException ex) // This will catch only InvalidOperationException 
    {         
    }
    catch (Exception ex) // This will catch all exceptions
    {            
    }
    finally
    {
         connection.Close();
    }
    

    您抛出的异常类型似乎不是
    SqlException
    invalidooperationexception

    因此,您的程序流程如下所示:

  • catch
    块不会执行,因为抛出的异常不是
    SqlException
    invalidooperationexception
  • 最后执行
    。始终执行
    最终
  • 因此,您需要捕获异常

    try
    {    
        // the other code is omitted for the brevity
    }
    catch (SqlException ex) // This will catch only SqlException
    {   
    }
    catch (InvalidOperationException ex) // This will catch only InvalidOperationException 
    {         
    }
    catch (Exception ex) // This will catch all exceptions
    {            
    }
    finally
    {
         connection.Close();
    }
    

    你有没有试过突破这些界限,使用“快速观察”来查看对这些陈述的评估?这将向您显示任何错误。另外,我认为在catch块中写入数据库不是一个好主意。您已经在每次复制代码了。您的方法应返回一个确定其是否成功的值,然后在读取该值后调用您的日志记录方法。请检查billDetail类中InvoiceTotal的数据类型。我认为它不匹配,应为该类型long@LeeWillis很好。我将实施更改。您是否尝试过打断这些行并使用“快速观察”查看对语句的评估?这将向您显示任何错误。另外,我认为在catch块中写入数据库不是一个好主意。您已经在每次复制代码了。您的方法应返回一个确定其是否成功的值,然后在读取该值后调用您的日志记录方法。请检查billDetail类中InvoiceTotal的数据类型。我认为它不匹配,应为该类型long@LeeWillis很好。我将实施变革。