I want to take the values from textboxes on a windows form and then update the values in the sql server database...I want to update only those fields for which a value has been entered by the user and leave the fields for which the textboxes are left empty by the user.....How can I generate the query dynamically for such a situation???
Edit:
I haven't yet coded for the update option...Here is my insertion code and i wanted to implement the update feature the same way just couldn't figure out how i can generate the query dynamically....It's a Booking System application
Dim str As String ' defines str as a string variable
'takes inse开发者_运维百科rtion 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)
this is a very broad question and highly dependend on what type of data-access framework your are going to use.
You could go low-level and use SqlCommand (if you use MsSql) or OdbcCommand to wrap up all with a SQL Update statement, with setters only for the fields you changed.
You could do dataset/datatable/dataadapter, load the row change only the fields you changed and update the table again.
You could do the same with LingToSql or EF.
If you want details please tell us what you tried and what you want to use.
精彩评论