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,
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!!
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.