我尝试在单个函数中执行两个相关的sql命令,通过一个新的xamlfile更新我的列xamlfile - > û
这是我的代码
Public Shared Function updatetest()
Dim c As SqlConnection = openConnection("Data Source=GENIOP40;Initial Catalog=TF5100_new;Integrated Security=true")
Dim c2 As SqlConnection = openConnection("Data Source=GENIOP40;Initial Catalog=TF5100_new;Integrated Security=true")
Dim cmd As New SqlCommand("SELECT xamlfile, id_wkfw_task from WKFW_Tasks Where xamlfile like '%ExecuteTask%' order by name", c)
Dim rd As SqlDataReader = executereader(cmd)
Dim listxaml As New List(Of String)
While rd.Read
Dim u As String = GetString(rd, "xamlfile")
Dim Id As Guid = GetGuid(rd, "id_wkfw_task")
u = Regex.Replace(u, "(?<=<t[1-2]|:ExecuteTask[^><]+)\bTache", "TaskID")
u = u.Replace("'", "''")
Dim cmd2 As New SqlCommand("UPDATE WKFW_Tasks set xamlfile=@xmlfile where id_wkfw_task=@id_t", c2)
cmd2.Parameters.Add("xmlfile", SqlDbType.NVarChar).Value = u
cmd2.Parameters.Add("id_t", SqlDbType.UniqueIdentifier).Value = Id
cmd2.CommandTimeout = 0
executeNonQuery(cmd2)
c2.Close()
End While
rd.Close()
c.Close()
End Function
我的问题是:当我在循环后关闭阅读器时,我的第一个命令不再被执行,我需要它被执行所以我的第二个命令可以工作,如果我不关闭我的读者我有错误,有什么建议吗?
答案 0 :(得分:0)
将初始连接字符串更改为
Dim c As SqlConnection = openConnection("Data Source=GENIOP40;
Initial Catalog=TF5100_new;Integrated Security=true;
MultipleActiveResultSets=true")
See MSDN on MultipleActiveResultSets
现在,您不再需要在调用之间关闭DataReader,您可以使用唯一的连接执行命令。但是我建议在循环外部创建update命令及其参数。
在循环内部只需更新参数值并执行
Dim cmd2 As New SqlCommand("UPDATE WKFW_Tasks set xamlfile=@xmlfile where id_wkfw_task=@id_t", c)
cmd2.Parameters.Add("xmlfile", SqlDbType.NVarChar)
cmd2.Parameters.Add("id_t", SqlDbType.UniqueIdentifier)
While rd.Read
....
cmd2.Parameters("xmlfile")..Value = u
cmd2.Parameters("id_t").Value = Id
executenonquery(cmd2)
End While