Answered by:
Connecting to a local SQL database?

Question
-
Hi,
I have created a local database by adding a new item to my project. So far the code looks like this:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace DatabaseTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SqlConnection con; private void Form1_Load(object sender, EventArgs e) { con = new SqlConnection(); string conStr = "Data Source=mupersan; Initial Catalog=DatabaseTest.sdf;"; con.ConnectionString = conStr; con.Open(); MessageBox.Show("Forbindelsen er åben!"); con.Close(); MessageBox.Show("Forbindelsen er lukket"); } } }
Now, I have big problems with getting the connection string right. I have tried different combinations, but nothing works. Can you give me an example of a working connection string in my case?
Regards
Anders
Answers
All replies
-
-
-
"Data Source=mupersan;Initial Catalog=DatabaseTest.sdf;Integrated Security=True" should work
It casts an exception error when the Con.Open method is invoked:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
? -
http://www.connectionstrings.com
That will show you how to setup connection strings for, virtually, any database type.
public enum Answers { Everything = 0x2A }
I went to the website and used the standard the standard connection string for SQl Server Compact Edition (I'm running SQL Server 2008 Express) and still the same error (see above).- Edited by mupersan82 Tuesday, January 12, 2010 1:57 PM
-
This should do something for you
Server =mupersan\SQLExpress; AttachDbFilename =c:\asd\qwe\mydbfile.mdf; Database =dbname; Trusted_Connection =Yes;
Dewayne Dodd - Landshark Software "Please make sure to 'Mark As Answer' if this answer solves your question"- Edited by LandsharkDaddy Tuesday, January 12, 2010 2:04 PM
-
-
This should do something for you
Server =.\SQLExpress; AttachDbFilename =c:\asd\qwe\mydbfile.mdf; Database =dbname; Trusted_Connection =Yes;
Dewayne Dodd - Landshark Software "Please make sure to 'Mark As Answer' if this answer solves your question"
I tried to configure your connection string, but it doesn't recognize the "\" as correct escape sequences? Why is it so hard to connect to a database? Lol... -
Here you have connection strings:
http://www.connectionstrings.com/
Coder24.com
Thanks, I've tried it, but still the same error. See above. -
hmm? Why do you not use the regular SQL connection tools in
Visual Studio?
Like this:
More here: http://social.msdn.microsoft.com/Forums/en/csharpide/thread/da36b0f9-ff7a-4da2-95cc-f36626317b51
You can also choose add connection from the menu!
Coder24.com- Edited by Harry Zhu Monday, January 18, 2010 6:59 AM I'm merging post for better viewing.
-
Like this:
More here: http://social.msdn.microsoft.com/Forums/en/csharpide/thread/da36b0f9-ff7a-4da2-95cc-f36626317b51
Coder24.com
The option "Create New SQL Server Database" is greyed out on my screen? -
Check this Link,I Hope this link will help you,http://www.connectionstrings.com/sql-server-2005-ce
Happy Coding, RDRaja -
Like this:
More here: http://social.msdn.microsoft.com/Forums/en/csharpide/thread/da36b0f9-ff7a-4da2-95cc-f36626317b51
Coder24.com
The option "Create New SQL Server Database" is greyed out on my screen?
Check the c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG for error information also, try to see if the SQL Server Service it turned on,
also check some settings, BTW, try to restart Visual Studio!
BTW, which VS do you have?
Coder24.com -
-
-
-
Hi.
Please refer to this post.
The answer is using a UDL file:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7b2e1bc2-72d7-488d-8a18-7837bb9dd46b
Look for my second answer there.
Noam B
_________________________________________________________
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you... -
Please read this msdn blog; it is about trouble shooting connectivity with SQL Express:
http://blogs.msdn.com/sql_protocols/archive/2006/03/23/558651.aspx