PC Review


Reply
Thread Tools Rate Thread

Absolute Beginner Question

 
 
Chris Strug
Guest
Posts: n/a
 
      29th Nov 2005
Hi,

My apologies if this is a simple question but I'm having a hard time making
any progress so...

I'm looking at VB ADO.net, moving from ADO / Access development.

I've created a bog standard windows app in VB2005 Express and having read
some of the basics am attempting to create a connection to a SQLS2000 db on
my network and display the contents of a table in a listbox on a my form.

As I understand it, the steps involved are:
* create a connection to the database (done, I think)
* create a command object containing the action that i wish to perform
(SELECT statement)
* execute the command object and return the reults set to a DataReader
object as read-only which I can use to populate the listbox on my form.

I can create the connection and the command object but I'm not quite sure
where the datareader object comes in and how exactly I use it...

Please see code below.

I appreciate that this is a very simple question, however any and all advice
is gratefully received - be that links, pointers or so on.

Thanks

Chris.

**************************
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim TLCConn As SqlClient.SqlConnection
Dim contCMD As SqlClient.SqlCommand

TLCConn = New SqlConnection
TLCConn.ConnectionString = _
"Data Source=SQL;Integrated Security=SSPI;Initial
Catalog=TLCBackup"
TLCConn.Open()

contCMD = New SqlCommand
contCMD.Connection = TLCConn
contCMD.CommandText = "SELECT MovementNo, ContainerNo, DateIn,
DateOut " & _
"FROM Stock " & _
"WHERE dateout is null " & _
"ORDER BY MovementNo"
End Sub
End Class


 
Reply With Quote
 
 
 
 
Stuart Carnie
Guest
Posts: n/a
 
      29th Nov 2005
Dim reader as SqlDataReader

...
...

contCMD.Connection = TLCConn
contCMD.CommandText = "SELECT MovementNo, ContainerNo, DateIn, DateOut "
& _
"FROM Stock " & _
"WHERE dateout is null " & _
"ORDER BY MovementNo"

reader = contCMD.ExecuteReader()
Dim text As String

While reader.Read()
text = text + reader(0) + vbCrLf
End While

End Sub

Note: this is an example of reading the first column "reader(0)". You
should handle nulls and also should use StringBuilder for larger amounts of
concatenation. This serves only as an example.


"Chris Strug" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> My apologies if this is a simple question but I'm having a hard time
> making any progress so...
>
> I'm looking at VB ADO.net, moving from ADO / Access development.
>
> I've created a bog standard windows app in VB2005 Express and having read
> some of the basics am attempting to create a connection to a SQLS2000 db
> on my network and display the contents of a table in a listbox on a my
> form.
>
> As I understand it, the steps involved are:
> * create a connection to the database (done, I think)
> * create a command object containing the action that i wish to perform
> (SELECT statement)
> * execute the command object and return the reults set to a DataReader
> object as read-only which I can use to populate the listbox on my form.
>
> I can create the connection and the command object but I'm not quite sure
> where the datareader object comes in and how exactly I use it...
>
> Please see code below.
>
> I appreciate that this is a very simple question, however any and all
> advice is gratefully received - be that links, pointers or so on.
>
> Thanks
>
> Chris.
>
> **************************
> Imports System.Data.SqlClient
>
> Public Class Form1
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim TLCConn As SqlClient.SqlConnection
> Dim contCMD As SqlClient.SqlCommand
>
> TLCConn = New SqlConnection
> TLCConn.ConnectionString = _
> "Data Source=SQL;Integrated Security=SSPI;Initial
> Catalog=TLCBackup"
> TLCConn.Open()
>
> contCMD = New SqlCommand
> contCMD.Connection = TLCConn
> contCMD.CommandText = "SELECT MovementNo, ContainerNo, DateIn,
> DateOut " & _
> "FROM Stock " & _
> "WHERE dateout is null " & _
> "ORDER BY MovementNo"
> End Sub
> End Class
>
>



 
Reply With Quote
 
=?Utf-8?B?VmVua2F0X0tM?=
Guest
Posts: n/a
 
      29th Nov 2005
Hi Dear Chris Strug,

here is one sample:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click


Dim oSQLConn As SqlConnection = New SqlConnection
oSQLConn.ConnectionString = "Data Source=(local);Initial
Catalog=pubs;Integrated Security=SSPI;"
Dim oSQLCommand As SqlCommand
Try
If oSQLConn.State = ConnectionState.Closed Then
oSQLConn.Open()
End If

oSQLCommand = New SqlCommand("Select * from Authors", oSQLConn)

Dim oSQLDataReader As SqlDataReader
oSQLDataReader = oSQLCommand.ExecuteReader

While (oSQLDataReader.Read())

Me.ListBox1.Items.Add(oSQLDataReader("au_lname") & " " &
oSQLDataReader("au_fname"))
End While
Catch

Finally

oSQLCommand.Dispose()

If oSQLConn.State = ConnectionState.Open Then
oSQLConn.Close()
''oSQLConn.Dispose()
End If

End Try

End Sub


here are some useful links
==================

http://msdn.microsoft.com/library/de...tringTopic.asp


http://support.microsoft.com/default...b;en-us;313482
 
Reply With Quote
 
Chris Strug
Guest
Posts: n/a
 
      30th Nov 2005
Many thanks for the replies gents - thats cleared up a few things.

Next step is getting my head around the data adapter and dataset objects!

Thanks again

Chris


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
absolute beginner ant! Microsoft Access Getting Started 5 9th Apr 2009 01:23 PM
Absolute beginner =?Utf-8?B?QW50?= Microsoft Excel Programming 2 29th Jun 2007 10:42 AM
Absolute Beginner japin0@yahoo.com Microsoft ASP .NET 9 9th Sep 2005 12:17 AM
Re: Absolute beginner needs help.......... Shiner452 Microsoft Access Getting Started 1 20th May 2004 03:40 PM
Absolute beginner needs help.......... The G Man Microsoft Access Getting Started 0 20th May 2004 10:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:22 PM.