简体   繁体   中英

How to open a database in asp.net web forms?

I need to open a pre-existing Database in ASP.NET Web Form with C#. So far all I have managed to find, are tutorial's on how to make a new Database and Table's.

If anyone know's how to do this please let me know.

Thanks'

  1. Open a connection to the database. You will need the proper connection string. The place to go is ConnectionStrings.com .

    SqlConnection conn = new SqlConnection(yourConnectionString);

  2. Create a command

     string yourSQL = "SELECT FirstName FROM Employees"; 

    SqlCommand cmd = new SqlCommand(yourSQL, conn);

  3. Do something with it..

    • execute a DataReader
    • fill a table with a DataAdapter

That's the basic pattern.

Add a connection string to web.config and then Write the connection code.

The following is a good place to start for the conn string and the code: https://www.connectionstrings.com/sql-server-2012/

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