gdsg

来源:互联网 发布:dbtg数据库 编辑:程序博客网 时间:2024/05/22 07:47

 

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;

namespace Common.File
{
    
public class DBConnect
    
{
        
public DBConnect() { }

        
private DBConnectInfo dbc;
        
private string encryName;
        
private string encryPwd;

        CryptoService cs 
= new CryptoService();
        
private void InitRoles()
        
{
            OleDbConnection con 
= new OleDbConnection();
            
string role = cs.Descrypt(encryName);
            
string pwd = cs.Descrypt(encryPwd);
            con.ConnectionString 
= "Provider=" + dbc.Provider + ";Initial Catalog=" + dbc.Database + ";Password=" + pwd + ";User ID=" + role + ";Data Source=" + dbc.DataSource;

            OleDbDataAdapter oda 
= new OleDbDataAdapter("Select role_id,role_pwd From sys_roles", con);
            
try
            
{
                con.Open();
                DataSet ds 
= new DataSet();
                oda.Fill(ds, 
"roles");
                
foreach (DataRow dr in ds.Tables["roles"].Rows)
                
{
                }

                con.Close();
                con.Dispose();
                oda.Dispose();

            }

            
catch (System.Exception e)
            
{
                Debug.Assert(
true, e.Message);
            }


        }

        
public OleDbConnection OleDbConnection(string loginrole,string as_Database,string as_DataSource)
        
{
            OleDbConnection dbcon 
= null;
            
string role = cs.Descrypt(this.encryName);
            
string pwd = cs.Descrypt(this.encryPwd);
            OleDbConnection con 
= new OleDbConnection();
            
string cstr = "Provider=" + dbc.Provider + ";Initial Catalog=" + dbc.Database + ";Password=" + pwd + ";User ID=" + role + ";Data Source=" + dbc.DataSource;
            con.ConnectionString 
= cstr;
            
string sql = "Select role_pwd From sys_roles Where role_id='" + loginrole + "'";
            OleDbCommand cmd 
= new OleDbCommand(sql, con);
            OleDbDataReader reader;
            
try
            
{
                con.Open();
                reader 
= cmd.ExecuteReader();
                
while (reader.Read())
                
{
                    pwd 
= reader.GetString(0);
                    pwd 
= Decrypt(pwd);
                    
break;
                }

                cstr 
= "Provider=" + dbc.Provider + ";Initial Catalog=" + dbc.Database + ";Password=" + pwd + ";User ID=" + loginrole + ";Data Source=" + dbc.DataSource;
                dbcon 
= new OleDbConnection(cstr);
            }

            
catch
            
{
                
return null;
            }

            
return dbcon;
        }

        
private string Decrypt(string pwd)
        
{
            
return cs.Descrypt(pwd);
        }


    }

}

原创粉丝点击