用户名,密码和3个按钮——登陆,注册,重置,目前无大问题了,就是注册时注册同名用户怎么让它说已注册,请换个名字,现在是注册同名不同密码也可以,但这个名字登陆失败,这代码不是我写的,只是大概能看懂,伸手党求代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (getInfo(txb_USERID.Text.ToString()).Tables[0].Rows.Count > 0)
{
DataRow DR = getInfo(txb_USERID.Text.ToString()).Tables[0].Rows[0];
if (DR["PWD"].ToString() == txb_PWD.Text.ToString())
{
lbl_INFO.Text = "登录成功";
}
else
{
lbl_INFO.Text = "登录失败";
}
}
else
{
lbl_INFO.Text = "无该人员信息";
}
} protected void Button2_Click(object sender, EventArgs e)
{
string USERID = txb_USERID.Text.ToString();
string PWD = txb_PWD.Text.ToString();
bool insertOK = InsertInfo(USERID, PWD);
if (insertOK)
{
lbl_INFO.Text = "注册成功";
}
else
{
lbl_INFO.Text = "注册失败";
}
}
protected DataSet getInfo(string USERID)
{
string strCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\liniannian\Documents\Visual Studio 2005\Projects\WebSite99\SqlDataSource.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(strCon);
string str = "select * from SQLTable where USERID='" + USERID + "'";
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch
{
throw new Exception(str);
}
finally
{
con.Close();
} }
protected bool InsertInfo(string USERID,string PWD)
{
string strCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\liniannian\Documents\Visual Studio 2005\Projects\WebSite99\SqlDataSource.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(strCon);
string str = "insert into SQLTable (USERID,PWD) VALUES ('" + USERID + "','" + PWD + "')";
SqlCommand cmd = new SqlCommand(str, con);
con.Open();
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
con.Close();
} }
protected void Button3_Click(object sender, EventArgs e)
{
txb_PWD.Text = "";
txb_USERID.Text = "";
lbl_INFO.Text = ""; }
protected void txb_USERID_TextChanged(object sender, EventArgs e)
{ }
protected void txb_PWD_TextChanged(object sender, EventArgs e)
{ }
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (getInfo(txb_USERID.Text.ToString()).Tables[0].Rows.Count > 0)
{
DataRow DR = getInfo(txb_USERID.Text.ToString()).Tables[0].Rows[0];
if (DR["PWD"].ToString() == txb_PWD.Text.ToString())
{
lbl_INFO.Text = "登录成功";
}
else
{
lbl_INFO.Text = "登录失败";
}
}
else
{
lbl_INFO.Text = "无该人员信息";
}
} protected void Button2_Click(object sender, EventArgs e)
{
string USERID = txb_USERID.Text.ToString();
string PWD = txb_PWD.Text.ToString();
bool insertOK = InsertInfo(USERID, PWD);
if (insertOK)
{
lbl_INFO.Text = "注册成功";
}
else
{
lbl_INFO.Text = "注册失败";
}
}
protected DataSet getInfo(string USERID)
{
string strCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\liniannian\Documents\Visual Studio 2005\Projects\WebSite99\SqlDataSource.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(strCon);
string str = "select * from SQLTable where USERID='" + USERID + "'";
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch
{
throw new Exception(str);
}
finally
{
con.Close();
} }
protected bool InsertInfo(string USERID,string PWD)
{
string strCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\liniannian\Documents\Visual Studio 2005\Projects\WebSite99\SqlDataSource.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(strCon);
string str = "insert into SQLTable (USERID,PWD) VALUES ('" + USERID + "','" + PWD + "')";
SqlCommand cmd = new SqlCommand(str, con);
con.Open();
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
con.Close();
} }
protected void Button3_Click(object sender, EventArgs e)
{
txb_PWD.Text = "";
txb_USERID.Text = "";
lbl_INFO.Text = ""; }
protected void txb_USERID_TextChanged(object sender, EventArgs e)
{ }
protected void txb_PWD_TextChanged(object sender, EventArgs e)
{ }
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{}
}
解决方案 »
- 救火
- C#中HttpWebRequest设置KeepAlive的问题
- 字符串中\0\f是什么意思?我在StreamReader获取到的
- listview 右键选中怎样设置
- 类访问窗体中控件事件的方法
- 请问在winform里怎么使用框架结构,象在网页里使用frame。然后在框架里用的界面是什么,也是一个新的窗体吗?
- 给个Borland Together 2005 for VS.net正式版的下载地址吧。
- TeeChart.Net 我想自定义这两个点的颜色, 要如何做呢?C#
- 用C#调用WinAPI的PolyPolyline函数的方法。
- 如何在读取一个非web目录的文件后用http给客户端。
- 就一般的客户机对客户机,media能否远程播放?
- C#获取从指定的文件路径,获取该文件的图标,然后显示在image里
http://kb.cnblogs.com/a/1579126/
这个比较原始
protected void Button2_Click(object sender, EventArgs e)
{
string USERID = txb_USERID.Text.ToString();
string PWD = txb_PWD.Text.ToString();
DataSet ds =getInfo(USERID );
if(dds.Tables[0].Rows.Count()>0)
{
lbl_INFO.Text = "用户名已存在!";
}
else
{
bool insertOK = InsertInfo(USERID, PWD);
if (insertOK)
{
lbl_INFO.Text = "注册成功";
}
else
{
lbl_INFO.Text = "注册失败";
}
}
}
行 43: if (ds.Tables[0].Rows.Count() > 0)
protected void Button2_Click(object sender, EventArgs e)
{
string USERID = txb_USERID.Text.ToString();
string PWD = txb_PWD.Text.ToString();
DataSet ds =getInfo(USERID );
if( ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0) //sorry 更正
{
lbl_INFO.Text = "用户名已存在!";
}
else
{
bool insertOK = InsertInfo(USERID, PWD);
if (insertOK)
{
lbl_INFO.Text = "注册成功";
}
else
{
lbl_INFO.Text = "注册失败";
}
}
}