有没有一种快速的方法可以更改asp.net Visual Studio中所有项目文件中的数据库连接名称?
您可以参考jhambright提供的链接。在项目中,封装在数据库上操作的实体类更为常见。
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();
}
}
修改数据库连接名称:
SqlConnection conn = new SqlConnection()
在项目中操作数据库:
DBHelper helper = new DBHelper()