0
        SqlConnection sqlConn = new SqlConnection(MyClass.GlobalConn());
        sqlConn.Open();

        try
        {
            string getAllBranch = "SELECT iBranch_num,LTRIM(RTRIM(sConstant)) FROM tblgobranch";
            SqlCommand cmdgetAllBranch = new SqlCommand(getAllBranch, sqlConn);
            SqlDataReader dRgetAllBranch=cmdgetAllBranch.ExecuteReader();



            while (dRgetAllBranch.Read())
            {

                SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", sqlConn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1;
                cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2;
                cmd.Parameters.Add("@BRANCH", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString();
                cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString();
                cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1;
                cmd.ExecuteNonQuery();

            }

我在这里出错了

“已打开与此命令关联的数据读取器”

请帮我解决一下这个。

4

4 回答 4

2

SqlDataReader dRgetAllBranch=cmdgetAllBranch.ExecuteReader();当您尝试在同一连接上执行存储过程时,您当前正在打开的连接 ( ) 上执行读取器。这就是错误的原因。

while在你的循环中试试这个

SqlConnection sqlConnStoredProc = new SqlConnection(MyClass.GlobalConn());
sqlConnStoredProc.Open();

SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", sqlConnStoredProc);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1;
cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2;
cmd.Parameters.Add("@BRANCH", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString();
cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString();
cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1;
cmd.ExecuteNonQuery();

sqlConnStoredProc.Close();
于 2013-04-02T08:39:52.290 回答
1

在使用阅读器时,您必须使用新的连接对象来执行另一个命令。

    SqlConnection sqlConn = new SqlConnection(MyClass.GlobalConn());
    sqlConn.Open();

    try
    {
        string getAllBranch = "SELECT iBranch_num,LTRIM(RTRIM(sConstant)) FROM tblgobranch";
        SqlCommand cmdgetAllBranch = new SqlCommand(getAllBranch, sqlConn);
        SqlDataReader dRgetAllBranch=cmdgetAllBranch.ExecuteReader();




        while (dRgetAllBranch.Read())
        {
            using(var con = new SqlConnection(MyClass.GlobalConn()))
            {

            con.Open();

            SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1;
            cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2;
            cmd.Parameters.Add("@BRANCH", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString();
            cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString();
            cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1;
            cmd.ExecuteNonQuery();

            }

        }
于 2013-04-02T08:42:44.253 回答
0

我认为您需要循环中的开放连接

while (dRgetAllBranch.Read())
        {

            SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", sqlConn);
            cmd.Connection.Open();  //Open
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1;
            cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2;
            cmd.Parameters.Add("@BRANCH", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString();
            cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString();
            cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1;
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();  //Close
        }
于 2013-04-02T08:43:24.607 回答
0

您只能使用一个 SqlCommand。在while循环之前打开,在循环之后关闭。

SqlConnection sqlConn = new SqlConnection(MyClass.GlobalConn());
    sqlConn.Open();

    try
    {
        string getAllBranch = "SELECT iBranch_num,LTRIM(RTRIM(sConstant)) FROM tblgobranch";
        SqlCommand cmdgetAllBranch = new SqlCommand(getAllBranch, sqlConn);
        SqlDataReader dRgetAllBranch=cmdgetAllBranch.ExecuteReader();


        SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection.Open();//open one time
        while (dRgetAllBranch.Read())
        {                
            cmd.parameters.Cler();//clear old parameter and add new
            cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1;
            cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2;
            cmd.Parameters.Add("@BRANCH", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString();
            cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString();
            cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1;
            cmd.ExecuteNonQuery();

        }
        cmd.Connection.Close();//open one time
于 2013-04-02T08:46:56.777 回答