在单个循环中选择多行和更新查询

时间:2016-12-06 05:48:28

标签: sql vb.net

从多行中选择一行(通过sql查询)更新它,然后选择另一行,再次更新它,依此类推。全部在循环中(逐行读取行),直到所有选定的行都没有更新

  Imports System.IO
    Imports System.Data.Sql
    Imports System.Data.SqlClient
    Imports System.Data
    Partial Class Officer_Module_GST_id_password
    Inherits System.Web.UI.Page
    Dim sscript As String
    Dim sms As New smsgw
    Dim mail As New MailSender
    Dim cmd As New SqlCommand
    Dim ds As New DataSet
    Dim dr As SqlDataReader
    Dim objconn As New connectioncls
    Dim name As String
    Dim pid As String
    Dim pwd As String
    Dim email_sent As Integer
    Dim email_status As String
    Dim mobile As String
    Dim message As String
    Dim subject As String
    Dim email As String
    Dim mtext As String

    Protected Sub validatedeal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles validatedeal.Click
containertwo.Visible = True
    txt_subject.Text = "Communication of the Provisional Identification Number and Password"
    txt_mail.Text = "Instance mail"
    End Sub

    Protected Sub btnsendmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    objconn.openconn()
    cmd.Connection = objconn.conn
    cmd.CommandText = "Select Trade_name,provissional_id,access_token,reg_mobile_no,reg_email_id,email_status,isnull(no_of_email_sent,0) from Provisional_details"
    Dim dr1 As SqlDataReader = cmd.ExecuteReader()
    While (dr1.Read())

    name = dr1(0).ToString()
    pid = dr1(1).ToString()
    pwd = dr1(2).ToString()
    mobile = dr1(3).ToString()
    email = dr1(4).ToString()
    email_status = dr1(5).ToString()
    email_sent = dr1(6).ToString()
    subject = "subject to instance"
    mtext = "new instance email"
    message = "new instance message"
    Try
    MailSender.SendEmail("riteshbhatt93" + "@gmail.com", "rock_on", email, subject, mtext,         System.Web.Mail.MailFormat.Text, "")
    Try
    Call sms.SendSMSUsingNICGW(mobile, message)
    Catch
    sscript = "<script language=javascript>alert('Message not sent!!')</script>"
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "Empty", sscript)
    sscript = Nothing
    Exit Try
    Finally
    End Try

    Try
    Call sms.SendSMSUsingMGOVGW(mobile, message)
    Catch
    sscript = "<script language=javascript>alert('Message not sent!!')</script>"
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "Empty", sscript)
    sscript = Nothing
    Exit Try
    Finally
    End Try
    Catch

    Dim cmd1 As New SqlCommand
    cmd1.Connection = objconn.conn
    cmd1.Parameters.AddWithValue("@mobile", mobile)
    cmd1.Parameters.AddWithValue("@Email_status", "NO")
    cmd1.CommandText = "Update Provisional_details set   Email_sent =  @Email_status where reg_mob_no = @mobile"
    cmd1.ExecuteNonQuery()
    cmd1.Parameters.Clear()
    Exit Sub
    Finally
    End Try
    dr1.Close()
    Dim cmd2 As New SqlCommand
    cmd2.Connection = objconn.conn
    cmd2.Parameters.AddWithValue("@mobile", mobile)
    cmd2.Parameters.AddWithValue("@Email_status", "YES")
    cmd2.Parameters.AddWithValue("@emailsent", email_sent + 1)
    cmd2.CommandText = "Update Provisional_details set   email_status =  @Email_status,no_of_email_sent = @emailsent  where reg_mobile_no = @mobile"

    cmd2.ExecuteNonQuery()
    cmd2.Parameters.Clear()
    End While

    sscript = "<script language=javascript>alert('Your Mail has been sent to all applied dealers!!')</script>"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Empty", sscript)
    sscript = Nothing

    End Sub
    End Class

0 个答案:

没有答案