Results 1 to 8 of 8

Thread: Execute SQL commands

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Question Execute SQL commands

    I have the following code.
    VB Code:
    1. 'Set SQL statements
    2.         With daStaff
    3.             .DeleteCommand.CommandText = "DELETE FROM Staff" 'Delete statement
    4.             .InsertCommand.CommandText = "INSERT INTO Staff" 'Insert statement
    5.             .UpdateCommand.CommandText = SQLUpdateStr 'Update statement
    6.         End With
    How would i execute the "Delete, Insert or Update' CommandText?

    When i am in the code window i see .ExecuteNonQuery, .ExecuteReader and .ExecuteScalar when i type daStaff.UpdateCommand. in the dropdown list. Which one, if any would i use?
    Last edited by x-ice; Jun 29th, 2005 at 08:38 PM.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Execute SQL commands

    .ExecuteNonQuery ' Returns the Number of rows affected, you should use this one.
    .ExecuteReader ' is for SELECT statements (where your getting values from a table)
    .ExecuteScalar ' is for when a single value is returned
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Execute SQL commands

    When i try to execute Delete, Insert or Update command i get an error (below). To execute the command i use the following code, where am i going wrong?
    VB Code:
    1. [B]daStaff.InsertCommand.ExecuteNonQuery()[/B]
    ERROR: "ExecuteNonQuery: Connection property has not been initialized."

    The code in bold is the error code.

    Here is the code i use to set the SQL commands.
    VB Code:
    1. Private Sub SetSQLStatements()
    2.         'Add parameters to use in SQL statements
    3.         With cmdStaff.Parameters
    4.             .Add("@EmpID", txtEmpID.Text)
    5.             .Add("@Surname", txtSurname.Text)
    6.             .Add("@Forename", txtForename.Text)
    7.             .Add("@Gender", txtGender.Text)
    8.             .Add("@DOB", dtpDOB.Text)
    9.             .Add("@Address", txtAddress.Text)
    10.             .Add("@Postcode", txtPostcode.Text)
    11.             .Add("@TelNum", txtTelNum.Text)
    12.             .Add("@Contract", txtContract.Text)
    13.             .Add("@Salary", txtSalary.Text)
    14.             .Add("@Position", txtPosition.Text)
    15.             .Add("@StartDate", dtpStartDate.Text)
    16.             .Add("@BookRight", cboBookingRights.Text)
    17.             .Add("@LoginRight", cboLoginRights.Text)
    18.             .Add("@MemberRight", cboMemberRights.Text)
    19.             .Add("@ManageRight", cboManageRights.Text)
    20.         End With
    21.         Dim SQLUpdateStr As String
    22.         'SQL Update string
    23.         SQLUpdateStr = "UPDATE Staff SET Address = @Address, BookingRights = @BookRight," & _
    24.         "Contract = @Contract, DOB = @DOB, Forename = @Forename, Gender = @Gender," & _
    25.         "LoginRights = @LoginRight, ManageRights = @ManageRight, MemberRights = @MemberRight," & _
    26.         "Position = @Position, Postcode = @Postcode, Salary = @Salary, StartDate = @StartDate," & _
    27.         "Surname = @Surname, [Telephone Number] = @TelNum"
    28.         'Create instances of OLEDB commands
    29.         Dim cmdSQLDel As New OleDbCommand("DELETE FROM Staff WHERE EmployeeID = @EmpID") 'Delete statement
    30.         Dim cmdSQLIns As New OleDbCommand("INSERT INTO Staff") 'Insert statement
    31.         Dim cmdSQLUpd As New OleDbCommand(SQLUpdateStr) 'Update statement
    32.         'Set SQL statements
    33.         With daStaff
    34.             .DeleteCommand = cmdSQLDel
    35.             .InsertCommand = cmdSQLIns
    36.             .UpdateCommand = cmdSQLUpd
    37.         End With
    38.     End Sub

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Execute SQL commands

    where is your connection?
    VB Code:
    1. Dim cmdSQLDel As New OleDbCommand("DELETE FROM Staff WHERE EmployeeID = @EmpID",[b]myconnection[/b]) 'Delete statement
    2.         Dim cmdSQLIns As New OleDbCommand("INSERT INTO Staff",[b]myconnection[/b]) 'Insert statement
    3.         Dim cmdSQLUpd As New OleDbCommand(SQLUpdateStr)
    4.         cmdsqlupd.connection=[b]myconnection[/b]

  5. #5

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Execute SQL commands

    Quote Originally Posted by mar_zim
    where is your connection?
    VB Code:
    1. Dim cmdSQLDel As New OleDbCommand("DELETE FROM Staff WHERE EmployeeID = @EmpID",[b]myconnection[/b]) 'Delete statement
    2.         Dim cmdSQLIns As New OleDbCommand("INSERT INTO Staff",[b]myconnection[/b]) 'Insert statement
    3.         Dim cmdSQLUpd As New OleDbCommand(SQLUpdateStr)
    4.         cmdsqlupd.connection=[b]myconnection[/b]
    Ok, i've done that but i am getting a syntax error in my SQL statements now. What is wrong with these SQL statements?[Highlight=VB]Dim SQLUpdateStr As String
    'SQL Update string
    SQLUpdateStr = "UPDATE Staff SET Address = @Address, BookingRights = @BookRight," & _
    "Contract = @Contract, DOB = @DOB, Forename = @Forename, Gender = @Gender," & _
    "LoginRights = @LoginRight, ManageRights = @ManageRight, MemberRights = @MemberRight," & _
    "Position = @Position, Postcode = @Postcode, Salary = @Salary, StartDate = @StartDate," & _
    "Surname = @Surname, [Telephone Number] = @TelNum"

    'Create instances of OLEDB commands
    Dim cmdSQLDel As New OleDbCommand("DELETE * FROM Staff WHERE EmployeeID = @EmpID", conStaff) 'Delete statement
    Dim cmdSQLIns As New OleDbCommand("INSERT INTO Staff", conStaff) 'Insert statement
    Dim cmdSQLUpd As New OleDbCommand(SQLUpdateStr) 'Update statement
    'Set connection for 'cmdSQLUpd'
    cmdSQLUpd.Connection = conStaff

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Execute SQL commands

    Your commands are already attached to a DataAdapter so don't be using ExecuteNonQuery for each command. The Update method of the DataAdapter executes all three commands. Assuming that each command contains valid SQL code in its CommandText and your connection issues are worked out, a single call to the Update method of your DataAdapter will update all deleted, inserted and updated rows (in that order).
    The easiest way to create a DataAdapter is in the design window. If it is desireable, or at least OK, that your DataAdapter have form-level scope, add it to your form in the designer and then you can simply set properties or, easier still, use the "Configure Data Adapter..." function at the bottom of the Properties window.

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Execute SQL commands

    x-ice: ok how do you declare your dataadapter?

    do you pass a connection to your dataadapter?
    like this one?
    VB Code:
    1. With daStaff
    2. [b]        .DeleteCommand.connection = myconnection[/b]
    3.         [b].InsertCommand.connection = myconnection[/b]
    4.         [b].UpdateCommand.connection = myconnection[/b]
    5.             .DeleteCommand = cmdSQLDel
    6.             .InsertCommand = cmdSQLIns
    7.             .UpdateCommand = cmdSQLUpd
    8.         End With

  8. #8

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Execute SQL commands

    Here is the code for the whole form.
    VB Code:
    1. #Region "Public Declaration"
    2.     'Create connection
    3.     Dim conStaff As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=XGamesDB.mdb;Persist Security Info=False")
    4.     'Create command
    5.     Dim cmdStaff As New OleDbCommand("SELECT * FROM Staff Where EmployeeID = " & EmpID, conStaff)
    6.     'Create data adapter
    7.     Dim daStaff As New OleDbDataAdapter(cmdStaff)
    8.     'Create dataset
    9.     Dim dsStaff As New DataSet()
    10. #End Region
    11.  
    12. #Region "Form Code"
    13.     Private Sub frmManageStf_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.         'Open connection
    15.         conStaff.Open()
    16.         'Fill Data Adapter
    17.         daStaff.Fill(dsStaff, "Staff")
    18.         'Bind data to controls
    19.         Call BindData()
    20.         'Set 'Insert, Delete and Update' SQL statements
    21.         Call SetSQLStatements()
    22.     End Sub
    23.  
    24.     Private Sub frmManageStf_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    25.         'Close connection, if its open
    26.         If conStaff.State = ConnectionState.Open Then conStaff.Close()
    27.     End Sub
    28. #End Region
    29.  
    30. #Region "Sub Procedures"
    31.     Private Sub BindData()
    32.         'Bind data to controls
    33.         txtEmpID.DataBindings.Add(New Binding("Text", dsStaff, "Staff.EmployeeID"))
    34.         txtSurname.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Surname"))
    35.         txtForename.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Forename"))
    36.         txtGender.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Gender"))
    37.         dtpDOB.DataBindings.Add(New Binding("Text", dsStaff, "Staff.DOB"))
    38.         txtAddress.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Address"))
    39.         txtPostcode.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Postcode"))
    40.         txtTelNum.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Telephone Number"))
    41.         txtContract.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Contract"))
    42.         txtSalary.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Salary"))
    43.         txtPosition.DataBindings.Add(New Binding("Text", dsStaff, "Staff.Position"))
    44.         dtpStartDate.DataBindings.Add(New Binding("Text", dsStaff, "Staff.StartDate"))
    45.         cboBookingRights.DataBindings.Add(New Binding("Text", dsStaff, "Staff.BookingRights"))
    46.         cboLoginRights.DataBindings.Add(New Binding("Text", dsStaff, "Staff.LoginRights"))
    47.         cboMemberRights.DataBindings.Add(New Binding("Text", dsStaff, "Staff.MemberRights"))
    48.         cboManageRights.DataBindings.Add(New Binding("Text", dsStaff, "Staff.ManageRights"))
    49.     End Sub
    50.  
    51.     Private Sub SetSQLStatements()
    52.         'Add parameters to use in SQL statements
    53.         With cmdStaff.Parameters
    54.             .Add("@EmpID", txtEmpID.Text)
    55.             .Add("@Surname", txtSurname.Text)
    56.             .Add("@Forename", txtForename.Text)
    57.             .Add("@Gender", txtGender.Text)
    58.             .Add("@DOB", dtpDOB.Text)
    59.             .Add("@Address", txtAddress.Text)
    60.             .Add("@Postcode", txtPostcode.Text)
    61.             .Add("@TelNum", txtTelNum.Text)
    62.             .Add("@Contract", txtContract.Text)
    63.             .Add("@Salary", txtSalary.Text)
    64.             .Add("@Position", txtPosition.Text)
    65.             .Add("@StartDate", dtpStartDate.Text)
    66.             .Add("@BookRight", cboBookingRights.Text)
    67.             .Add("@LoginRight", cboLoginRights.Text)
    68.             .Add("@MemberRight", cboMemberRights.Text)
    69.             .Add("@ManageRight", cboManageRights.Text)
    70.         End With
    71.         Dim SQLUpdateStr As String
    72.         'SQL Update string
    73.         SQLUpdateStr = "UPDATE Staff SET Address = @Address, BookingRights = @BookRight," & _
    74.         "Contract = @Contract, DOB = @DOB, Forename = @Forename, Gender = @Gender," & _
    75.         "LoginRights = @LoginRight, ManageRights = @ManageRight, MemberRights = @MemberRight," & _
    76.         "Position = @Position, Postcode = @Postcode, Salary = @Salary, StartDate = @StartDate," & _
    77.         "Surname = @Surname, [Telephone Number] = @TelNum"
    78.         'Create instances of OLEDB commands
    79.         Dim cmdSQLDel As New OleDbCommand("DELETE * FROM Staff WHERE EmployeeID = @EmpID", conStaff) 'Delete statement
    80.         Dim cmdSQLIns As New OleDbCommand("INSERT INTO Staff", conStaff) 'Insert statement
    81.         Dim cmdSQLUpd As New OleDbCommand(SQLUpdateStr) 'Update statement
    82.         'Set connection for 'cmdSQLUpd'
    83.         cmdSQLUpd.Connection = conStaff
    84.         'Set SQL statements
    85.         With daStaff
    86.             .DeleteCommand = cmdSQLDel
    87.             .InsertCommand = cmdSQLIns
    88.             .UpdateCommand = cmdSQLUpd
    89.         End With
    90.     End Sub
    91. #End Region
    92.  
    93.     Private Sub btnAddEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEmp.Click
    94.         Try
    95.             daStaff.DeleteCommand.ExecuteNonQuery()
    96.             'Whatever command i try to execute, i still get a syntax error in my SQL queries
    97.         Catch Ex As Exception
    98.             MessageBox.Show(Ex.Message)
    99.         End Try
    100.     End Sub
    Last edited by x-ice; Jun 6th, 2005 at 05:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width