reading ms sql server
reading ms sql server
reading ms sql server
reading ms sql server
reading ms sql server
reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server
reading ms sql server reading ms sql server
reading ms sql server
Go Back  Xtreme Visual Basic Talk > > > reading ms sql server


Reply
 
Thread Tools Display Modes
  #1  
Old 08-29-2010, 01:42 PM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default reading ms sql server


I have been searching the internet for hours now looking for some decent tutorials or articles on microsoft sql server 2008.

I use this code for the connection:

Public myConnection As SqlConnection = New SqlConnection("Data Source=NAME;Initial Catalog=NAME;User Id=NAME;Password=NAME;")
Public myCommand As SqlCommand

I want to read data from a table and place the contents in the variable arrays to then use for other purposes.

I dont even know if this works but I dont know how to read the table:

myConnection.Open()
myCommand = New SqlCommand(strSQL, myConnection)
myCommand.ExecuteReader()
myConnection.Close()

As when I used MS ACCESS I used:
Code:
    dim  ItemID(10) as string 
            dim      ItemCatID(10) as string 
             dim     ItemTitle(10) as string 

       Dim strResultsHolder As String
        Dim objConnection As New OleDbConnection(strConnection)
        Dim objCommand As New OleDbCommand(strSQL, objConnection)
        Dim objDataReader As OleDbDataReader
        Try
            objConnection.Open()
            objDataReader = objCommand.ExecuteReader
            Do While objDataReader.Read() = True
                ItemID(counter) = CStr(objDataReader("itmID"))
                ItemCatID(counter)  = CStr(objDataReader("itmCatID"))
                ItemTitle(counter)  = CStr(objDataReader("itmTitle"))
                Counter += 1
            Loop
            objDataReader.Close()
            objConnection.Close()
Can somebody please tell me how to read the table or atleast point me to a decent tutorial?

Thanks
Reply With Quote
  #2  
Old 08-29-2010, 02:58 PM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

After hours and hours of research & trial and error I finally figured out the solution - and it turned our to be so much bloody simpler than I had hoped!!!

For anybody else that has a similar problem heres the simple bit of code:

Dim t As String
myCommand = New SqlCommand(strSQL, myConnection)
Dim rd As SqlDataReader = myCommand.ExecuteReader()
Do While rd.Read = True
t = rd("userid")
Loop
myConnection.Close()
Reply With Quote
  #3  
Old 08-29-2010, 04:23 PM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

I am still experiencing an error.

Quote:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
I use this code for connecting and reading the DB:

Code:
     
    Public myConnection As SqlConnection = New SqlConnection("Data Source=SERVER;Initial Catalog=MYDB;User Id=MYDB;Password=MYDB;")
     Public myCommand As SqlCommand   

        myCommand = New SqlCommand(strSQL, myConnection)
        myConnection.Open()
        Dim rd As SqlDataReader = myCommand.ExecuteReader()
        While rd.Read()
            ItemID(counter) = rd("itmID")
            ItemTitle(counter) = rd("itmTitle")
            test = strSQL
            counter += 1
        End While
        ItemQty = rd.FieldCount
        myConnection.Close()
The sql statement is fine! I executed it directly in the ms server and it yieldedthe correct result.

Please please please help!!...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
reading ms sql server
reading ms sql server
reading ms sql server reading ms sql server
reading ms sql server
reading ms sql server
reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server reading ms sql server
reading ms sql server
reading ms sql server
 
reading ms sql server
reading ms sql server
 
-->