连接数据库与角色相关简单实现
2008-02-28 14:07
661 查看
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);
}
}
}
2---
using System;
using System.Collections.Generic;
using System.Text;
namespace Common.File
...{
public class DBConnectInfo
...{
Fields#region Fields
private string _provider;
private string _database;
private string _datasource;
private string _role;
#endregion
Properties#region Properties
public string Provider
...{
get
...{
return _provider;
}
set
...{
_provider = value;
}
}
public string Database
...{
get
...{
return _database;
}
set
...{
_database = value;
}
}
public string DataSource
...{
get
...{
return _datasource;
}
set
...{
_datasource = value;
}
}
public string Role
...{
get
...{
return _role;
}
set
...{
_role = value;
}
}
#endregion
Methods#region Methods
#endregion
}
}
相关文章推荐
- 关于c#数据库的简单应用-datagriview连接数据库及更相关操作
- 简单封装sqlite3 实现实体对象与关系型数据库的相关操作【IOS】
- C语言连接到数据库实现一个具有简单的client端功能
- 通过动态代理(Proxy)实现的数据库连接池的创建连接与归还链接的操作的简单的实现流程
- 连接数据库及数据查询的简单实现方法
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- 简单调用VS自带的数据源选择对话框实现数据库连接字符串配置
- Struts2+Hibernate实现增、删、查、改的简单网页(连接数据库)
- 5.页面用ajax实现简单的连接数据库
- Java实现JDBC连接数据库简单案例
- Eclipse使用(七)—— 使用Eclipse创建JavaWeb项目并使用JDBC连接数据库实现简单的登陆注册功能
- 简单调用VS自带的数据源选择对话框实现数据库连接字符串配置
- 通过VS中的数据源选择对话框简单实现数据库连接配置
- 新建web项目,并连接到Oracle数据库,实现简单的控制台输出数据库中的表记录
- jsp+servlet+ajax+oracle数据库-实现简单的登陆、注册、找回密码功能(与Oralce数据库连接)
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- 简单实现jdbc连接数据库工具类
- ajax 登录功能简单实现(未连接数据库)
- 通过VS中的数据源选择对话框简单实现数据库连接配置[图]
- c#实现简单工厂—数据库连接