日期:2014-05-18  浏览次数:20735 次

继68,16问题之后,我还想问一个巨大的问题,谢谢
什么都不说,线上一大段代码
C# code
public class datacon
{
    public datacon()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public SqlConnection getcon()
    {
        string strcon = ConfigurationManager.ConnectionStrings["constr"].ToString();
        SqlConnection sqlcon = new SqlConnection(strcon);
        return sqlcon;
    }
    public bool eccom(string sqlstr)
    {
        SqlConnection con = this.getcon();
        con.Open();
        SqlCommand mycommand = new SqlCommand(sqlstr, con);
        try
        {
            mycommand.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            con.Close();
        }
    }
    public bool ecdropdonlist(DropDownList ddl, string sqlstr3, string dtf, string dvf)//DropDownList的数据库连接
    {
        SqlConnection con = this.getcon();
        con.Open();
        SqlDataAdapter mydataadapter = new SqlDataAdapter(sqlstr3, con);
        DataSet mydataset = new DataSet();
        mydataadapter.Fill(mydataset);
        ddl.DataSource = mydataset;
        ddl.DataTextField = dtf;
        ddl.DataValueField = dvf;
        try
        {
            ddl.DataBind();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            con.Close();
        }

    }
    public bool ecadabind(GridView gv, string sqlstrl4)//GridView的数据库连接
    {
        SqlConnection con = this.getcon();
        con.Open();
        SqlDataAdapter mydataadapter = new SqlDataAdapter(sqlstrl4, con);
        DataSet mydataset = new DataSet();
        mydataadapter.Fill(mydataset);
        gv.DataSource = mydataset;
        try
        {
            gv.DataBind();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            con.Close();
        }
    }
    public bool ecadabinginfoetring(GridView gv, string sqlstr5, string dnk)
    {
        SqlConnection con = this.getcon();
        con.Open();
        SqlDataAdapter mydataadapter = new SqlDataAdapter(sqlstr5, con);
        DataSet mydataset = new DataSet();
        mydataadapter.Fill(mydataset);
        gv.DataSource = mydataset;
        gv.DataKeyNames = new string[] { dnk };
        try
        {
            gv.DataBind();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            con.Close();
        }
    }
    public SqlDataReader exceread(string sqlcom)
    {
        SqlConnection con = this.getcon();
        con.Open();
        SqlCommand sqlcom = new SqlCommand(sqlcom, con);
        SqlDataReader read = sqlcom.ExecuteReader();
        return read;
    }
}


没错很多的代码,其实我要问的是这样的,这里面的方法应该都是和连接数据库相联系的,因为要重复连接所以就高了这么一个公共类

悲催的是你知道我原来是怎么连接数据库的吗?
是这样的
C# code

  SqlConnection conn = new SqlConnection("server=.;uid=customer adm;pwd=6568986z;database=customer");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select*from cdetials where uname='"+this.textBox1.Text+"'";---执行tsql语句,读取数据库中的信息
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();---  SqlDataReader读取行的操作,ExecuteReader()执行读取
            if (dr.HasRows)————读取第一个值,即第一个uname
            {
                dr.Read();————继续读下一条
                if (this.textBox2.Text == dr["upwd"].ToString())
                {
                    Form2 f2 = new Form2();
                    f2.Show();
                    this.Visible = false;
                }