开发者

ConnectionString problem in C# database connection SQL Server 2005

开发者 https://www.devze.com 2023-01-04 11:36 出处:网络
I 开发者_如何学Pythonam 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:

I 开发者_如何学Pythonam 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 e.g. SQLSERVER01 or 192.168.1.5 myDataBase should be the actual database name not the MDF file e.g 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消