我在另一個sqldatareader內部使用sqldatareader聲明MultipleActiveResultSets = True。但它給一個異常「無效的嘗試調用read時讀取器已關閉」表在另一個sqldatareader內部使用sqldatareader生成錯誤
con.ConnectionString = "Data Source=HELLO-PC; Initial Catalog=hrmdb; Integrated Security=True; MultipleActiveResultSets=True"
con.Open()
Dim cmd2 As New SqlCommand("select count(*) from empsal", con)
Dim dr2 As SqlDataReader = cmd2.ExecuteReader()
While dr2.Read()
Dim count As Integer = dr2(0).ToString
End While
dr2.Close()
Dim cmd3 As New SqlCommand("select emplycode from empsal", con)
Dim dr3 As SqlDataReader = cmd3.ExecuteReader()
While dr3.Read()
code1 = dr3(i2).ToString
Dim cmd1 As New SqlCommand("select total from empsal where emplycode='" + code1 + "'", con)
Dim dr10 As SqlDataReader = cmd1.ExecuteReader()
While dr10.Read()
tot = dr10(0).ToString
End While
dr10.Close()
pTax = professionalTax(tot)
ESI(tot)
PF(code1, tot)
adDebits = addDebits()
eDebits = extraDebits(code1)
iTax = IncomeTax(tot)
txfrm = taxForm(code1, tot)
abf = absentFine(code1, tot)
totsal1 = tot - (Convert.ToDouble(pTax.ToString) + Convert.ToDouble(adDebits.ToString) + Convert.ToDouble(eDebits.ToString) + Convert.ToDouble(iTax.ToString) + Convert.ToDouble(txfrm.ToString) + Convert.ToDouble(abf.ToString) + Convert.ToDouble(esi1.ToString) + Convert.ToDouble(esi2.ToString) + Convert.ToDouble(pf1.ToString) + Convert.ToDouble(pf2.ToString))
com = New SqlCommand("INSERT INTO fnlreport (enumbr,pcd,pnm,dcd,dnm,totsal,pt,it,pf,cpf,esi,cesi,debits,edebits,absent,txfrm,tot,mnth1,year1) VALUES ('" + code1 + "','" + TextBox7.Text + "','" + ComboBox7.Text + "','" + TextBox8.Text + "','" + ComboBox8.Text + "','" + tot.ToString + "','" + pTax.ToString + "','" + iTax.ToString + "','" + pf1.ToString + "','" + pf2.ToString + "','" + esi1.ToString + "','" + esi2.ToString + "','" + adDebits.ToString + "','" + eDebits.ToString + "','" + abf.ToString + "','" + txfrm.ToString + "','" + totsal1.ToString + "','" + TextBox9.Text + "','" + TextBox10.Text + "')", con)
com.ExecuteNonQuery()
i2 = i2 + 1
End While
dr3.Close()
con.Close()
第一行被插入和發生異常的「而dr3.Read()」
亂七八糟的代碼弄亂了嗎?我不驚訝。更新您的代碼,但是這次在所有'IDisposable'對象上使用['Using'](http://msdn.microsoft.com/en-us/library/htd05whh.aspx)關鍵字。 –
我沒有看到你聲明「tot」的地方。 – alybaba726
http://stackoverflow.com/questions/2057534/using-multiple-data-readers –