Connecting to a database

VonEhle

Active member
Joined
Feb 20, 2006
Messages
26
Programming Experience
Beginner
I am trying to do a little practicing. The following code was supplied from a book, but it doesn't work for me. I always get an error saying "login failed for GATEWAY/owner". I'm guessing it is ignoring the login info on the OdbcConnection.

VB.NET:
Expand Collapse Copy
Imports System.Data.SqlClient
Imports System.Convert
Public Class SQLPractice
Dim sqlconn As New SqlConnection("Data Source = .\SQLExpress; Initial Catalog = Pubs; Integrated Security = True")
Dim cn As New Odbc.OdbcConnection("dsn=pubs;uid=sa;pwd=admin;database=pubs")
Dim sqlcomm As New SqlCommand("select * from Authors", sqlconn)
Private Sub SQLPractice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQL.Click
Dim da As New SqlClient.SqlDataAdapter(sqlcomm)
Dim ds As New DataSet("Pubs")
da.Fill(ds)
dgv1.DataSource = ds.Tables(0)
End Sub
End Class
 
Well you're not using the OdbcConnection anywhere in your code (other than declaring it), so I guess it is "ignoring" the login info. But for SQL server, you should be using only the SqlConnection anyway. There's no need for two connections.
I believe the problem is that you're not opening the connection before calling the Fill method of the dataAdapter.
 
Specificaly, the problem is this part of the ConnectionString fro SQL Server: "Integrated Security = True" that tells ADO to use WINDOWS security to connect. Unless you've given your Windows login permissions to the server.... it will fail with the message you are getting. And if you have to ask, how do I know if my Windows login has permission? Then it's obvious that you haven't and either need to change the connection string to include a login... or learn how to manage the SQL Server and set permissions on it.

-tg
 
Back
Top