简体   繁体   中英

connecting to an SQL Server database

I've recently started programming asp.net with C# (using VS2008) and I wrote my first web application that connects to a database. First version worked ok but now there are some problems once I modify it. I'm giving the examples below which will depict the situation:

1) Works OK. Program connects to a database and uses a function DeleteAllRecords() to perform an action on it; important to note that I created the database to connect to in SQL Server Management Studio.

Code behind page of the button-click event handler:

    SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated Security=true");
    try
    {
        dbConnection.Open();
        dbConnection.ChangeDatabase("przemek8");
        SqlCommand myCommand = new SqlCommand("DELETE FROM table8", dbConnection);
        myCommand.ExecuteNonQuery();
    }
    catch (SqlException exception)
    {
        Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>");
    }
    dbConnection.Close();
} 

2) the second time I didn't use the database made in SQL SM Studio but I added a new database element from Visual Studio itself (Website -> Add New Item). I added some fields to that database and I also configured a GridView to show the database which is working. The problem, however, is that when I want to connect the Gridview to the database created before in SQL SM Studio, it doesn't work - when configuring the connection it won;t let choose the database file, saying:

You don't have permission to open this file. Contact the owner or an administrator to obtain permission.

It seems to me that the reason for that may be trivial but I cannot sort it out.

Just to note that all that database files were created SQL SM Studio in its default destination on disc C.

3) Not being able to connect with the GridView to the database created by SQL Server I continued working with the database added by Visual Studio itself. It was working with the GridView so I used the function to interact with it (delete all the records) - the same that was used at point 1) but with database now.

  SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename='D:\\WebSite1\\App_Data\\mydtb.mdf'; Integrated Security=true; User Instance=true");
    try
    {
        dbConnection.Open();
        dbConnection.ChangeDatabase("mydtb");
        SqlCommand myCommand = new SqlCommand("DELETE FROM Table1", dbConnection);
        myCommand.ExecuteNonQuery();
    }
    catch (SqlException exception)
    {
        Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>");
    }
    dbConnection.Close();

It does not connect to that one and the error message is:

Error code 911: Database 'mydtb' does not exist. Make sure that the name is entered correctly.

I'm new in this field, but should the data source in this case (connecting to the database created in Visual Studio) be Data Source=.\\\\SQLEXPRESS; as it is when the database is created in SQL Server Management Studio?

Thanks a lot for any help and suggestions! asp.net excited beginner:-)

The problem your having with regards to connecting to the database on a server is because of

SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated   Security=true");

The best way to do this is to go to your Web.config file and find the block and add a connection to your database in there.

eg

<add name="ConnectionString" connectionString="Data Source=YOUR SERVER;Initial Catalog=YOUR DATABASE;User ID=YOUR USER ID;Password=YOUR PASSWORD" />

then you can just call the connection string accross your whole project whenever you need to use it.

Also with regards to VS2012. There are very few companies using that IDE at the moment so your probably better off learning VS 2010 in the most part but i would agree that VS2008 is fairly out of date now

i think this will help

SqlConnection dbConnection = new SqlConnection("Data
  Source=.\\SQLEXPRESS;Integrated   Security=true; initial
  catalog=database name; uid=servername ; password=yourpassword");

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