150 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			150 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Configuration;
 | |
| using System.Windows.Forms;
 | |
| using System.Data.SqlClient;
 | |
| using System.Data;
 | |
| using System.Security.Cryptography;  
 | |
| 
 | |
| namespace DeiNiu.Utils
 | |
| {
 | |
|     /// <summary>  
 | |
|     /// SQL数据库服务器配置静态类  
 | |
|     /// </summary>  
 | |
|     public class ConntionConfig
 | |
|     {
 | |
|        public static string connString = "SqlconnectionString";
 | |
|         /// <summary>  
 | |
|         /// 检查配置信息  
 | |
|         /// </summary>  
 | |
|         /// <returns>完整有效返回true,无效则启动配置界面</returns>  
 | |
|         public static bool CheckConntionConfig(string conn=null)
 | |
|         {
 | |
|             if (CheckedConnection(conn))
 | |
|             {
 | |
|                 return true;
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 return false;// CheckedConfig();
 | |
|             }
 | |
|         }
 | |
|         /*
 | |
|         /// <summary>  
 | |
|         /// 验证配置信息  
 | |
|         /// </summary>  
 | |
|         private static bool CheckedConfig()
 | |
|         {
 | |
|             MessageBox.Show("数据库服务器无法连接,请重新配置。",
 | |
|                    "系统提示",
 | |
|                    MessageBoxButtons.OK,
 | |
|                    MessageBoxIcon.Warning);
 | |
| 
 | |
|             SvrConf svrConf = new SvrConf();
 | |
|             svrConf.ShowDialog();
 | |
| 
 | |
|             if (MessageBox.Show("是否现在进入系统?", "询问",
 | |
|                 MessageBoxButtons.YesNo,
 | |
|                 MessageBoxIcon.Question) == DialogResult.Yes)
 | |
|             {
 | |
|                 return CheckConntionConfig();
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 return false;
 | |
|             }
 | |
|         }
 | |
|         */
 | |
|         /// <summary>  
 | |
|         /// 验证配置信息的数据库连接  
 | |
|         /// </summary>  
 | |
|         private static bool CheckedConnection(string conn=null)
 | |
|         {
 | |
|             string connectionString = string.IsNullOrEmpty(conn) ? ConString() : conn;
 | |
|             return !string.IsNullOrEmpty(connectionString) &&
 | |
|                    TestConntion(connectionString);
 | |
|         }
 | |
| 
 | |
|         /// <summary>  
 | |
|         /// 测试与服务器数据库是否成功连接  
 | |
|         /// </summary>  
 | |
|         /// <param name="connectionString">数据库连接字符串</param>  
 | |
|         /// <returns></returns>  
 | |
|         public static bool TestConntion(string connectionString)
 | |
|         {
 | |
|        
 | |
|             using (SqlConnection conn = new SqlConnection(connectionString))
 | |
|             {
 | |
|                 try
 | |
|                 {
 | |
|                     if (conn.State == ConnectionState.Open)
 | |
|                         conn.Close();
 | |
|                     conn.Open();
 | |
|                     pickConstants.SQL_CONN_WMS = connectionString;//set con in memory
 | |
|                     return true;
 | |
|                 }
 | |
|                 catch
 | |
|                 {
 | |
|                     return false;
 | |
|                 }
 | |
|                 finally
 | |
|                 {
 | |
|                     conn.Close();
 | |
|                     conn.Dispose();
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>  
 | |
|         /// 数据库连接字符串,自动判断加密状态并获取对应连接字符串。  
 | |
|         /// </summary>  
 | |
|         /// <returns></returns>  
 | |
|         public static string ConString()
 | |
|         {
 | |
|             
 | |
|             if (IsEncrypt())
 | |
|             {
 | |
|                 string cstr = ConfigurationManager.AppSettings[connString];
 | |
|                 try
 | |
|                 {
 | |
|                     //解密后的数据库连接字符串  
 | |
|                     cstr = DESEncrypt.Decrypt(cstr);
 | |
|                     return cstr;
 | |
|                 }
 | |
|                 catch
 | |
|                 {
 | |
|                     //如果勾选加密,第一次登陆时取到的是未加密连接字符串,所以直接返回。如果返回空字符串会导致首次登陆失败。  
 | |
|                     //ConfigurationManager.AppSettings["ConnectionString"] = string.Empty;  
 | |
|                     //return string.Empty;  
 | |
|                     return cstr;
 | |
|                 }
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 //无须解密的数据库连接字符串  
 | |
|                 return ConfigurationManager.AppSettings[connString];
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         /// <summary>  
 | |
|         /// 验证是否已加密  
 | |
|         /// </summary>  
 | |
|         /// <returns></returns>  
 | |
|         internal static bool IsEncrypt()
 | |
|         { 
 | |
|             switch (ConfigurationManager.AppSettings["ConStringEncrypt"])
 | |
|             {
 | |
|                 case "1":
 | |
|                 case "TRUE":
 | |
|                 case "true":
 | |
|                     return true;
 | |
|                 default:
 | |
|                     return false;
 | |
|             }
 | |
|         }
 | |
|     }  
 | |
| }
 |