SQL connection fails

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

SQL connection fails

 
0
  #1
Jul 20th, 2008
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: SQL connection fails

 
0
  #2
Jul 21st, 2008
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 6
Reputation: sandeep_chalke is an unknown quantity at this point 
Solved Threads: 0
sandeep_chalke sandeep_chalke is offline Offline
Newbie Poster

Re: SQL connection fails

 
0
  #3
Jul 21st, 2008
Could you please post the exact error text?
Last edited by sandeep_chalke; Jul 21st, 2008 at 1:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

Re: SQL connection fails

 
0
  #4
Jul 21st, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: SQL connection fails

 
0
  #5
Jul 21st, 2008
The problem is your connection string. To connect to a SQL server Compact edition database you use the following:
  1. Data Source=MyData.sdf;Persist Security Info=False;

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.
  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/
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

Re: SQL connection fails

 
0
  #6
Jul 21st, 2008
When I use
  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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: SQL connection fails

 
0
  #7
Jul 21st, 2008
Maybe you don't have your Sql Server running? Try starting/restarting it using the surface area configuration manager or services.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 42
Reputation: Bill Purkins is an unknown quantity at this point 
Solved Threads: 1
Bill Purkins Bill Purkins is offline Offline
Light Poster

Re: SQL connection fails

 
0
  #8
Jul 21st, 2008
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)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC