asp.net create class file

When using Microsoft Visual Studio 2005 development tools to develop an asp.net website, it is indispensable to deal with the database (taking Microsoft SQL Server 2000 database server as an example). We are used to writing the code for connecting to the database in a class file, and when using it directly Just call this class, which is easier to maintain in the future! So how to create this kind of file and how to call this kind of method?
1. Open the Microsoft Visual Studio 2005 development tool, take the new website webtest as an example, find the solution explorer on the right, right click webtest, add a new item, select the class file, the name is DB.cs, as shown in the following figure:
Insert picture description here
Edit DB.cs file:

添加1: using System.Data.SqlClient;

Add 2:

public static SqlConnection lianjie() {

    SqlConnection con = new SqlConnection("server=.;database=databasename;uid=sa;pwd=123456");

    return con;



}

Ctrl+s save the code.

As shown in the red border in the following figure:
Insert picture description here
Take the login page Login.aspx as an example to see how to call this method?

Create a new Login.aspx page: as shown below
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41661800/article/details/105373068