在循环 vb.net 中保存每个项目

发布于 2024-09-14 12:25:40 字数 1539 浏览 4 评论 0原文

我需要更新我的存储过程,因为消息框读取 datagridview 的每个项目。它只保存消息框读取的最后一条记录。需要帮助:(

For Each row As DataGridViewRow In DataGridView1.Rows
                Dim strunitprice As String = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))

                'opening the connection
                x = MessageBox.Show(CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text)))
                If x = DialogResult.OK Then
                    Label27.Text = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))
                End If

Dim dr5 As SqlDataReader
        Dim myCommand As SqlCommand
        connectme.Open()
        myCommand = New SqlCommand("Select * from quotation", connectme)
        dr5 = myCommand.ExecuteReader()

        While dr5.Read()
            connectme1.Close()

            Dim c As New SqlCommand("updatequotation", connectme1)
            Dim Adapter As SqlDataAdapter = New SqlDataAdapter(c)
            c.CommandType = CommandType.StoredProcedure
            c.Parameters.Add("@Customer_Name", SqlDbType.NVarChar).Value =   txtclientname.Text
            c.Parameters.Add("@Date_Today", SqlDbType.VarChar).Value = Label7.Text
            c.Parameters.Add("@Net_Income", SqlDbType.VarChar).Value = TXTNET.Text
            c.Parameters.Add("@Unit_Price", SqlDbType.VarChar).Value = Label27.Text
            connectme1.Open()
            c.ExecuteNonQuery()
            connectme1.Close()
        End While
        dr5.Close()
        connectme.Close()


            Next row
        End If

i need to make my storedprocedure update as as the messabox reads each item of a datagridview. it only save the last record that messagebox read. need help:(

For Each row As DataGridViewRow In DataGridView1.Rows
                Dim strunitprice As String = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))

                'opening the connection
                x = MessageBox.Show(CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text)))
                If x = DialogResult.OK Then
                    Label27.Text = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))
                End If

Dim dr5 As SqlDataReader
        Dim myCommand As SqlCommand
        connectme.Open()
        myCommand = New SqlCommand("Select * from quotation", connectme)
        dr5 = myCommand.ExecuteReader()

        While dr5.Read()
            connectme1.Close()

            Dim c As New SqlCommand("updatequotation", connectme1)
            Dim Adapter As SqlDataAdapter = New SqlDataAdapter(c)
            c.CommandType = CommandType.StoredProcedure
            c.Parameters.Add("@Customer_Name", SqlDbType.NVarChar).Value =   txtclientname.Text
            c.Parameters.Add("@Date_Today", SqlDbType.VarChar).Value = Label7.Text
            c.Parameters.Add("@Net_Income", SqlDbType.VarChar).Value = TXTNET.Text
            c.Parameters.Add("@Unit_Price", SqlDbType.VarChar).Value = Label27.Text
            connectme1.Open()
            c.ExecuteNonQuery()
            connectme1.Close()
        End While
        dr5.Close()
        connectme.Close()


            Next row
        End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-09-21 12:25:40

为什么要关闭连接然后从中创建命令。从 while 循环中删除 connection.close() 并再次运行代码。它应该有效。

Why are you closing connection and then creating command out of it. Remove the connection.close() from while loop and run the code again. It should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文