473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

VB.NET & SQL Server Connection Example

I am new to .NET & SQL Server and am having fits trying to connect to a SQL
Server Database/table. Can someone please please please send me a copy of
their connection process (connection, dataadapater, fill, whatever is
needed to actually connect to the database & table) including querying a
table and looping through it? I don't need the code during the loop, I
just would like to see how you reference the fields and such.

Thanks for the help.
May 30 '06 #1
2 9526
Below is a sample class, that should do the job

If you need a connection string sample please reply with a request

hth,
Samuel

Public Class clsDataSQL

Public Shared Function GetConnectObj() As SqlClient.SqlConnection

Return New SqlClient.SqlConnection(objProgGlobals.ConnectionS tring)'The
connection string should be assigned

End Function

'Return a dataTable that was filled by the Adaptor.Fill method

Public Shared Function FillData(ByVal sSQL As String) As DataTable

Dim objDataAdapter As New System.Data.SqlClient.SqlDataAdapter(sSQL,
GetConnectObj)

Dim ObjTable As New DataTable("")

Try

objDataAdapter.Fill(ObjTable)

Catch Ex As System.data.SqlClient.SqlException

'Log the error

clsErrorLog.Log(Ex)

MsgBox(Ex.Message)

End Try

Return ObjTable

End Function

'Returns the ID of the new record

Shared Function DataSetAdaptor(ByVal sCommandText As String, _

ByRef iErrorNum As Integer, _

ByRef sErrorDesc As String, _

ByVal bNotLog As Boolean, _

ByVal bNotifyError As Boolean) As Integer

Dim objCmd As New System.Data.SqlClient.SqlCommand

Dim objReader As System.Data.SqlClient.SqlDataReader

With objCmd

.Connection = GetConnectObj()

.CommandText = sCommandText

.CommandType = CommandType.Text

End With

Try

objCmd.Connection.Open()

Catch myException As System.Exception

If myException.GetType.ToString =
"System.Data.SqlClient.SqlException" Then

iErrorNum = CType(myException,
System.Data.SqlClient.SqlException).Number

Exit Function

End If

If Not bNotLog Then

clsErrorLog.Log(myException)

iErrorNum = -1

sErrorDesc = myException.Message

End If

'Notify the error to the user

If bNotifyError Then

MsgBox(myException.Message)

End If

End Try

If objCmd.Connection.State = ConnectionState.Open Then

Try

objReader =
objCmd.ExecuteReader(CommandBehavior.CloseConnecti on)

Do While objReader.Read()

DataSetAdaptor = objReader(0)

Loop

objReader.Close()

Catch myException As System.Data.SqlClient.SqlException

If Not bNotLog Then

'Log the error

clsErrorLog.Log(myException)

End If

iErrorNum = myException.Number

'Notify the error to the user

If bNotifyError Then

MsgBox(myException.Message)

End If

Try

'Close the object reader that will cause the connection
to close

objReader.Close()

Catch

End Try

End Try

objCmd.Connection.Close()

End If

End Function



End Class



"Chris Moore" <chris@dblayoutdotcom> wrote in message
news:Xn*********************@207.46.248.16...
I am new to .NET & SQL Server and am having fits trying to connect to a SQL
Server Database/table. Can someone please please please send me a copy of
their connection process (connection, dataadapater, fill, whatever is
needed to actually connect to the database & table) including querying a
table and looping through it? I don't need the code during the loop, I
just would like to see how you reference the fields and such.

Thanks for the help.

May 30 '06 #2
This is extremely rough, and it will only work for SQL server (You'll
need to add data for each argument in the connection string):

Imports System.Data
Imports System.Data.SqlClient

Public Class DataConnection
Dim mIsSQLServer As Boolean
Dim mCn As SqlConnection
Public Function GetData(ByVal SQLstr As String) As SqlDataReader
Dim MyCommand As SqlCommand
Dim MyDataReader As SqlDataReader

' Create a Command object with the SQL statement.
MyCommand = New SqlCommand(SQLstr, mCn)

' Fill a DataSet with data returned from the database.
MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConne ction)

GetData = MyDataReader

End Function

Public Sub ExecuteQuery(ByVal Query As String)
'This Sub does not return a data set it just executes a query.
Dim myCmd As OleDbCommand

End Sub
Public Sub New(Optional ByVal IsSqlServer As Boolean = False)
'initialize the connection string
Dim ConnStr As String

ConnStr = "Server=;DataBase=;uid=;pwd="

mCn = New SqlConnection(ConnStr)

mCn.Open()

End Sub

End Class
Private Sub PopulateDropDown(ByRef Combo As DropDownList)
Dim myConnection As New DataConnection
Dim myDataSet As SqlDataReader
Dim SQLStr As String

SQLStr = "select kbsiseq from kbsi"

'get the data
myDataSet = myConnection.GetData(SQLStr)

'iterate through the data and populate the combo box
While myDataSet.Read
Combo.Items.Add(myDataSet("kbsiseq"))
End While

End Sub

There are of course, other ways to do it, and there is a lot more to a
real connection class than that, but that should get you through the
basics.

May 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ray | last post by:
Hi I need to access an access db via php. My hosting provider is using php version 4.3.10 on a windows 2003 platform. I think that php was installed with the Plesk for windows package/control...
3
by: Mick Turner | last post by:
I have connected to SQL Server database (server-side vbscript) and read some data from the tables. This works correctly. I now have the data in an array (server-side). I have to draw a...
10
by: Shaniqua Jones | last post by:
I've designed a C# application consisting of two EXEs: a client and server. The server runs on my Win2000 Server box, and the client runs on my customers' machines -- typically XP. The client app...
0
by: ʹÃûÑï | last post by:
ORA-03114: not connected to ORACLE && MS's Bug?? DataBase:Oracle 817 using OracleClient,net framework 1.1 I'm using ADO.Net in C# with Oracle 817. and following is my public data access...
1
by: hangaround | last post by:
I have read some codes like following: /////////////////////////////////////////// SqlConnection* conn = new SqlConnection(S"Server=localhost;" S"Database=Northwind;" S"Integrated...
6
by: Mike Moore | last post by:
I'm not very familiar with the web config file. I would like to use the timeout item in the session state for our connection string, but not use the stateconnection string and sqlconnectionstring...
1
by: dirk van waes | last post by:
Hello everyone, Being complete newbie in asp.net I am trying to make an example which works with a very simple database. First I made my project in VS- vb.net, draging an oledbconnection and an...
1
by: Jennifer | last post by:
Hi. I don't know a whole bunch about html and asp pages, but I thought I'd give it a try to see if I could get it to work. I found an example for a calendar event page from Microsoft at: ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.