Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a database

Status
Not open for further replies.

arkomnv

Programmer
May 22, 2006
14
BE
Hello there

could some1 help me to a example code or programe thats search a database using query's or something a like

I now how it works in vb 6 but not in .NET

could some1 pls help me out with this 1


THX
 
yep,

Code:
[COLOR=green]' Declarations[/color]
Imports System.Data
Imports System.Data.SqlClient

[COLOR=green]' Create the conncetion string[/color]
Dim cnStr As String = "Data Source=.\SQLEXPRESS; AttachDbFilename=***; Integrated Security=True; User Instance=True"

[COLOR=green]' Create the connection[/color]
Dim cn As New SqlConnection(cnStr)

[COLOR=green]' Open it![/color]
cn.Open()

[COLOR=green]' Your query String[/color]
Dim qS as String = "SELECT ..... etc"

[COLOR=green]' The data adapter[/color]
Dim da As New SqlDataAdapter(qS, cn)

[COLOR=green]' Use it to fill a data table with the result of the qS[/color]
Dim myDataTable as DataTable
da.Fill(myDataTable)

[COLOR=green]' Release the resources[/color]
cn.Close()
cn.Dispose()
da.Dispose()

Then use the myDataTable and loop:
myDataTable.Rows(i).Item(j) ' gives the j'th item of the i'th row

The *** is the location of the database, ... C:\mydatas\blah.mdf

ALSO, do the appropriate changes if you want to use oracle for example. In that example i use sql server. (You may need to change the "Data Source=" and the data adapter, e.g. OLE)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top