Tools:
1. Visual Studio (I use vs2019)
2. SQL Server (I use sql2008)
3. Web page running framework:. Net2.0
catalogue
1, The web site connects to the database with windows authentication
2, The website connects to the database with sql server authentication
3, Use the tools to connect to the database that comes with Visual Studio
1, The web site connects to the database with windows authentication
1. The sql database is logged in in the form of Windows authentication;
2. Configure the connection in the Web.config configuration file, and add the following connection string in the < configuration > < / configuration > tag:
<connectionStrings> <add name="[Database connection string name--Decide for yourself]" connectionString="Data Source=[Server name];Initial Catalog=[Database name to connect to];Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> eg: <connectionStrings> <add name="sqlconstr" connectionString="Data Source=PC;Initial Catalog=BookShop;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <!--[Integrated Security=True]This code means to adopt Windows Authentication connection--> <!--[providerName="System.Data.SqlClient"]This code means that the type of database connected is sql server-->
3. Enter the following code in an executable
[the following two namespaces need to be referenced in the program code:
using System.Data.SqlClient;
using System.Data;
]
[the following code is used to test whether there is a link to the database]
SqlConnection sqlconstr = new SqlConnection();//The Connection object is instantiated. The object name is sqlconstr //Server = [server name], database = [name of the database you want to connect to] sqlconstr.ConnectionString="server=PC;database=BookShop;Integrated Security=True"; sqlconstr.Open();//Open database connection if(sqlconstr.State==ConnectionState.Open) { Response.Write("The database is already open"); } Sqlconstr.Close();//Close database connection
2, The website connects to the database with sql server authentication
1. sql login with sql server authentication.
For specific steps, refer to:
sql database authentication login
2. Enter the following code in an executable
[the following two namespaces need to be referenced in the program code:
using System.Data.SqlClient;
using System.Data;
]
[the following code is in reference format]
//The contents to be filled after "=" are in parentheses string connectionString ="server=[Server name];database=[The name of the database you want to connect to];uid=(The login name you created or existing can be used, but it should be the same as the login name in the first step);pwd=123456"((password you set); SqlConnection [Object name] = new SqlConnection(connectionString);//Object instantiation [Object name].Open();//open a connection //Fill content [Object name].Close();//Close connection
3, Use the tools to connect to the database that comes with Visual Studio
1. Open Visual Studio, open [tools], and select [connect to database]
2. We want to connect to the sql database, so our data source is Microsoft SQL Server in the red box (the following one needs to be connected through the database file), and our data provider is. NET Framework for SQL Server
3. You can choose to enter the server name yourself or select the small triangle in the small blue box on the right.
4. Select Windows authentication and select the database to connect to.
5. Select sql server authentication and select the database to connect. (first, the database should be logged in with sql server authentication. Secondly, the login name and password filled in this interface should be consistent with the login name and password of the database logged in with sql server authentication.)
6. Click test connection, the test connection is successful, and then click OK to connect.
7. Finally, such an interface will appear.