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;
            }
        }
    }
}出现问题:

解决方案 »

  1.   

    把SQL Server服务器开起来就OK
      

  2.   

    sql2005开启远程连接功能 
    打开MSSQLSERVER节点下的Database Engine节点,选择"远程连接",接下来建议选择”同时使用TCP/IP和named pipes ”,确定后,重启数据库服务. 
    如果已经设置了 windows与SQL Server混合验证且启用了sa帐户 
     在开始菜单中的"SQL SERVER 2005"->"Configuration Tools"->"SQL SERVER Configuration Management"中将"SQL SERVER Clinet"中的命名管道(named pipes)打开,然后重启一下SQL SERVER的服务 
    防火墙是否设置.端口号是否禁用,企启用1433端口
    或在页面放置一个sqlconnection,然后通过.net的配置程序,配置一个,看看里面的连接字符串
    connectionString="Data Source==.\SQLEXPRESS;Initial Catalog=;User ID=sa;Password=sa" 
      

  3.   

    http://topic.csdn.net/u/20090610/19/d4328440-62ec-463a-ac19-3ecfa2533211.html
      

  4.   

    看你 连接字符 账号 密码   IP 对吗看看你  SQL服务是否开启 
      

  5.   

    在SQL 设置程序中, 你的TCP是否是可用的.
      

  6.   

    楼主你还是先把你的连接字符串给大家看看吧,或者你可以用一个sqldatasource控件图形化连接一下看看,可不可以,要是可以的话你可以用自动生成的。还有给你一个连接字符串的网站,www.connectionstrings.com参考上面的修改你的。