简体   繁体   中英

ConnectionString problem in C# database connection SQL Server 2005

I am a beginner in using Asp.NET with C# and it is my first time I am trying to establish a connection with an SQL server 2005 database:

Here is my simple code to connect to the sql server database,I am getting the text message set in the label. Is my problem in the connectionString ??? if so please show me examples how to write it and ow to get the server name and write it correctly ....or how to specify the database name (all path or just database name??)

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("server = Saher;Database=Database.mdf;integrated security = true");
            try{
                connection.Open();
            }
            catch{
                lblMessage.Text = "COULDN'T CONNECT to Stupid database";
            }finally{
                connection.Close();
            }



        }

Thanks,

The Connection string for SQL Server using a trusted connection should be as follows:

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI

myServerAddress can be the IP address of name of your server eg SQLSERVER01 or 192.168.1.5 myDataBase should be the actual database name not the MDF file eg Northwind

Change your code to this and post what lblMessage has in it:

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("server = Saher;Database=Database.mdf;integrated security = true");
            try{
                connection.Open();
            }
            catch(Exception ex)
            {
                lblMessage.Text = ex.Message;
            }finally{
                connection.Close();
            }

        }

Right now you are hiding the problem by putting "COULDN'T CONNECT to Stupid database" into your error label.

Others have already suggest better exception handling to get some infornmation.

Another approach: In addition you can use the "Add DataSource" wizard of Visual studio to find and connect to your database. You can do this in a dummy page or project. Then copy the resulting ConnectionString (from xx.config).

Check this Connection-Strings

Before all this, can you make it clear on which version of SQL server are you using and is the database file residing in the App_Data directory?

//Change the catch statement to get the error
//Original Code

catch{
  lblMessage.Text = "COULDN'T CONNECT to Stupid database";
}

//Change it into
catch(Exception ex){
  lblMessage.Text = ex.Message.ToString();
}

Thanks everyone! I think I had more than one problem here but let me show the solution so that no one will spend many hours connecting to an SQL server database!!

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ChurchApp\ChurchApplication\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        try{
            connection.Open();
            lblMessage.Text = "Connection Succeeded!";
        }
        catch(Exception ex){
            lblMessage.Text = ex.Message;
        }finally{
            connection.Close();
        }



    }

I had the @ sign missing before the connection string and I used to user the / instead to get over the error I get!!! (don't do that!!)

Get the connection string by right clicking on your database in the Server Explorer and Modify Connections and then ADVANCED .....copy everything from the advanced property you get or just the basic connection string found in the bottom of the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM