📄 adohelper.cs
字号:
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text;
using System.Security.Cryptography;
namespace SB
{
public class ADOHelper
{
public SqlConnection conn;
static String m_ConnectionString;
public ADOHelper()
{
conn = new SqlConnection(ADOHelper.ConnectionString());
}
#region// getDataSet
public DataSet getDataset(string cmd)
{
this.CheckConnection();
DataSet dataSet= new DataSet();
try
{
SqlCommand dataCommand = new SqlCommand(cmd,conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand =dataCommand;
dataAdapter.Fill(dataSet);
}
catch(SqlException se)
{
throw new Exception("Error in SQL", se);
}
this.Dispose();
return dataSet;
}
#endregion
#region//getReader
public SqlDataReader getReader(String cmd)
{
this.CheckConnection();
SqlDataReader dr = null;
try
{
SqlCommand dc = new SqlCommand(cmd, conn);
dr = dc.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch(Exception ex)
{
throw new Exception("error",ex);
}
}
#endregion
#region//updateSql
public int updateSql(string cmd)
{
this.CheckConnection();
try
{
SqlCommand dc = new SqlCommand(cmd, conn);
int dr = Convert.ToInt32(dc.ExecuteNonQuery().ToString());
return dr;
}
catch(Exception ex)
{
throw new Exception("error",ex);
}
}
#endregion
#region//execSql
public void execSql(string cmd)
{
this.CheckConnection();
try
{
SqlCommand dc = new SqlCommand(cmd, conn);
dc.ExecuteNonQuery();
}
catch(SqlException ex)
{
throw new Exception("error",ex);
}
this.Dispose();
return;
}
#endregion
#region//CheckConnection
private void CheckConnection()
{
try
{
if (conn.State != ConnectionState.Open)
conn.Open();
}
catch (System.Data.SqlClient.SqlException se)
{
throw new Exception("Failed to Open connection.", se);
}
}
#endregion
#region//ConnectionString
public static String ConnectionString()
{
if (m_ConnectionString == null)
{
m_ConnectionString = (String) ConfigurationSettings.AppSettings["ConnectionString"];
if (m_ConnectionString == null)
{
throw new Exception("Connect string value not set in Web.config");
}
}
return m_ConnectionString;
}
#endregion
#region// Dispose
public void Dispose()
{
try
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
catch(Exception ex)
{
throw new Exception("error",ex);
}
}
#endregion
#region//execprocedure
public int execprocedure(DateTime dateinit)
{
this.CheckConnection();
try
{
SqlCommand dc = new SqlCommand("weeks", conn);
dc.CommandType=CommandType.StoredProcedure;
dc.Parameters.Add("@dateinit",dateinit);
SqlParameter ret=dc.Parameters.Add("@ret",SqlDbType.Int);
ret.Direction=ParameterDirection.ReturnValue;
dc.ExecuteNonQuery();
return (int)ret.Value;
}
catch(Exception ex)
{
throw new Exception("error",ex);
}
}
#endregion
#region//execExecuteScalar
public int execExecuteScalar(string cmd)
{
this.CheckConnection();
try
{
SqlCommand dc = new SqlCommand(cmd, conn);
return (Convert.ToInt32(dc.ExecuteScalar()));
}
catch(SqlException ex)
{
throw new Exception("error",ex);
}
}
#endregion
#region//权限判断
public static bool pass(string name,string src)
{
string role;
bool exist=false;
SqlConnection conn=new SqlConnection((string)ConfigurationSettings.AppSettings["ConnectionString"]);
conn.Open();
SqlCommand cmd=new SqlCommand("select Role from SystemUser where UserName='"+name+"'",conn);
SqlDataReader rd=cmd.ExecuteReader();
rd.Read();
role=rd["Role"].ToString();
for(int i=0;i<role.Length;i=role.IndexOf("#"))
{
if(role.Substring(i,role.IndexOf("#"))==src)
{
exist=true;
}
}
rd.Close();
return exist;
}
#endregion
#region //字符替换函数
public static String inputText(string username)
{
StringBuilder tempstr=new StringBuilder();
if((username!=null)&&(username!=String.Empty))
{
string usernamestr=username.Trim();
for(int i=0;i<usernamestr.Length;i++)
{
switch(usernamestr[i])
{
case '"':tempstr.Append(""");break;
case '<':tempstr.Append("<");break;
case '>':tempstr.Append("gt");break;
default:tempstr.Append(usernamestr[i]);break;
}
}
tempstr.Replace("'"," ");
}
return tempstr.ToString();
}
#endregion
//
#region/加密码函数
public static String Encrypt(string password)
{//string EnPswdStr=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(source.Text.ToString(),"MD5");
Byte[] clearBytes=new UnicodeEncoding().GetBytes(password);
Byte[] hashedBytes=((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
//return EnPswdStr;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -