初学,关于很简单的登陆问题,在线等,快速给分

xiaolin4ever 2009-01-04 12:41:28
登陆需要用户名密码
然后连接数据库,寻找用户名和密码是否正确
就这么简单,要用到DataReader
哪位大神给一个最简单的连接数据库步骤(我的数据库是sql2000)和判断方法
谢谢
...全文
96 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangzhenyue 2009-01-04
  • 打赏
  • 举报
回复
楼上正解,但是他WEB CONFIG 不会配。。。
jiang_jiajia10 2009-01-04
  • 打赏
  • 举报
回复

protected void ButtonLogin_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]); //创建连接对象
SqlCommand cmd = new SqlCommand("select * from Users where userId='" + txtUserName.Text + "'", conn); //创建查询用户名是否存在数据对象
try
{
conn.Open(); //打开连接
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read()) //如果用户名输入正确
{
if (sdr["userPwd"].ToString() == txtPassword.Text)//密码正确
{
conn.Close();
Session["userID"] = txtUserName.Text.Trim();//存储用户名
Response.Redirect("AddressList.aspx"); //进入系统
}
else
{
Response.Write("<script language=javascript>alert('您输入的密码错误!')</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('您输入的用户名错误或该用户名不存在!')</script>");
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}
wickiup 2009-01-04
  • 打赏
  • 举报
回复
楼上的应该是最简便的了吧。
chwlyr 2009-01-04
  • 打赏
  • 举报
回复
SqlConnection con = new SqlConnection("连接字符串");
SqlCommand cmd = new SqlCommand("",con);
cmd.CommandText = string.Format("select count(*) from userTable where username = '{0}' and userpwd = '{1}'",用户名","密码");
object o = cmd.ExecuteScelar();
if(o!=null)
{
int i = (int)o;
if(i== 1)
{
//通过验证,可以登录
}
else
{
//验证失败。
}
}
xiaolin4ever 2009-01-04
  • 打赏
  • 举报
回复
楼上的,你没有看清楚我题目的意思啊
别要复制别人的,就要最简单的步骤
hongmaohouzi 2009-01-04
  • 打赏
  • 举报
回复
asp.net中c#操作SQL SERVER 2000数据库的操作类
(1)类中命名空间:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
(2)在web.config中的<configuration>下面一行加入数据库连接字符串
<appSettings>
<add key="sqlConn" value="data source=(local); initial catalog=02wangluo;user id=sa; password="/>
</appSettings>
(2)操作类代码:
public class DoDataBase
{
public static SqlConnection sqlConn = new SqlConnection();
public DoDataBase()
{

}
public static DataSet GetDataSet(string sql)
{
DataSet ds = new DataSet();

try
{
sqlConn = new SqlConnection(ConfigurationSettings.AppSettings["sqlConn"]);
SqlDataAdapter sqlApt = new SqlDataAdapter(sql, sqlConn);
sqlApt.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw (ex);
}
}

public static int ExecuteSQL(string sql)
{
try
{
sqlConn = new SqlConnection(ConfigurationSettings.AppSettings["sqlConn"]);
SqlCommand sqlCmd =new SqlCommand(sql,sqlConn);
sqlConn.Open();
return sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw (ex);
}
finally
{
sqlConn.Close();
}
}
}
alex_zs 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiaolin4ever 的回复:]
楼上的,你没有看清楚我题目的意思啊
别要复制别人的,就要最简单的步骤
[/Quote]
1楼的是正确的。。。
楼主不是在考试吧。。
xiaolin4ever 2009-01-04
  • 打赏
  • 举报
回复
谢谢8楼,方法采用了
各位给过答案的朋友新年快乐,09年多多加薪!
xiaolin4ever 2009-01-04
  • 打赏
  • 举报
回复
。。。。。。。。。。。。。
谁帮忙解决一下啊
wuyq11 2009-01-04
  • 打赏
  • 举报
回复
string u_name, u_pwd;
u_name = txtusername.Text.Trim().Replace("'","");
u_pwd = txtuserpwd.Text.Trim();
string connectionString = ConfigurationSettings.AppSettings["connstring"];
SqlConnection conn = new SqlConnection(connectionString);
string sqlStr = "select * from A where username = '"+u_name.ToString()+"' ";
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr["password"].ToString() == u_pwd)
{
Response.Write("登陆成功");
}
else
{
Response.Write("密码错误");
}
}
else
{
Response.Write("用户名错误");
}
conn.Close();
javakiki 2009-01-04
  • 打赏
  • 举报
回复
上面几位的程序怎么都不用参数?
3L的sql语句不好。容易sql 注入
建议传参或者使用5L的方法

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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