asp 登陆界面连接sql数据库完成登录怎么做?求大神帮忙

qq_25744253 2015-01-31 10:30:11
vs2012建了个asp登录界面,想连接数据库完成这个过程,sql 2008 建了表了,username和password,小白一个弄了半天没弄好,求大神帮帮忙
...全文
388 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_25744253 2015-02-02
  • 打赏
  • 举报
回复
大神都忙,还好自己搞定了,就这样结贴吧
qq_25744253 2015-02-02
  • 打赏
  • 举报
回复
protected void button1_Click(object sender, EventArgs e) { string str = WebConfigurationManager.ConnectionStrings["con"].ToString(); string user = uname.Text; string pwd = upwd.Text; SqlConnection conn = new SqlConnection(str); string sqlsel = "select * from logininfor where username="+user+" and password="+pwd; SqlCommand comm = new SqlCommand(sqlsel,conn ); //"Select * from logininfor where username="+user+"and password="+pwd" SqlDataReader dr; //comm.Connection = conn; conn.Open(); dr = comm.ExecuteReader(); if (dr.HasRows) { Server.Execute("main.html"); Response.Write("<script>alert('登录成功');</script>"); conn.Close(); } else { Response.Write("登录失败"); conn.Close(); } } 改写了一下代码,还是不行,提示说列名无效
qq_25744253 2015-02-01
  • 打赏
  • 举报
回复
<configuration> <connectionStrings> <add name="con" connectionString="Data Source=localhost;Database=testproject;uid=用户名;pwd=密码" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration> 以上是Web.config的内容。 <form id="form1" runat="server"> <div> 用户名:<asp:TextBox ID="uname" runat="server" ></asp:TextBox><br /> 密 码: <asp:TextBox ID="upwd" runat="server" TextMode="Password" ></asp:TextBox><br /> <asp:Button ID="button1" runat="server" Text="提交" onclientclick="button1_Click()"/> </div> </form> 以上是我登陆界面的主要代码; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Web.Configuration; namespace test1 { public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string str = WebConfigurationManager.ConnectionStrings["con"].ToString(); SqlCommand comm = new SqlCommand("Select * from logininfor"); string user = uname.Text.ToString(); string pwd = upwd.Text.ToString(); SqlDataReader dr; SqlConnection conn = new SqlConnection(str); conn.Open(); comm.Connection = conn; dr = comm.ExecuteReader(); if (dr.HasRows) { //Server.Execute("main.html"); Response.Write("<script>alert('登录成功');</script>"); } else { Response.Write("登录失败"); } } 这是login.aspx.cs的代码,现在只要我一打开页面不输入用户名和密码就直接弹出登录成功的对话框了,登录按钮的事件怎么写?我知道写在页面加载事件里是不对的,但不知道怎么把写的单击事件和按钮关联起来,还有什么都没输入就弹出登录成功对话框是为什么?求各位大侠帮忙解释一下
孟子E章 2015-02-01
  • 打赏
  • 举报
回复
数据库连接的写法可能存在问题,根本连不上数据库 另外,asp连接需要安装 Microsoft® SQL Server® 2008 R2 功能包 中的 Microsoft® SQL Server® 2008 R2 Native Client 下载地址 展开安装说明找 Microsoft® SQL Server® 2008 R2 Native Client http://www.microsoft.com/zh-cn/download/details.aspx?id=16978

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧