none
Connecting to a local SQL database? RRS feed

  • 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
    Tuesday, January 12, 2010 1:34 PM

Answers

  • 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
    • Proposed as answer by Harry Zhu Tuesday, January 19, 2010 2:43 AM
    • Marked as answer by Harry Zhu Wednesday, January 20, 2010 6:45 AM
    Tuesday, January 12, 2010 2:20 PM

All replies

  • "Data Source=mupersan;Initial Catalog=DatabaseTest.sdf;Integrated Security=True" should work

    Tuesday, January 12, 2010 1:37 PM
  • http://www.connectionstrings.com

    That will show you how to setup connection strings for, virtually, any database type.
    public enum Answers { Everything = 0x2A }
    Tuesday, January 12, 2010 1:40 PM
  • "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)"

    ?
    Tuesday, January 12, 2010 1:43 PM
  • 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
    Tuesday, January 12, 2010 1:48 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"
    Tuesday, January 12, 2010 1:51 PM
  • Here you have connection strings:
    http://www.connectionstrings.com/
    Coder24.com
    Tuesday, January 12, 2010 1:51 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...
    Tuesday, January 12, 2010 1:55 PM
  • Here you have connection strings:
    http://www.connectionstrings.com/
    Coder24.com

    Thanks, I've tried it, but still the same error. See above.
    Tuesday, January 12, 2010 1:56 PM
  • 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.
    Tuesday, January 12, 2010 1:57 PM
  • 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?

    Tuesday, January 12, 2010 2:03 PM
  • Check this Link,

    I Hope this link will help you,

    http://www.connectionstrings.com/sql-server-2005-ce
    Happy Coding, RDRaja
    Tuesday, January 12, 2010 2:07 PM
  • 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
    Tuesday, January 12, 2010 2:08 PM
  • The menu option works in my Visual Studio.
    BTW, try to click on the "Refresh" button as well.
    Coder24.com
    Tuesday, January 12, 2010 2:10 PM
  • Coder24...are you trying to boost your post count or something by making as many posts as possible?  Try editing your post instead of making multiple consecutive posts!
    Tuesday, January 12, 2010 2:12 PM
  • Coder24...are you trying to boost your post count or something by making as many posts as possible?  Try editing your post instead of making multiple consecutive posts!

    No I am not trying to BOOST something!
    Coder24.com
    Tuesday, January 12, 2010 2:13 PM
  • 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...
    Tuesday, January 12, 2010 2:17 PM
  • 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
    • Proposed as answer by Harry Zhu Tuesday, January 19, 2010 2:43 AM
    • Marked as answer by Harry Zhu Wednesday, January 20, 2010 6:45 AM
    Tuesday, January 12, 2010 2:20 PM