如何通过从vb.net中的Windows窗体上的文本框中获取值来更新

时间:2011-09-16 19:27:49

标签: sql-server vb.net

我想从Windows窗体上的文本框中获取值,然后更新sql server数据库中的值...我想只更新用户已输入值的字段并保留字段哪个文本框由用户留空......如何针对这种情况动态生成查询???

编辑:

我还没有为更新选项编码...这是我的插入代码,我想以同样的方式实现更新功能,但无法弄清楚我如何动态生成查询....这是预订系统申请

        Dim str As String  ' defines str as a string variable

        'takes insertion query as a string in str variable
        str = "Insert into Bookings  Values(@cust_id, @cust_name,@contact, @game, @courtno, @poolno, @tableno, @booking_date, @booking_time, @booking_duration)"

        'defines a new command which takes query string and connection string as parameters
        Dim cmd As New SqlCommand(str, con)

        ' defines Customer ID parameter and takes its value from the form 
        Dim prmCustID As New SqlParameter("@cust_id", SqlDbType.Char)
        prmCustID.Value = MskdTxtCustId.Text

        ' defines Customer Name parameter and takes its value from the form 
        Dim prmCustName As New SqlParameter("@cust_name", SqlDbType.Char)
        prmCustName.Value = TxtCustName.Text

        ' defines Contact parameter and takes its value from the form 

        Dim prmContact As New SqlParameter("@contact", SqlDbType.VarChar)
        prmContact.Value = MskdTxtCntctno.Text

        ' defines Game parameter and takes its value from the form 

        Dim prmGame As New SqlParameter("@game", SqlDbType.Char)
        prmGame.Value = TxtGame.Text

        ' defines Court No parameter and takes its value from the form 

        Dim prmCrtNo As New SqlParameter("@courtno", SqlDbType.Int)
        If TxtCrtNo.Text = "" Then
            prmCrtNo.Value = Convert.DBNull     'If the textbox is empty then places Null in databse field
        Else
            prmCrtNo.Value = CType(TxtCrtNo.Text, Integer)  ' converts from string to integer
        End If
        ' defines Pool No parameter and takes its value from the form 

        Dim prmPoolNo As New SqlParameter("@poolno", SqlDbType.Int)
        If TxtPoolNo.Text = "" Then
            prmPoolNo.Value = Convert.DBNull    'If the textbox is empty then places Null in databse field
        Else
            prmPoolNo.Value = CType(TxtPoolNo.Text, Integer)    ' converts from string to integer

        End If

        ' defines Table No parameter and takes its value from the form 

        Dim prmTblNo As New SqlParameter("@tableno", SqlDbType.Int)
        If TxtTblNo.Text = "" Then
            prmTblNo.Value = Convert.DBNull     'If the textbox is empty then places Null in databse field
        Else
            prmTblNo.Value = CType(TxtTblNo.Text, Integer)  ' converts from string to integer
        End If

        ' defines Booking Date parameter and takes its value from the form 

        Dim prmBookDate As New SqlParameter("@booking_date", SqlDbType.DateTime)
        prmBookDate.Value = TxtBookDate.Text

        ' defines Booking Time parameter and takes its value from the form 

        Dim prmBookTime As New SqlParameter("@booking_time", SqlDbType.DateTime)
        prmBookTime.Value = TxtBookTime.Text

        ' defines Booking Duration parameter and takes its value from the form 

        Dim prmBookDur As New SqlParameter("@booking_duration", SqlDbType.Int)
        prmBookDur.Value = CType(TxtBookDur.Text, Integer)


        'Command cmd takes all the parameters

        cmd.Parameters.Add(prmCustID)
        cmd.Parameters.Add(prmCustName)
        cmd.Parameters.Add(prmContact)
        cmd.Parameters.Add(prmGame)
        cmd.Parameters.Add(prmCrtNo)
        cmd.Parameters.Add(prmBookDate)
        cmd.Parameters.Add(prmBookTime)
        cmd.Parameters.Add(prmBookDur)
        cmd.Parameters.Add(prmPoolNo)
        cmd.Parameters.Add(prmTblNo)


        Dim str1 As String  ' defines string variable for taking select query


        str1 = "select bookingID from Bookings"     'takes select query in string variable for retrieving booking ID from the databse

        Dim cmd1 As New SqlCommand(str1, con)    'defines a new command which takes query string and connection string as parameters
        Dim x As Integer                        ' defines an integer for storing booking ID

        con.Open()                               'sets the connection state to open
        Using (con)                              'specifies the connection which is to be used by the SQLcommands

            cmd.ExecuteNonQuery()                'Executes the insertion query

            Dim id As SqlDataReader = cmd1.ExecuteReader()      'Defines and initiates the datareader to read data from database using cmd1 command
            While id.Read()                                     'Iterates the reader to read booking id
                x = id("bookingID")                             'stores the booking Id in variable x
            End While
            id.Close()
        End Using
        con.Close()                                             'sets the connection state to close

        ' shows message box with successful booking message
        MessageBox.Show("New booking saved successfully" & vbCrLf & "Your Booking ID is " & x, "Saved Successfully", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

1 个答案:

答案 0 :(得分:0)

这是一个非常广泛的问题,并且高度依赖于您将使用何种类型的数据访问框架。

你可以进入低级别并使用SqlCommand(如果使用MsSql)或OdbcCommand用SQL Update语句包装所有语句,只为你更改的字段设置setter。

您可以执行dataset / datatable / dataadapter,只加载行更改您更改的字段并再次更新表。

您可以使用LingToSql或EF执行相同操作。

如果您需要详细信息,请告诉我们您的尝试以及您想要使用的内容。