📄 dbclass.cs
字号:
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.Security.Cryptography;
using System.Net;
using System.Drawing;
using System.Text.RegularExpressions;
//该源码下载自www.51aspx.com(51aspx.com)
namespace DBLibrary
{
public class DBClass : IDisposable
{
public SqlConnection conn;
private string ConnName;
private string ConnString = "";
public DBClass()
{
ConnString = System.Configuration.ConfigurationManager.AppSettings["ConnString"].ToString();
//conn = new SqlConnection(ConnString);
}
public DBClass(string conn_str)
{
if (conn_str != null && conn_str.Trim() != "")
{
ConnName = conn_str.Trim();
}
ConnString = System.Configuration.ConfigurationManager.AppSettings[ConnName].ToString();
//conn = new SqlConnection(ConnString);
}
public void Dispose()
{
if (conn != null)
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Dispose();
conn = null;
}
//conn = null;
}
/// <summary>
/// 打开数据库
/// </summary>
/// <returns>成功或者失败</returns>
public bool Open()
{
if (ConnString == null || ConnString.Trim()=="")
{
return false;
}
try
{
if (conn.State == ConnectionState.Closed)
{
conn = new SqlConnection(ConnString);
conn.Open();
}
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 关闭数据库
/// </summary>
public void Close()
{
if (conn != null)
{
conn.Close();
}
conn.Dispose();
}
/// <summary>
/// 运行储存过程
/// </summary>
/// <param name="ProcedureName">储存过程的名称</param>
/// <returns>返回值</returns>
public int RunProcedureForInt(string ProcedureName)
{
using(SqlConnection cn = new SqlConnection(ConnString))
{
int ret = -1;
try
{
SqlCommand Sqlcmd = new SqlCommand(ProcedureName, cn);
Sqlcmd.CommandType = CommandType.StoredProcedure;
Sqlcmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cn.Open();
Sqlcmd.ExecuteNonQuery();
ret = (int)Sqlcmd.Parameters["ReturnValue"].Value;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cn.Close();
}
return ret;
}
}
/// <summary>
/// 运行没有参数的过程
/// </summary>
/// <param name="ProcedureName">proc name</param>
/// <param name="SqlReader">out sqldatareader</param>
public void RunProcForDataReader(string ProcedureName, out SqlDataReader SqlReader)
{
RunProcForDataReader(ProcedureName, null, out SqlReader);
}
/// <summary>
/// 运行过程,返回空
/// </summary>
/// <param name="ProcedureName">过程名</param>
/// <param name="SqlPrams">参数名数组</param>
/// <param name="SqlReader">输出Datareader结果</param>
public void RunProcForDataReader(string ProcedureName, SqlParameter[] SqlPrams, out SqlDataReader SqlReader)
{
try
{
SqlConnection cn = new SqlConnection(ConnString);
cn.Open();
SqlCommand Sqlcmd = new SqlCommand(ProcedureName, cn);
Sqlcmd.CommandType = CommandType.StoredProcedure;
if (SqlPrams != null)
{
foreach (SqlParameter Sqlparameter in SqlPrams)
{
Sqlcmd.Parameters.Add(Sqlparameter);
}
}
Sqlcmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
SqlReader = Sqlcmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
/// 运行SQL Server储存过程
///
/// 储存过程名称
/// 以SqlDataReader返回储存过程结果
public void RunProcedure(string ProcedureName, out SqlDataReader SqlReader)
{
using (SqlConnection cn = new SqlConnection(ConnString))
{
try
{
SqlCommand Sqlcmd = new SqlCommand(ProcedureName, cn);
Sqlcmd.CommandType = CommandType.StoredProcedure;
Sqlcmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cn.Open();
SqlReader = Sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// /// 运行SQL Server储存过程
/// </summary>
/// <param name="ProcedureName">储存过程名称</param>
/// <param name="SqlPrams">储存过程参数</param>
/// <returns>储存过程返回值</returns>
public int RunProcedureForInt(string ProcedureName, SqlParameter[] SqlPrams)
{
using (SqlConnection cn = new SqlConnection(ConnString))
{
int ret = -1;
try
{
SqlCommand Sqlcmd = new SqlCommand(ProcedureName, cn);
Sqlcmd.CommandType = CommandType.StoredProcedure;
if (SqlPrams != null)
{
foreach (SqlParameter Sqlparameter in SqlPrams)
{
Sqlcmd.Parameters.Add(Sqlparameter);
}
}
Sqlcmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cn.Open();
Sqlcmd.ExecuteNonQuery();
ret = (int)Sqlcmd.Parameters["ReturnValue"].Value;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cn.Close();
}
return ret;
}
}
/// <summary>
/// 创建DataSet对象
/// </summary>
/// <param name="ProcedureName">储存过程名称</param>
/// <param name="Table">数据表名</param>
/// <returns>DataSet对象</returns>
///
public DataSet RunProcedureForDataSet(string ProcedureName)
{
using (SqlConnection cn = new SqlConnection(ConnString))
{
DataSet SqlDS = new DataSet();
try
{
SqlDataAdapter ada = new SqlDataAdapter(ProcedureName, cn);
ada.SelectCommand.CommandType = CommandType.StoredProcedure;
ada.SelectCommand.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cn.Open();
ada.Fill(SqlDS);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cn.Close();
}
return (SqlDS);
}
}
/// <summary>
/// 运行SQL Server储存过程
/// </summary>
/// <param name="ProcedureName">储存过程名称</param>
/// <param name="SqlPrams">Sql参数</param>
/// <param name="Table">数据表名</param>
/// <returns>DataSet对象</returns>
public DataSet RunProcedureForDataSet(string ProcedureName, SqlParameter[] SqlPrams)
{
using (SqlConnection cn = new SqlConnection(ConnString))
{
DataSet SqlDS = new DataSet();
try
{
SqlDataAdapter SqlDA = new SqlDataAdapter(ProcedureName, cn);
SqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;
if (SqlPrams != null)
{
foreach (SqlParameter Sqlparameter in SqlPrams)
{
SqlDA.SelectCommand.Parameters.Add(Sqlparameter);
}
}
SqlDA.SelectCommand.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cn.Open();
SqlDA.Fill(SqlDS);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cn.Close();
}
return (SqlDS);
}
}
/// <summary>
/// 创建SqlDataAdapter对象
/// </summary>
/// <param name="ProcedureName">储存过程的名称</param>
/// <param name="SqlPrams">参数</param>
/// <returns>返回SqlDataAdapter对象</returns>
//private SqlDataAdapter CreateDataAdapter(string ProcedureName, SqlParameter[] SqlPrams)
//{
// using (SqlConnection cn = new SqlConnection(ConnString))
// {
// }
//}
// cn.Open();
/// 创建输入参数
/// </summary>
/// <param name="ParameterName">参数名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新的参数</returns>
public SqlParameter MakeInputParameter(string ParameterName, SqlDbType DbType, int Size, object Value)
{
return this.MakeParameter(ParameterName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 创建输出参数
/// </summary>
/// <param name="ParameterName">参数名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的参数</returns>
public SqlParameter MakeOutParameter(string ParameterName, SqlDbType DbType, int Size)
{
return this.MakeParameter(ParameterName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 创建储存过程参数
/// </summary>
/// <param name="ParameterName">参数名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数方法</param>
/// <param name="Value">参数值</param>
/// <returns>新的参数</returns>
public SqlParameter MakeParameter(string ParameterName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter SqlParam;
if (Size > 0)
{
SqlParam = new SqlParameter(ParameterName, DbType, Size);
}
else
{
SqlParam = new SqlParameter(ParameterName, DbType);
}
SqlParam.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
{
SqlParam.Value = Value;
}
return SqlParam;
}
/// <summary>
/// 创建Command对象.
/// </summary>
/// <param name="ProcedureName">存储过程的名称</param>
/// <param name="SqlPrams">存储过程的参数</param>
/// <returns>返回SqlCommand对象</returns>
//private SqlCommand CreateCommand(string ProcedureName, SqlParameter[] SqlPrams)
//{
// SqlCommand Sqlcmd = new SqlCommand(ProcedureName, conn);
// Sqlcmd.CommandType = CommandType.StoredProcedure;
// if (SqlPrams != null)
// {
// foreach (SqlParameter Sqlparameter in SqlPrams)
// {
// Sqlcmd.Parameters.Add(Sqlparameter);
// }
// }
// Sqlcmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
// return Sqlcmd;
//}
}
public class WebHtmlCode
{
public static string GetHtmlFromUrl(string url)
{
string targeturl = url.Trim().ToString();
try
{
HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
WebResponse hs = hr.GetResponse();
Stream sr = hs.GetResponseStream();
StreamReader ser = new StreamReader(sr, Encoding.Default);
return ser.ReadToEnd();
}
catch (Exception ex)
{
return null;
}
}
}
public class NFile
{
//public bool MakeStaticFile(string webaddr, string target)
//{
// StringBuilder sb = new StringBuilder();
// bool ret = false;
// string web_text = "";
// try
// {
// web_text = DBLibrary.WebHtml.WebHtmlCode.GetHtmlFromUrl(webaddr.Trim());
// if (web_text.Length > 0)
// {
// sb.Append(web_text);
// ret = WriteFile(sb, target.Trim());
// }
// }
// catch
// {
// ret = false;
// }
// return ret;
//}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="filename">文件名全路径</param>
/// <returns></returns>
public static bool DeleteFile(string filename)
{
try
{
File.Delete(filename);
}
catch
{
return false;
}
return true;
}
public bool CreateDirectory(string directory)
{
if (directory.ToString().Trim() != "")
{
try
{
if (Directory.Exists(directory) == false)
{
Directory.CreateDirectory(directory.ToString().Trim());
}
}
catch
{
return false;
}
}
return true;
}
public bool FileExists(string filename)
{
return File.Exists(filename.ToString().Trim());
}
public bool WriteFile(StringBuilder sb, string filename)
{
try
{
if (sb != null && filename != "")
{
string dir = "";
int sbegin = filename.LastIndexOf("\\");
dir = filename.Substring(0, sbegin);
if (Directory.Exists(dir.ToString().Trim()) == false)
{
Directory.CreateDirectory(dir);
}
if (File.Exists(filename.ToString().Trim()) == false)
{
FileStream fs = File.Create(filename);
fs.Close();
}
//fs.Close();
StreamWriter sw = new StreamWriter(filename, false, Encoding.Default);
sw.Write(sb.ToString().Trim());
sw.Close();
}
}
catch
{
return false;
}
return true;
}
public StringBuilder ReadFile(string filename)
{
StringBuilder sb = new StringBuilder();
try
{
StreamReader sr = new StreamReader(filename.ToString().Trim(), Encoding.Default);
sb.Append(sr.ReadToEnd());
sr.Close();
return sb;
}
catch
{
return null;
}
}
public string GetExpandName(string filename)
{
string temp = filename.Trim();
int del = temp.LastIndexOf(".");
string rel = "";
if (del != -1)
{
rel = temp.Substring(del, temp.Length - del);
}
return rel;
}
public string GetCurrentTime()
{
string ctime = DateTime.Now.ToString().Trim();
ctime = ctime.Replace(":", "");
ctime = ctime.Replace("-", "");
ctime = ctime.Replace(" ", "");
ctime = ctime.Replace("AM", "");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -