Very simple connection problem

Topics: Data Access Application Block
Mar 14, 2011 at 7:52 PM

Sirs, I made the most simple connection sample to try the data access application block.

But it appears I'm mading a obvius mistake and it's not working.

Can you please take a look for me and tell me wha'ts wrong?

VB.NET, on my form:

 Private Sub dbConnect()

  Dim db As SqlDatabase = New SqlDatabase("Data Source=myServer; Initial Catalog=EntLibQuickStarts;Integrated Security=SSPI;")
  If CType(db.ExecuteScalar(Data.CommandType.Text, "SELECT COUNT(*) FROM Customers"), Integer) = 0 Then
   Throw New Exception("Error")
   Exit Sub
  End If

 End Sub

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

If I run the program, on the line db.ExecuteScalar I receive an error: SqlException was unhandled: can't connect to SQL Server. Server not found...

MyServer is a valid SQL Server 2008 remote server.

If I try the code using regular SQLClient library, it works.

Regards, Ésio

Mar 15, 2011 at 1:37 AM

Are you sure you're passing the correct connection string?  Because right now, that's the only thing which I think could be the reason why you're encountering the exception.  Besides, that's what the exception really indicates.

In addition, the EntLib way of doing it is to define the connection string in the configuration file and use this code when creating a database:

Database database = EnterpriseLibraryContainer.Current.GetInstance<Database>(connectionStringName);

If you don't have a compelling need to not put the connection string on the config file, you should use this approach.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.