How can it connect to the database without knowing it's path

F

Familjen Karlsson

Here is an example from the help on the keword OleDbConnection, in VB.Net,
they don't give the path to the database just the word localhost. How can it
connect to the database without knowing where it is. What does the keyword
localhost do.

Thank's

Fia

Public Sub InsertRow(myConnectionString As String)
' If the connection string is null, use a default.
If myConnectionString = "" Then
myConnectionString = "Provider=SQLOLEDB;Data
Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"
End If
Dim myConnection As New OleDbConnection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID,
CompanyName) Values('NWIND', 'Northwind Traders')"
Dim myCommand As New OleDbCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Sub
 
J

JamesB

Familjen Karlsson said:
Here is an example from the help on the keword OleDbConnection, in VB.Net,
they don't give the path to the database just the word localhost. How can
it
connect to the database without knowing where it is. What does the keyword
localhost do.

Thank's

Fia

Public Sub InsertRow(myConnectionString As String)
' If the connection string is null, use a default.
If myConnectionString = "" Then
myConnectionString = "Provider=SQLOLEDB;Data
Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"
End If
Dim myConnection As New OleDbConnection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID,
CompanyName) Values('NWIND', 'Northwind Traders')"
Dim myCommand As New OleDbCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Sub

Localhost simply refers to the machine you are on at the time (i.e. go to a
DOS prompt and type "ping localhost" you'll get 4 replies.

Looking at the code I am guessing it is connecting to some kind of SQL
Server/MSDE running on the computer so you don't supply a path to the data
file but instead give it the server name and the database name (initial
catalog).
James.
 
A

Armin Zingler

Familjen Karlsson said:
Here is an example from the help on the keword OleDbConnection, in
VB.Net, they don't give the path to the database just the word
localhost. How can it connect to the database without knowing where
it is. What does the keyword localhost do.

Please stay in the thread you've already started.
Thank's

Fia

Public Sub InsertRow(myConnectionString As String)
' If the connection string is null, use a default.
If myConnectionString = "" Then
myConnectionString = "Provider=SQLOLEDB;Data
Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"
End If
Dim myConnection As New OleDbConnection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID,
CompanyName) Values('NWIND', 'Northwind Traders')"
Dim myCommand As New OleDbCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Sub


Thanks for the example. As you see, the code uses an SQL server ("SQLOLEDB"
provider). It is not necessary to specify a path because the clients (you)
identify the database by it's name. According to the name, SQL server itself
knows where the database files are stored on the server. You (the client)
does not have to care about.

Your first question was about connecting to an Access database without
specifying a path. This is not possible. To connect to an Access database,
the Jet OleDB-Provider ist used. Access is not a database server, thus you
have to specify the path to the database file.


BTW, database related (language unrelated) question best fit into
microsoft.public.dotnet.framework.adonet


Armin
 
P

Paul Clement

¤ Here is an example from the help on the keword OleDbConnection, in VB.Net,
¤ they don't give the path to the database just the word localhost. How can it
¤ connect to the database without knowing where it is. What does the keyword
¤ localhost do.

You can use a DSN and ODBC but that's the wrong direction as far as I am concerned.

Access is a file based, not server based, database system. There is no equivalent to localhost for
Access.


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top