Is there a quick way to change the database connection name in all project files in asp.net Visual Studio?
You can refer to the link provided by jhambright.In a project, it is more common to encapsulate entity classes that operate on the database.
public class DBHelper
{
SqlConnection connection = new SqlConnection();//The object connection responsible for connecting to the data
SqlCommand command = new SqlCommand();//Object command responsible for executing SQL statements
string connectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
/// <summary>
/// Connect to the database
/// </summary>
private void ConnectDataBase()
{
connection.ConnectionString = connectionString; //Specify the connection string
if(connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
}
/// <summary>
/// SQL statement to be executed
/// </summary>
/// <param name="command"></param>
public void ExecuteSQL(string commandString)
{
ConnectDataBase();//Connect to the database
command.Connection = connection;//Specify the connection
command.CommandText = commandString;//Specify the SQL command to be operated
command.ExecuteNonQuery();
CloseConnection();//Close the database connection
}
/// <summary>
/// Close the existing database connection
/// </summary>
private void CloseConnection()
{
connection.Close();
}
}
Modify the database connection name:
SqlConnection conn = new SqlConnection()
To operate the database in the project:
DBHelper helper = new DBHelper()
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.