简体   繁体   English

循环遍历数据表并运行存储过程

[英]loop through datatable and run stored procedure

I am trying to loop through a datatable, that will pass information to my stored procedure but it isn't working.我正在尝试遍历一个数据表,它将信息传递给我的存储过程,但它不起作用。

even though it works when i manuelly即使它在我手动时有效

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dv As New DataView
            Dim dt As New DataTable
            dv = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

            dt = dv.ToTable()


            For Each row As DataRow In dt.Rows

                If row("vtr_gen10").ToString() = "y" Or row("vn10").ToString() = "a" Or row("vtr_gen10").ToString() = "p" Then
                    Dim SQLCon As New SqlClient.SqlConnection
                    Dim sqlcmd As New SqlCommand
                    SQLCon.ConnectionString = SqlDataSource2.ConnectionString

                    SQLCon.Open()
                    sqlcmd.CommandText = "protest" ' Stored Procedure to Call
                    sqlcmd.CommandType = CommandType.StoredProcedure 'Setup Command Type
                    sqlcmd.Connection = SQLCon 'Active Connection

                    sqlcmd.Parameters.AddWithValue("@ID", row("id"))
                    sqlcmd.Parameters.AddWithValue("@Value", "Y")


                    sqlcmd.ExecuteNonQuery()
                    SQLCon.Close()

                End If

            Next

        End Sub

There is nothing seems to be wrong with your code, but if you think its because its moving too fast, try adding sleep using System.Threading.Thread.Sleep(milliseconds)您的代码似乎没有任何问题,但如果您认为它的移动速度太快,请尝试使用System.Threading.Thread.Sleep(milliseconds)添加睡眠

Are you absolutely sure the IF criteria is being met after the first two records?您绝对确定在前两条记录之后满足 IF 标准吗? One thing to look for is spaces or other string anomalies such as carraige returns in your database...要查找的一件事是空格或其他字符串异常,例如数据库中的 carraige 返回...

Row("vn10").ToString for example may for some reason be "a " instead of "a".例如,Row("vn10").ToString 可能由于某种原因是“a”而不是“a”。 Might be worth putting in a temporary debug line such as:可能值得放入一个临时调试行,例如:

Throw New Exception("[" & Row("vn10").ToString & "]") 

or inspect length on some records that erroneously fail.或检查一些错误失败的记录的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM