简体   繁体   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?

Is there a quick way to change the database connection name in all project files in asp.net Visual Studio?有没有一种快速的方法来更改 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.可以参考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();
    }
}

Modify the database connection name:修改数据库连接名:

 SqlConnection conn = new SqlConnection()

To operate the database in the project:在项目中操作数据库:

  DBHelper helper = new DBHelper()

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

相关问题 在Visual Studio中运行Asp.NET项目时出现数据库错误 - DataBase Error while running Asp.NET project in Visual Studio Visual Studio中使用ASP.NET Web API项目的Angular项目 - Angular project with ASP.NET Web API project in Visual Studio 如何在Visual Studio 2013中编辑ASP.Net MVC C#项目模板文件? - How can I edit ASP.Net MVC C# project template files in Visual Studio 2013? 如何在Visual Studio(C#)中将ASP.NET项目与SQL Server数据库表连接 - How to connect ASP.NET project with SQL Server database table in Visual Studio (C#) 在ASP.NET MVC中的Visual Studio 2013中调用“全部中断”时,是否可以隐藏“源不可用”选项卡? - Is there a way to hide the “Source Not Available” tab when invoking “Break All” in Visual Studio 2013 in ASP.NET MVC? 在asp.net中集成Xaml的快速方法? - Quick way to integrate Xaml in asp.net? Visual Studio-无法启动ASP.NET Core项目 - Visual Studio - Cannot start ASP.NET Core project Visual Studio会忽略我在ASP.net项目中的语法错误 - Visual Studio ignores my syntax errors in ASP.net project 如何运行连接 Visual Studio 和 github 的 ASP.NET 项目 - How to run ASP.NET project connecting Visual Studio with github 如何在Visual Studio 2015中创建ASP.NET MVC 4项目 - How to Create an ASP.NET MVC 4 project in visual studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM