62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Security
{
public class ValidateBase : System.Web.UI.Page
{
private string sqlConnStr;
private SqlConnection conn;
public ValidateBase()
{
this.sqlConnStr = "Data Source=192.168.1.102;Initial Catalog=didian_data;User=sa;Password=sa;";
}
public ValidateBase(string connStr)
{
this.sqlConnStr = connStr;
}
private void Open()
{
if (this.conn == null)
this.conn = new SqlConnection(this.sqlConnStr);
if (this.conn.State == ConnectionState.Closed)
this.conn.Open();
}
private void Close()
{
if (this.conn.State == ConnectionState.Open)
this.conn.Close();
if (this.conn != null)
this.conn.Dispose();
this.conn = null;
}
public void BuildParameters(SqlParameter[] param, ref SqlCommand comm)
{
//若参数不为空,则添加到命令对象
if (param != null)
{
foreach (SqlParameter par in param)
comm.Parameters.Add(par);
}
//添加返回参数
comm.Parameters.Add(new SqlParameter("returnvalue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, true, 0, 0, string.Empty, DataRowVersion.Default, 0));
return;
}
public void ExecuteProcedure(string proceName, SqlParameter[] param, ref DataSet ds)
{
//打开连接
this.Open();
SqlCommand comm = this.conn.CreateCommand();
comm.CommandText = proceName;
comm.CommandType = CommandType.StoredProcedure;
this.BuildParameters(param, ref comm);
//初始化adapter对象
SqlDataAdapter ada = new SqlDataAdapter(comm);
//返回ds
ada.Fill(ds);
//关闭连接
this.Close();
return;
}
public void ExecuteProcedureInsertInto(string proceName, SqlParameter[] param)
{
//打开连接
this.Open();
SqlCommand comm = this.conn.CreateCommand();
comm.CommandText = proceName;
comm.CommandType = CommandType.StoredProcedure;
this.BuildParameters(param, ref comm);
comm.ExecuteNonQuery();
//关闭连接
this.Close();
//return;
}
public bool check_username(string procedurename, SqlParameter[] parm)
{
this.Open();
SqlCommand comm = this.conn.CreateCommand();
comm.CommandText = procedurename;
comm.CommandType = CommandType.StoredProcedure;
this.BuildParameters(parm, ref comm);
return Convert.ToBoolean(comm.ExecuteScalar());
this.Close();
}
public void Executeprocedure(string procedurename, ref DataSet ds)
{
this.Open();
SqlCommand comm = this.conn.CreateCommand();
comm.CommandText = procedurename;
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ada = new SqlDataAdapter(comm);
ada.Fill(ds);
this.Close();
}
public SqlDataReader Executeprocedure1(string procedurename)
{
this.Open();
SqlCommand comm = this.conn.CreateCommand();
comm.CommandText = procedurename;
comm.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = comm.ExecuteReader();
return dr;
this.Close();
}
public SqlDataReader ExecuteProcedure(string proceName, SqlParameter[] param)
{
//打开连接
//int num;
this.Open();
SqlCommand comm = this.conn.CreateCommand();
comm.CommandText = proceName;
comm.CommandType = CommandType.StoredProcedure;
this.BuildParameters(param, ref comm);
//SqlDataReader dr = comm.ExecuteReader();
//news = dr.GetValue(1).ToString();
SqlDataReader dr = comm.ExecuteReader();
// num = Convert.ToInt32(comm.ExecuteScalar());
//if(dr.Read())
return dr;
// Response.Write("<script>alert('"+dr.GetString(1)+"');</script>");
//关闭连接
this.Close();
}
public void datab(string sql, ref DataSet ds)
{
this.Open();
//OleDbCommand comm = new OleDbCommand(sql,this.OledbConn);
SqlDataAdapter adap = new SqlDataAdapter(sql, this.conn);
adap.Fill(ds);
this.Close();
return;
}
public SqlDataReader readrow(string sqlstr1)
{
SqlConnection datareaderconn = new SqlConnection(this.sqlConnStr);
SqlCommand datareadercomm = new SqlCommand(sqlstr1, datareaderconn);
SqlDataReader datareader;
datareadercomm.Connection.Open();
datareader = datareadercomm.ExecuteReader();
if (datareader.Read())
{
datareadercomm.Dispose();
return datareader;
}
else
{
datareadercomm.Dispose();
return null;
}
}
/// <summary>
/// 存放验证码值
/// </summary>
public string strValidate
{
get
{
if (Session["ValidateCode"] != null)
return Session["ValidateCode"].ToString();
else
return "";
}
set
{
Session["ValidateCode"] = value;
}
}
/// <summary>
/// 存放登入信息
/// </summary>
public string strUser
{
get
{
if (Session["strUser"] != null)
return Session["strUser"].ToString();
else
return "";
}
set
{
Session["strUser"] = value;
}
}
}
}