2011-09-16 8 views
0

Windowsフォームのテキストボックスから値を取り出し、SQL Serverデータベースの値を更新したいと思います。これらのフィールドのみを更新したいその値はユーザによって入力され、ユーザによってテキストボックスが空のままであるフィールドは残されています.....どのようにしてこのような状況に対して動的にクエリを生成できますか?vb.netのWindowsフォームのテキストボックスから値を取って更新する方法

編集:

私はまだ更新オプションのためにコード化されていないが...ここに私の挿入コードであると私はちょうど私が生成できる方法を見つけ出すことができなかったのと同じ方法を特徴とアップデートを実装したかったですクエリが動的に....これは非常に広い問題であり、非常にあなたが使用しようとしているデータアクセスフレームワークの種類にdependend

 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) 

答えて

0

予約システムアプリケーションです。

MsSqlを使用している場合はSqlCommandを使用するか、OdbcCommandを使用すると、変更したフィールドのセッターのみを含むSQL Updateステートメントですべてをラップすることができます。

dataset/datatable/dataadapterを実行し、行をロードして変更したフィールドのみを変更し、再度テーブルを更新することができます。

LingToSqlまたはEFでも同じことができます。

詳細をお知りになりたい場合は、試したことと使いたいことを教えてください。

+0

私はSqlCommandを試していて、Sqlコマンドのパラメータとしてテキストボックスの値を渡します。 –

+0

ok - これまでのコードを投稿できますか?しかし、代わりにDataSetやLinq2Sqlを使うことをお勧めします。これは簡単で安全です。 – Carsten

+0

私が知りたいことは、どのテキストボックスの値が変更されたかを追跡し、それに基づいてクエリ文字列を動的に生成できることです。 –