943,959 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 6113
  • VB.NET RSS
Jul 20th, 2008
0

SQL connection fails

Expand Post »
Hi, using VB 2008 Express, I have a button on a form which is to use a datareader to loop through all the records in a file. When I try to run it I get a message indicating it could not connect. I assume something is wrong with my connection string but I don't know what. I know the path name is correct, and I know my database does not require a password. I have tried it with and without USER ID and PASSWORD. I don;t know where to look next. Any help is appreciated.
Thanks,
Bill P.
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. Dim sConnection As String = "server=(local);uid=test;pwd=PassWord;database=C:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2008\Projects\Medications\Medications\medications.sdf"
  4.  
  5. Dim objCommand As New SqlCommand
  6. objCommand.CommandText = "Select doctor From Doctors"
  7. objCommand.Connection = New SqlConnection(sConnection)
  8. objCommand.Connection.Open()
  9.  
  10. Dim objDataReader As SqlDataReader = objCommand.ExecuteReader()
  11.  
  12. If objDataReader.HasRows Then
  13. Do While objDataReader.Read()
  14. Console.WriteLine(" Your name is: " & objDataReader(0))
  15. Loop
  16. Else
  17. Console.WriteLine("No rows returned.")
  18. End If
  19.  
  20. objDataReader.Close()
  21. objCommand.Dispose()
  22.  
  23.  
  24.  
  25. End Sub
  26. End Class
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008
Jul 21st, 2008
0

Re: SQL connection fails

Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jul 21st, 2008
0

Re: SQL connection fails

Could you please post the exact error text?
Last edited by sandeep_chalke; Jul 21st, 2008 at 1:09 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sandeep_chalke is offline Offline
6 posts
since Aug 2007
Jul 21st, 2008
0

Re: SQL connection fails

I find my connection string saved in my application configuration file, but when I refer to it by name I get this error message:

Error 1 Reference to a non-shared member requires an object reference.

Here is the app.config entry:
VB.NET Syntax (Toggle Plain Text)
  1. <connectionStrings>
  2. <add name="WindowsApplication1.My.MySettings.medicationsConnectionString"
  3. connectionString="Data Source=|DataDirectory|\medications.sdf"
  4. providerName="Microsoft.SqlServerCe.Client.3.5" />
  5. </connectionStrings>
Thanks,
Bill P.
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008
Jul 21st, 2008
0

Re: SQL connection fails

The problem is your connection string. To connect to a SQL server Compact edition database you use the following:
VB.NET Syntax (Toggle Plain Text)
  1. Data Source=MyData.sdf;Persist Security Info=False;

Quote ...
How to specify the location of the SDF file
Often times the .SDF database is not running in the current directory so it becomes necessary to programatically set the path to the SDF file. This is an example (.net C#) on how to do this when the SDF file is located in the same directory as the executing application.
VB.NET Syntax (Toggle Plain Text)
  1. Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyData.sdf;Persist Security Info=False;


Here you get more info about how to use the App.config information in your application
Last edited by ChaseVoid; Jul 21st, 2008 at 1:51 pm. Reason: Added website : http://www.codeguru.com/columns/DotNet/article.php/c7987/
Reputation Points: 40
Solved Threads: 12
Junior Poster
ChaseVoid is offline Offline
116 posts
since Aug 2007
Jul 21st, 2008
0

Re: SQL connection fails

When I use
VB.NET Syntax (Toggle Plain Text)
  1. Data Source=MyData.sdf;Persist Security Info=False;Data Source=MyData.sdf;Persist Security Info=False;
I get error:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008
Jul 21st, 2008
0

Re: SQL connection fails

Maybe you don't have your Sql Server running? Try starting/restarting it using the surface area configuration manager or services.
Reputation Points: 40
Solved Threads: 12
Junior Poster
ChaseVoid is offline Offline
116 posts
since Aug 2007
Jul 21st, 2008
0

Re: SQL connection fails

SQL Server Browser was stopped. I started it. Now I get:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Reputation Points: 10
Solved Threads: 1
Light Poster
Bill Purkins is offline Offline
42 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Errorhandler vs. Msgbox for errors - preference?
Next Thread in VB.NET Forum Timeline: Graph controls





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC