日期:2014-05-18 浏览次数:20735 次
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; } }
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; }