C#Vs关于连接数据库的语句

App.config的配置: 数据库连接的是云服务器

<appSettings>
    
    <add key="ConStringEncrypt" value="false" />
    
    
    <add key="ConnectionString" value="server=120.79.183.167

;database=StoreHouse;uid=sa;pwd=BAILE@123456" />
    
    
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  appSettings>

//数据库连接字符串(web.config来配置),多数据库可使用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;
                    }
                }
            }

得到数据库的连接语句的方法:

 public class PubConstant
    {        
        /// 
        /// 获取连接字符串
        /// 
        public static string ConnectionString
        {           
            get 
            {
                string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];       
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                if (ConStringEncrypt == "true")
                {
                    _connectionString = DESEncrypt.Decrypt(_connectionString);
                }
                return _connectionString; 
            }
        }

        /// 
        /// 得到web.config里配置项的数据库连接字符串。
        /// 
        /// 
        /// 
        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;
        }


    }

你可能感兴趣的:(c#)