繁体   English   中英

有没有一种快速的方法来更改 asp.net Visual Studio 中所有项目文件中的数据库连接名称?

[英]Is there a quick way to change the database connection name in all project files in asp.net Visual Studio?

有没有一种快速的方法来更改 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM