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();
}
我在这里遇到错误
“已经打开了与此命令关联的数据阅读器”
请帮助我。
答案 0 :(得分: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();
答案 1 :(得分: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();
}
}
答案 2 :(得分: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
}
答案 3 :(得分:0)
您只能使用一个SqlCommand。在循环之前打开,在循环之后关闭。
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