初学asp.net,请教关于数据库编程的一个问题(C#)。 Button1_Click方法中没有连接数据库怎么取! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void Page_Load(object sender, System.EventArgs e) { }//按“确定”后检验用户名和密码是否正确?private void Button1_Click(object sender, System.EventArgs e) { // Put user code to initialize the page here //建立连接 string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30"; string strSel = "Select * from userinfo"; SqlConnection MyConn = new SqlConnection(MyConnString); SqlCommand MyComm = new SqlCommand(strSel,MyConn); MyComm.Connection.Open(); //读取数据 string NameString; string PString; SqlCommand cmd=new SqlCommand("SELECT PASSWORD FORM USERINFO where userid='"+txtUserID.Text+"'"); string pwd =Convert.ToString(cmd.ExecuteScalar()); //检验用户名和密码是否相同 ... } sorry://按“确定”后检验用户名和密码是否正确?private void Button1_Click(object sender, System.EventArgs e) { // Put user code to initialize the page here //建立连接 string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30"; string strSel = "Select * from userinfo"; SqlConnection MyConn = new SqlConnection(MyConnString); SqlCommand MyComm = new SqlCommand(strSel,MyConn); MyComm.Connection.Open(); //读取数据 string NameString; string PString; MyComm.CommandText = "SELECT PASSWORD FORM USERINFO where userid='"+txtUserID.Text+"'"; string pwd =Convert.ToString(MyComm.ExecuteScalar()); //检验用户名和密码是否相同 ... } //在Page_Load是建立数据库连接private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here }//按“确定”后检验用户名和密码是否正确?private void Button1_Click(object sender, System.EventArgs e) {//建立连接 string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30"; string strSel = "Select * from userinfo"; SqlConnection MyConn = new SqlConnection(MyConnString); SqlCommand MyComm = new SqlCommand(strSel,MyConn); MyComm.Connection.Open(); //读取数据 string NameString; string PString; SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO"); SqlDataReader dr=cmd.ExecuteReader(); //获得用户名和密码 If(dr.Read()) { NameString=dr.GetName(1); PString=dr.GetName (2); } //检验用户名和密码是否相同 } 你的Button1_Click中的SqlCommand cmd=...,cmd没有指定数据连接,asp.net和asp不一样的,在asp中可以打开页面就打开数据连接,数据处理完了之后再关,但是在asp.net中就要用的时候打开,用完及时关闭private void Button1_Click(object sender, System.EventArgs e){ string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30"; string strSel = "Select * from userinfo"; SqlConnection MyConn = new SqlConnection(MyConnString); MyConn.Open(); SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO",MyConn); SqlDataReader dr=cmd.ExecuteReader(); ...//处理数据 MyConn.Close(); //记得关闭连接}这样就可以了 谢谢,我以为在Page_Load()开了连接就可以了。 SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO");里面没有Connection对象!应该好好了解一下变量的作用范围了! GridView取值问题 ajax给页面添加一个div,再也无法删除? 网页上显示字符的问题! 请各位帮个忙,datalist翻页 求救 关于CheckBoxList获取值的问题? 我是个新手:为什么我找不到控件??? 将excel数据导出到gridview表代码出错 简单问题! ASP。NET 中我用C#做邮件发送功能遇到问题请帮我! 调试时明明可以看到执行的加法没有加成功,却通过了调试,最后导致运算错误。 各位高手,高分求WEB控件 用<%----%>注释和<!-- -->注释有什么区别?
{
}//按“确定”后检验用户名和密码是否正确?
private void Button1_Click(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//建立连接
string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
string strSel = "Select * from userinfo";
SqlConnection MyConn = new SqlConnection(MyConnString);
SqlCommand MyComm = new SqlCommand(strSel,MyConn);
MyComm.Connection.Open(); //读取数据
string NameString;
string PString;
SqlCommand cmd=new SqlCommand("SELECT PASSWORD FORM USERINFO where userid='"+txtUserID.Text+"'");
string pwd =Convert.ToString(cmd.ExecuteScalar()); //检验用户名和密码是否相同
...
}
private void Button1_Click(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//建立连接
string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
string strSel = "Select * from userinfo";
SqlConnection MyConn = new SqlConnection(MyConnString);
SqlCommand MyComm = new SqlCommand(strSel,MyConn);
MyComm.Connection.Open(); //读取数据
string NameString;
string PString;
MyComm.CommandText = "SELECT PASSWORD FORM USERINFO where userid='"+txtUserID.Text+"'";
string pwd =Convert.ToString(MyComm.ExecuteScalar()); //检验用户名和密码是否相同
...
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}//按“确定”后检验用户名和密码是否正确?
private void Button1_Click(object sender, System.EventArgs e)
{
//建立连接
string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
string strSel = "Select * from userinfo";
SqlConnection MyConn = new SqlConnection(MyConnString);
SqlCommand MyComm = new SqlCommand(strSel,MyConn);
MyComm.Connection.Open(); //读取数据
string NameString;
string PString;
SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO");
SqlDataReader dr=cmd.ExecuteReader();
//获得用户名和密码
If(dr.Read())
{
NameString=dr.GetName(1);
PString=dr.GetName (2);
} //检验用户名和密码是否相同
}
asp.net和asp不一样的,在asp中可以打开页面就打开数据连接,数据处理完了之后再关,但是在asp.net中就要用的时候打开,用完及时关闭
private void Button1_Click(object sender, System.EventArgs e)
{
string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
string strSel = "Select * from userinfo";
SqlConnection MyConn = new SqlConnection(MyConnString);
MyConn.Open();
SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO",
MyConn);
SqlDataReader dr=cmd.ExecuteReader();
...//处理数据
MyConn.Close(); //记得关闭连接
}
这样就可以了
里面没有Connection对象!
应该好好了解一下变量的作用范围了!