C × vs statement about connecting database

Posted by Spiz on Sun, 03 May 2020 19:59:22 +0200

Configuration of App.config: the database is connected to the ECS

<appSettings>
    <! -- whether the connection string is encrypted -- >
    <add key="ConStringEncrypt" value="false" />
    <! -- database connection string, (if encryption is used, the above item should be set to true; encryption tools can be downloaded from the official,
     If server=127.0.0.1;database =..., the above is set to false. >
    <!--<add key="ConnectionString" value="server=119.29.37.246 

;database=Serious;uid=sa;pwd=0377123456" />-->
    <add key="ConnectionString" value="server=120.79.183.167

;database=StoreHouse;uid=sa;pwd=BAILE@123456" />
    <! -- connection strings of other modules can be increased continuously so that the same project can support connection to mu lt iple databases. If not, delete the line -- >
    <!--<add key="ConnectionString2" value="server=.;database=Serious;uid=sa;pwd=123456" />-->
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>

//Database connection string (configured by web.config), multi - database can be implemented by DbHelperSQLP

 public static string connectionString = PubConstant.ConnectionString;  
 using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                }
            }

To get the connection statement of the database:

 public class PubConstant
    {        
        /// <summary>
        /// Get connection string
        /// </summary>
        public static string ConnectionString
        {           
            get 
            {
                string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];       
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                if (ConStringEncrypt == "true")
                {
                    _connectionString = DESEncrypt.Decrypt(_connectionString);
                }
                return _connectionString; 
            }
        }

        /// <summary>
        /// obtain web.config The database connection string for the configuration item in.
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (ConStringEncrypt == "true")
            {
                connectionString = DESEncrypt.Decrypt(connectionString);
            }
            return connectionString;
        }


    }

Topics: Database