首页上一页 1 下一页尾页 2 条记录 1/1页
我把书上的代码融合了一下,作了一个类文件,但出错了,请问这是什么原因呀?
发表在C#图书答疑
2010-07-11
是否精华
是
否
版块置顶:
是
否
[font color=#FF0000][strong]红色是出错的地方,应该怎么写才可以在类中实现读取加密的ini文件[/strong][/font]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices; //读写ini文件用
using System.Security.Cryptography; //加密解密用
using System.IO; //MemoryStream文件流用
using cls_encrypt;
namespace IT部门管理系统
{
class cls_dbOpSql
{
//定义ini文件路径 [font color=#FF0000][strong]类中用Application调用不了这个ini文件,请问要用什么语句才可调用到相对路径呀?
[/strong[/font]]
private string FFileName = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\") + 1) + "dataCon.ini";
[DllImport("kernel32")] //导入dll
private static extern int GetPrivateProfileString( //定义私有方法 取得ini文件
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName);
public string ReadString(string section, string key, string def) //定义读取ini文件的方法
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, temp, 1024, FFileName); //调用私有方法 取得ini文件
return temp.ToString();
}
[DllImport("kernel32")] //导入dll
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); //定义写入ini文件的方法
string DSource = ReadString("Server", "Name", "");
string DBase = ReadString("DataBase", "Name", "");
string DUid = ReadString("userid", "Name", "");
string DPwd = ReadString("userpass", "Name", "");
cls_encryptString dbEnc = new cls_encryptString(); //调用加密解密类
[font color=#FF0000][strong]下面这句赋值,好像也有问题,请问要怎么改才可以呀?[/strong][/font]
string sql_str= "Server=" + dbEnc.Descryp(DSource) + ";User Id=" + dbEnc.Descryp(DUid) + ";Pwd=" + dbEnc.Descryp(DPwd) + ";DataBase=" + dbEnc.Descryp(DBase) + "";
#region 全局变量
public static string Login_ID = ""; //定义全局变量,记录当前登录的用户编号
public static string Login_Name = ""; //定义全局变量,记录当前登录的用户名
public static string Mean_SQL = "", Mean_Table = "", Mean_Field = ""; //定义全局变量,记录“基础信息”各窗体中的表名及SQL语句
public static SqlConnection My_con; //定义一个SqlConnection类型的公共变量My_con,用于判断数据库是否连接成功
public static string M_str_sqlcon = sql_str;
public static int Login_n = 0; //用户登录与重新登录的标识
public static string AllSql = "Select * from tb_Stuffbusic"; //存储职工基本信息表中的SQL语句
//public static int res = 0;
#endregion
#region 建立数据库连接
/// <summary>
/// 建立数据库连接.
/// </summary>
/// <returns>返回SqlConnection对象</returns>
public static SqlConnection getcon()
{
My_con = new SqlConnection(M_str_sqlcon); //用SqlConnection对象与指定的数据库相连接
My_con.Open(); //打开数据库连接
return My_con; //返回SqlConnection对象的信息
}
#endregion
#region 测试数据库是否赋加
/// <summary>
/// 测试数据库是否赋加
/// </summary>
public void con_open()
{
getcon();
//con_close();
}
#endregion
#region 关闭数据库连接
/// <summary>
/// 关闭于数据库的连接.
/// </summary>
public void con_close()
{
if (My_con.State == ConnectionState.Open) //判断是否打开与数据库的连接
{
My_con.Close(); //关闭数据库的连接
My_con.Dispose(); //释放My_con变量的所有空间
}
}
#endregion
#region 读取指定表中的信息
/// <summary>
/// 读取指定表中的信息.
/// </summary>
/// <param name="SQLstr">SQL语句</param>
/// <returns>返回bool型</returns>
public SqlDataReader getcom(string SQLstr)
{
getcon(); //打开与数据库的连接
SqlCommand My_com = My_con.CreateCommand(); //创建一个SqlCommand对象,用于执行SQL语句
My_com.CommandText = SQLstr; //获取指定的SQL语句
SqlDataReader My_read = My_com.ExecuteReader(); //执行SQL语名句,生成一个SqlDataReader对象
return My_read;
}
#endregion
#region 执行SqlCommand命令
/// <summary>
/// 执行SqlCommand
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
public void getsqlcom(string SQLstr)
{
getcon(); //打开与数据库的连接
SqlCommand SQLcom = new SqlCommand(SQLstr, My_con); //创建一个SqlCommand对象,用于执行SQL语句
SQLcom.ExecuteNonQuery(); //执行SQL语句
SQLcom.Dispose(); //释放所有空间
con_close(); //调用con_close()方法,关闭与数据库的连接
}
#endregion
#region 创建DataSet对象
/// <summary>
/// 创建一个DataSet对象
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
/// <param name="M_str_table">表名</param>
/// <returns>返回DataSet对象</returns>
public DataSet getDataSet(string SQLstr, string tableName)
{
getcon(); //打开与数据库的连接
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con); //创建一个SqlDataAdapter对象,并获取指定数据表的信息
DataSet My_DataSet = new DataSet(); //创建DataSet对象
SQLda.Fill(My_DataSet, tableName); //通过SqlDataAdapter对象的Fill()方法,将数据表信息添加到DataSet对象中
con_close(); //关闭数据库的连接
return My_DataSet; //返回DataSet对象的信息
//WritePrivateProfileString(string section, string key, string val, string filePath);
}
#endregion
}
}
[font color=#FF0000]
[strong]//以下是cls_EncrypString类文件代码[/strong][/font]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography; //加密解密用
using System.IO; //MemoryStream文件流用
namespace 从ini文件中读取数据库连接
{
class cls_EncrypString
{
public string Encryp(string what)
{
string encryptKey = "secr";//加密key
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(what.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Convert.ToBase64String(MStream.ToArray());
}
catch
{
MessageBox.Show("加密失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return string.Empty;
}
}
public string Descryp(string what) //有返回值的方法
{
string encryptKey = "secr";//解密key
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Convert.FromBase64String(what.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Encoding.Unicode.GetString(MStream.ToArray());
}
catch
{
MessageBox.Show("解密失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return string.Empty;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices; //读写ini文件用
using System.Security.Cryptography; //加密解密用
using System.IO; //MemoryStream文件流用
using cls_encrypt;
namespace IT部门管理系统
{
class cls_dbOpSql
{
//定义ini文件路径 [font color=#FF0000][strong]类中用Application调用不了这个ini文件,请问要用什么语句才可调用到相对路径呀?
[/strong[/font]]
private string FFileName = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\") + 1) + "dataCon.ini";
[DllImport("kernel32")] //导入dll
private static extern int GetPrivateProfileString( //定义私有方法 取得ini文件
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName);
public string ReadString(string section, string key, string def) //定义读取ini文件的方法
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, temp, 1024, FFileName); //调用私有方法 取得ini文件
return temp.ToString();
}
[DllImport("kernel32")] //导入dll
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); //定义写入ini文件的方法
string DSource = ReadString("Server", "Name", "");
string DBase = ReadString("DataBase", "Name", "");
string DUid = ReadString("userid", "Name", "");
string DPwd = ReadString("userpass", "Name", "");
cls_encryptString dbEnc = new cls_encryptString(); //调用加密解密类
[font color=#FF0000][strong]下面这句赋值,好像也有问题,请问要怎么改才可以呀?[/strong][/font]
string sql_str= "Server=" + dbEnc.Descryp(DSource) + ";User Id=" + dbEnc.Descryp(DUid) + ";Pwd=" + dbEnc.Descryp(DPwd) + ";DataBase=" + dbEnc.Descryp(DBase) + "";
#region 全局变量
public static string Login_ID = ""; //定义全局变量,记录当前登录的用户编号
public static string Login_Name = ""; //定义全局变量,记录当前登录的用户名
public static string Mean_SQL = "", Mean_Table = "", Mean_Field = ""; //定义全局变量,记录“基础信息”各窗体中的表名及SQL语句
public static SqlConnection My_con; //定义一个SqlConnection类型的公共变量My_con,用于判断数据库是否连接成功
public static string M_str_sqlcon = sql_str;
public static int Login_n = 0; //用户登录与重新登录的标识
public static string AllSql = "Select * from tb_Stuffbusic"; //存储职工基本信息表中的SQL语句
//public static int res = 0;
#endregion
#region 建立数据库连接
/// <summary>
/// 建立数据库连接.
/// </summary>
/// <returns>返回SqlConnection对象</returns>
public static SqlConnection getcon()
{
My_con = new SqlConnection(M_str_sqlcon); //用SqlConnection对象与指定的数据库相连接
My_con.Open(); //打开数据库连接
return My_con; //返回SqlConnection对象的信息
}
#endregion
#region 测试数据库是否赋加
/// <summary>
/// 测试数据库是否赋加
/// </summary>
public void con_open()
{
getcon();
//con_close();
}
#endregion
#region 关闭数据库连接
/// <summary>
/// 关闭于数据库的连接.
/// </summary>
public void con_close()
{
if (My_con.State == ConnectionState.Open) //判断是否打开与数据库的连接
{
My_con.Close(); //关闭数据库的连接
My_con.Dispose(); //释放My_con变量的所有空间
}
}
#endregion
#region 读取指定表中的信息
/// <summary>
/// 读取指定表中的信息.
/// </summary>
/// <param name="SQLstr">SQL语句</param>
/// <returns>返回bool型</returns>
public SqlDataReader getcom(string SQLstr)
{
getcon(); //打开与数据库的连接
SqlCommand My_com = My_con.CreateCommand(); //创建一个SqlCommand对象,用于执行SQL语句
My_com.CommandText = SQLstr; //获取指定的SQL语句
SqlDataReader My_read = My_com.ExecuteReader(); //执行SQL语名句,生成一个SqlDataReader对象
return My_read;
}
#endregion
#region 执行SqlCommand命令
/// <summary>
/// 执行SqlCommand
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
public void getsqlcom(string SQLstr)
{
getcon(); //打开与数据库的连接
SqlCommand SQLcom = new SqlCommand(SQLstr, My_con); //创建一个SqlCommand对象,用于执行SQL语句
SQLcom.ExecuteNonQuery(); //执行SQL语句
SQLcom.Dispose(); //释放所有空间
con_close(); //调用con_close()方法,关闭与数据库的连接
}
#endregion
#region 创建DataSet对象
/// <summary>
/// 创建一个DataSet对象
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
/// <param name="M_str_table">表名</param>
/// <returns>返回DataSet对象</returns>
public DataSet getDataSet(string SQLstr, string tableName)
{
getcon(); //打开与数据库的连接
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con); //创建一个SqlDataAdapter对象,并获取指定数据表的信息
DataSet My_DataSet = new DataSet(); //创建DataSet对象
SQLda.Fill(My_DataSet, tableName); //通过SqlDataAdapter对象的Fill()方法,将数据表信息添加到DataSet对象中
con_close(); //关闭数据库的连接
return My_DataSet; //返回DataSet对象的信息
//WritePrivateProfileString(string section, string key, string val, string filePath);
}
#endregion
}
}
[font color=#FF0000]
[strong]//以下是cls_EncrypString类文件代码[/strong][/font]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography; //加密解密用
using System.IO; //MemoryStream文件流用
namespace 从ini文件中读取数据库连接
{
class cls_EncrypString
{
public string Encryp(string what)
{
string encryptKey = "secr";//加密key
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(what.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Convert.ToBase64String(MStream.ToArray());
}
catch
{
MessageBox.Show("加密失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return string.Empty;
}
}
public string Descryp(string what) //有返回值的方法
{
string encryptKey = "secr";//解密key
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Convert.FromBase64String(what.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Encoding.Unicode.GetString(MStream.ToArray());
}
catch
{
MessageBox.Show("解密失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return string.Empty;
}
}
}
}