Results 1 to 13 of 13

Thread: Need step-by-step SQL help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    Need step-by-step SQL help

    So my VB book only shows how to setup an SQL query at design time. I need to do it at run time.

    My program is just a little thing that allows a person to copy a file from their desktop to the network. The user would put the filename to be copied in a textbox. There is an Access database sitting on the network that maps all the files to particular directories. So when the user clicks on submit, the program looks up what directory to put it in and copies it. I'm stuck on looking up the directory to put it in.

    For development purposes, I have a copy of the Access database on my harddrive.

    I know I need a connection, a data adapter, and a dataset. How do I connect to the database on my hard drive? I've looked at a lot of code in other threads here, but I am lost. I think I need something like (this is just off the top of my head, I haven't tried this):

    VB Code:
    1. Dim dir As String
    2. Dim conn As New SqlConnection("c:\DB\SRCDATA.MDB")
    3. Dim query As String = "SELECT LockDirectory FROM MstrFileList WHERE MstrFileName = " & TextBox1.Text
    4. Dim cmd = New SqlCommand(query, conn)
    5. conn.Open()
    6. dir = cmd.ExecuteNonQuery()
    7. conn.Close()
    8. MsgBox("The directory is " & dir)

    As I said, I haven't actually tried this yet because I am so lost. Am I even on the right track? If this is completely off, could someone kind of step me through it?

    Can I put the path in the SqlConnection?

    Will the concatenation for query work?

    Did I declare cmd properly?

    Can I assign the cmd.ExecuteNonQuery to dir like that? Is the ExecuteNonQuery even correct?

    ?????

    Thanks

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You are using access, so don't use the SqlConnection object. You will want to use the OleDbConnection object. Same with dataadapters, datareaders, etc.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Do I explicitly declare the dataadapters and datareaders? If so, where and how?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by shadowfyre
    Do I explicitly declare the dataadapters and datareaders? If so, where and how?
    You need to read more about ADO.NET here : http://samples.gotdotnet.com/quickstart/winforms/

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Thanks for the link.

    One thing that I'm still confused on though.

    Here is what I have so far:

    VB Code:
    1. Dim oOleDbConnection As OleDb.OleDbConnection
    2.             Dim sConnString As String = _
    3.                 "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    4.                 "Data Source=C:\DB\SRCDATA.MDB;" & _
    5.                 "User ID=Admin;" & _
    6.                 "Password="
    7.             Dim cmd As String = "SELECT LockDirectory FROM MasterFileList WHERE MstrFileName = " _
    8.                 & TextBox1.Text
    9.             Dim mySqlCommand As SqlCommand
    10.  
    11.             oOleDbConnection = New OleDb.OleDbConnection(sConnString)
    12.             oOleDbConnection.Open()
    13.             mySqlCommand = New SqlCommand(cmd)
    14.             mySqlCommand.ExecuteNonQuery()

    It is not complete. My main question is, do I use the ExecuteNonQuery method or should I use the ExecuteReader method to get the result?

    Also, how do I assign the result, a directory path, to a usuable variable to use in my program? I haven't seen any examples that do that.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If you want to to get data from the database , then use DataReader like this :
    VB Code:
    1. While (adoReader.Read)
    2.             Str = adoReader.Item(ColumnString)            
    3. End While



    if you are deleting or update (meaning you're not getting data)
    then use OleCommand object like this :

    VB Code:
    1. If objCmd.ExecuteNonQuery() Then
    2.  'Executed
    3.  
    4.   Else
    5. 'Can't execute
    6.  
    7. End If

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Alright. I think I'm getting this. Ran into another problem, though.

    I'm getting an error when it tries to run the ExecuteReader method. It says:

    An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

    Additional information: ExecuteReader: Connection property has not been initialized.
    VB Code:
    1. Dim oOleDbConnection As OleDb.OleDbConnection
    2. Dim sConnString As String = _
    3.                 "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    4.                 "Data Source=C:\DB\SRCDATA.MDB;" & _
    5.                 "User ID=Admin;" & _
    6.                 "Password="
    7. Dim myReader As SqlDataReader
    8. Dim cmd As String = "SELECT LockDirectory FROM MasterFileList WHERE MstrFileName = " _
    9.                 & TextBox1.Text
    10. Dim mySqlCommand As SqlCommand
    11. Dim dir As String
    12.  
    13. oOleDbConnection = New OleDb.OleDbConnection(sConnString)
    14. oOleDbConnection.Open()
    15. mySqlCommand = New SqlCommand(cmd)
    16. myReader = mySqlCommand.ExecuteReader()    'This is where the error comes from
    17. dir = myReader.Item(0)
    18. myReader.Close()
    19. oOleDbConnection.Close()

    Any ideas on how to fix the error? Where should the connection property be initiailized?

    Also, the parameters for the myReader.Item method want an integer. Since this should only return a single result, I put 0 in instead of the column name. Is that correct?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    First off , why are you using Sql objs instead of using OleDb which is perfectly for mdb databases .sql classes are optimized versions of ole for SQL Servers though it can be used with mdb but not recommended .


    Then try this .
    VB Code:
    1. Private Sub CallMe()
    2.         Dim dir As String
    3.         Dim sConnString As String = _
    4.                                 "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    5.                                 "Data Source=C:\DB\SRCDATA.MDB;" & _
    6.                                 "User ID=Admin;" & _
    7.                                 "Password="
    8.  
    9.         Dim oOleDbConnection As New OleDbConnection(sConnString)
    10.         Dim cmd As String = "SELECT LockDirectory FROM MasterFileList WHERE MstrFileName = " _
    11.         & TextBox1.Text
    12.         Dim myReader As OleDbDataReader
    13.  
    14.         Try
    15.  
    16.             oOleDbConnection.Open()
    17.             Dim myOleCommand As New OleDbCommand(cmd, oOleDbConnection)
    18.             myReader = myOleCommand.ExecuteReader()    'This is where the error comes from
    19.             While (myReader.Read)
    20.                 dir = myReader.Item(0)
    21.             End While
    22.  
    23.         Catch ex As Exception
    24.             MessageBox.Show(ex.Message) ' if any error then send me this error message
    25.             myReader.Close()
    26.             oOleDbConnection.Close()
    27.         End Try
    28.        
    29.     End Sub

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Also, the parameters for the myReader.Item method want an integer. Since this should only return a single result, I put 0 in instead of the column name. Is that correct?
    myReader(column) is equal to myReader.Item(column) .
    You can replace 'column' with the number of colmn in the database (in your case you put 0 which means it's the first column in the table 'MasterFileList' ) or you can use string value that represents the column name . Both works but to make sure everything work well type the name of the column you are retrieving from .

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Thanks. There were a lot of DUH's in your code, stuff that I should have caught that I didn't. Namely the Ole vs. sql. It was late Friday afternoon. Does that a good excuse?

    Anyway. Here is my udated code (thanks Pirate):

    VB Code:
    1. '---------------- SQL stuff ----------------
    2.             Dim sConnString As String = _
    3.                 "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    4.                 "Data Source=C:\DB\SRCDATA.MDB;" & _
    5.                 "User ID=Admin;" & _
    6.                 "Password="
    7.             Dim oOleDbConnection As New OleDb.OleDbConnection(sConnString)
    8.             Dim myReader As OleDb.OleDbDataReader
    9.             Dim cmd As String = "SELECT LockDirectory FROM MasterFileList WHERE MstrFileName = " _
    10.                 & filename.ToUpper
    11.             Dim dir As String
    12.  
    13.             Try
    14.                 oOleDbConnection.Open()
    15.                 Dim myOleCommand = New OleDb.OleDbCommand(cmd, oOleDbConnection)
    16.                 myReader = myOleCommand.executereader()
    17.                 While (myReader.Read)
    18.                     dir = myReader.Item("LockDirectory")
    19.                 End While
    20.             Catch ex As Exception
    21.                 MessageBox.Show(ex.Message, "Caught Exception")
    22.                 myReader.Close()
    23.                 oOleDbConnection.Close()
    24.             End Try
    25.             myReader.Close()
    26.             oOleDbConnection.Close()
    27.             '----------------- SQL stuff --------------------

    Here is what the Catch statement sends:
    No value given for one or more required parameters.
    Once I click OK, I get the VS generated error:
    An unhandled exception of type 'System.NullReferenceException' occurred in PtchMngr.exe

    Additional information: Object reference not set to an instance of an object.
    and the "myReader.Close() command is highlighted.

    I wish the ex.Message was a bit more specific, so I could figure out which method I'm missing parameters for.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Got it! I needed to add some quotes around the filename and add a semi colon at the end of my query.

    Works great now. Thanks.

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok . Here is a working demo showing you how to use OleDataReader . You need VB.NET 2003 , XP (or 2000) MS Access , Winrar to unzip the project. Tell me if you're stuck in any part .
    Attached Files Attached Files

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by shadowfyre
    Got it! I needed to add some quotes around the filename and add a semi colon at the end of my query.
    Works great now. Thanks.
    Opps , I didn't know that you got it till I posted the demo . Glad it's working now .

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