问题描述
用户代码未处理sqlexection怎么解决啊?usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;usingSystem.Data.Sql;publicpartialclasslogin:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){stringuser_name=Request.Form["user_name"].ToString();stringuser_password=Request.Form["user_password"].ToString();SqlConnectionconn=newSqlConnection("server=.;pwd=;uid=sa;database=login;");conn.Open();SqlCommandcmd=newSqlCommand("selectcount(*)fromloginwhereuser_name='"+user_name+"'anduser_password='"+user_password+"'",conn);intcount=Convert.ToInt32(cmd.ExecuteScalar());if(count>0){Response.Redirect("main.aspx?user_name="+user_name);}else{Response.Redirect("loginFail.htm");}}}
解决方案
解决方案二:
楼主应该看看真正出错的信息就凭楼主所说,根本无法确认是连接出错还是执行语句出错还有确认你的数据库和表名字都是loginSqlConnectionconn=newSqlConnection("server=.;pwd=;uid=sa;database=login;");conn.Open();SqlCommandcmd=newSqlCommand("selectcount(*)fromloginwhereuser_name='"+user_name+"'anduser_password='"+user_password+"'",conn);==即使是login,也该改为SqlConnectionconn=newSqlConnection("server=.;pwd=;uid=sa;database=[login]");conn.Open();SqlCommandcmd=newSqlCommand("selectcount(*)from[login]whereuser_name='"+user_name+"'anduser_password='"+user_password+"'",conn);
解决方案三:
是数据库安装期间没有选择默认
解决方案四:
加try..catch
解决方案五:
错误很可能是:1.连接字符传出了问题2.sql语句写的有问题---建议单步调试,看看到底在哪里出错~
解决方案六:
数据库名是LOGIN表名也是LOGIN?