c#用代码实现注册登录代码
在c#窗体程序中,注册界面有姓名,帐号,密码,如何实现,点击注册按钮,就将以上信息保存到相应的数据库中。还有注册好了后登录按钮的代码,数据库是SQLServer数据库。...
在c#窗体程序中,注册界面有姓名,帐号,密码,如何实现,点击注册按钮,就将以上信息保存到相应的数据库中。还有注册好了后登录按钮的代码,数据库是SQL Server数据库。
展开
7个回答
展开全部
随便写的,不过试过正常使用,分层的就不写啦
注册按钮事件
public void zhuce_click(Object sender,EventArgs e)
{
if (username.Text == "")
{
Response.Write("<script>alert('请输入用户名!')</script>");
return;
}
if (password.Text == "")
{
Response.Write("<script>alert('请输入密码!')</script>");
return;
}
if (repass.Text == "")
{
Response.Write("<script>alert('请确认密码!')</script>");
return;
}
if (qu.Text == "")
{
Response.Write("<script>alert('请输入提示问题!')</script>");
return;
}
if (an.Text == "")
{
Response.Write("<script>alert('请回答问题!')</script>");
return;
}
if (company.Text == "")
{
Response.Write("<script>alert('请输入公司名称!')</script>");
return;
}
if (sex.Text == "")
{
Response.Write("<script>alert('请输入联系人性别!')</script>");
return;
}
if (phone.Text == "")
{
Response.Write("<script>alert('请输入公司联系电话!')</script>");
return;
}
if (email.Text == "")
{
Response.Write("<script>alert('请输入公司电子邮件!')</script>");
return;
}
if (address.Text == "")
{
Response.Write("<script>alert('请输入公司办公地址!')</script>");
return;
}
if (country.Text == "")
{
Response.Write("<script>alert('请输入公司注册地-国家!')</script>");
return;
}
if (sf.Text == "")
{
Response.Write("<script>alert('请输入公司注册地-省份!')</script>");
return;
}
if (city.Text == "")
{
Response.Write("<script>alert('请输入公司注册地-城市!')</script>");
return;
}
if (phone2.Text == "")
{
Response.Write("<script>alert('请输入联系人的手机号!')</script>");
return;
}
if (fax.Text == "")
{
Response.Write("<script>alert('请输入公司传真!')</script>");
return;
}
if (post.Text == "")
{
Response.Write("<script>alert('请输入邮政编码!')</script>");
return;
}
if (qyjj.Text == "")
{
Response.Write("<script>alert('请输入企业介绍!')</script>");
return;
}
if (frdb.Text == "")
{
Response.Write("<script>alert('请输入法人代表!')</script>");
return;
}
if (zczj.Text == "")
{
Response.Write("<script>alert('请输入注册资金!')</script>");
return;
}
if (zycp.Text == "")
{
Response.Write("<script>alert('请输入主营产品!')</script>");
return;
}
if (gsrs.Text == "")
{
Response.Write("<script>alert('请输入公司人数!')</script>");
return;
}
if (lname.Text == "")
{
Response.Write("<script>alert('请输入联系人姓名!')</script>");
return;
}
String pass1,pass2;
pass1 = (FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text.Trim(),"MD5"));
pass2 = pass1.Substring(8,pass1.Length-16);
String strconn;
strconn = ConfigurationManager.ConnectionStrings["strconn"].ConnectionString;
SqlConnection conn = new SqlConnection(strconn);
String sqlstr;
sqlstr = "Insert into company (username,password,question,answer,country,sf,city,qymc,zczj,frdb,qylb,name,sex,fax,sortid,typeid,phone,qyjj,web,vip_flag,uflag,url,deng,email,address,mobile,gsrs,zycp,post,postdate) Values ('"+username.Text.Trim()+" ','"+pass2+"','"+qu.Text.Trim()+"','"+an.Text.Trim()+"','"+country.Text.Trim()+"','"+sf.Text.Trim()+"','"+city.Text.Trim()+" ','"+company.Text.Trim()+" ','"+zczj.Text.Trim()+" ','"+frdb.Text.Trim()+" ','"+leixin.SelectedItem.Text.Trim()+"','"+lname.Text.Trim()+" ','"+sex.SelectedItem.Text.Trim()+"','"+fax.Text.Trim()+"','"+DropDownList1.Text.Trim()+"','"+DropDownList2.Text.Trim()+"','" +phone.Text.Trim()+"','"+qyjj.Text.Trim()+"','0','0','未审核','0','0','"+email.Text.Trim()+"','"+address.Text.Trim()+"','"+phone2.Text.Trim()+"','"+gsrs.Text.Trim()+"','"+zycp.Text.Trim()+"','"+post.Text.Trim()+"','"+DateTime.Now.ToString()+"')";
conn.Open();
SqlCommand cmd1;
String strsql;
strsql = "select username from [company] where username = '" + username.Text + "'";
cmd1 = new SqlCommand(strsql, conn);
SqlDataReader rd;
rd = cmd1.ExecuteReader();
if (rd.Read())
{
Response.Write("<SCRIPT language=JavaScript>alert('此用户名已被注册!');</SCRIPT>");
return;
}
else
{
conn.Close();
conn.Open();
SqlCommand cmd;
cmd = new SqlCommand(sqlstr, conn);
if (tongyi.Checked == true)
{
cmd.ExecuteNonQuery();
}
conn.Close();
conn.Dispose();
Response.Redirect("successful_zc.aspx");
}
登陆按钮事件
public void checklogin_click(Object sender, EventArgs e)
{
String strconn;
strconn = ConfigurationManager.ConnectionStrings["strconn"].ConnectionString;
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd;
String sqlstr;
String pass1, pass2;
pass1 = (FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text.Trim(), "MD5"));
pass2 = pass1.Substring(8,pass1.Length-16);
sqlstr = "Select username,password From [company] where username = '" + username.Text + "' and password='" + pass2 + "'";
cmd = new SqlCommand(sqlstr,conn);
SqlDataReader rd;
conn.Open();
rd = cmd.ExecuteReader();
if (check.Text != Label1.Text)
{
jc.Text = "附加码输入错误!";
return;
}
if (rd.Read())
{
Session["UserName"]= username.Text;
String key;
key = Session["UserName"].ToString();
String strsql;
strsql = "update [company] set deng=deng+1 where username = '" + key + "' ";
conn.Close();
conn.Open();
cmd = new SqlCommand(strsql, conn);
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
Response.Redirect("../member/ad_xx.aspx");
}
else
{
jc.Text = "用户名或密码错误,请重新输入!";
}
conn.Close();
conn.Dispose();
}
注册按钮事件
public void zhuce_click(Object sender,EventArgs e)
{
if (username.Text == "")
{
Response.Write("<script>alert('请输入用户名!')</script>");
return;
}
if (password.Text == "")
{
Response.Write("<script>alert('请输入密码!')</script>");
return;
}
if (repass.Text == "")
{
Response.Write("<script>alert('请确认密码!')</script>");
return;
}
if (qu.Text == "")
{
Response.Write("<script>alert('请输入提示问题!')</script>");
return;
}
if (an.Text == "")
{
Response.Write("<script>alert('请回答问题!')</script>");
return;
}
if (company.Text == "")
{
Response.Write("<script>alert('请输入公司名称!')</script>");
return;
}
if (sex.Text == "")
{
Response.Write("<script>alert('请输入联系人性别!')</script>");
return;
}
if (phone.Text == "")
{
Response.Write("<script>alert('请输入公司联系电话!')</script>");
return;
}
if (email.Text == "")
{
Response.Write("<script>alert('请输入公司电子邮件!')</script>");
return;
}
if (address.Text == "")
{
Response.Write("<script>alert('请输入公司办公地址!')</script>");
return;
}
if (country.Text == "")
{
Response.Write("<script>alert('请输入公司注册地-国家!')</script>");
return;
}
if (sf.Text == "")
{
Response.Write("<script>alert('请输入公司注册地-省份!')</script>");
return;
}
if (city.Text == "")
{
Response.Write("<script>alert('请输入公司注册地-城市!')</script>");
return;
}
if (phone2.Text == "")
{
Response.Write("<script>alert('请输入联系人的手机号!')</script>");
return;
}
if (fax.Text == "")
{
Response.Write("<script>alert('请输入公司传真!')</script>");
return;
}
if (post.Text == "")
{
Response.Write("<script>alert('请输入邮政编码!')</script>");
return;
}
if (qyjj.Text == "")
{
Response.Write("<script>alert('请输入企业介绍!')</script>");
return;
}
if (frdb.Text == "")
{
Response.Write("<script>alert('请输入法人代表!')</script>");
return;
}
if (zczj.Text == "")
{
Response.Write("<script>alert('请输入注册资金!')</script>");
return;
}
if (zycp.Text == "")
{
Response.Write("<script>alert('请输入主营产品!')</script>");
return;
}
if (gsrs.Text == "")
{
Response.Write("<script>alert('请输入公司人数!')</script>");
return;
}
if (lname.Text == "")
{
Response.Write("<script>alert('请输入联系人姓名!')</script>");
return;
}
String pass1,pass2;
pass1 = (FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text.Trim(),"MD5"));
pass2 = pass1.Substring(8,pass1.Length-16);
String strconn;
strconn = ConfigurationManager.ConnectionStrings["strconn"].ConnectionString;
SqlConnection conn = new SqlConnection(strconn);
String sqlstr;
sqlstr = "Insert into company (username,password,question,answer,country,sf,city,qymc,zczj,frdb,qylb,name,sex,fax,sortid,typeid,phone,qyjj,web,vip_flag,uflag,url,deng,email,address,mobile,gsrs,zycp,post,postdate) Values ('"+username.Text.Trim()+" ','"+pass2+"','"+qu.Text.Trim()+"','"+an.Text.Trim()+"','"+country.Text.Trim()+"','"+sf.Text.Trim()+"','"+city.Text.Trim()+" ','"+company.Text.Trim()+" ','"+zczj.Text.Trim()+" ','"+frdb.Text.Trim()+" ','"+leixin.SelectedItem.Text.Trim()+"','"+lname.Text.Trim()+" ','"+sex.SelectedItem.Text.Trim()+"','"+fax.Text.Trim()+"','"+DropDownList1.Text.Trim()+"','"+DropDownList2.Text.Trim()+"','" +phone.Text.Trim()+"','"+qyjj.Text.Trim()+"','0','0','未审核','0','0','"+email.Text.Trim()+"','"+address.Text.Trim()+"','"+phone2.Text.Trim()+"','"+gsrs.Text.Trim()+"','"+zycp.Text.Trim()+"','"+post.Text.Trim()+"','"+DateTime.Now.ToString()+"')";
conn.Open();
SqlCommand cmd1;
String strsql;
strsql = "select username from [company] where username = '" + username.Text + "'";
cmd1 = new SqlCommand(strsql, conn);
SqlDataReader rd;
rd = cmd1.ExecuteReader();
if (rd.Read())
{
Response.Write("<SCRIPT language=JavaScript>alert('此用户名已被注册!');</SCRIPT>");
return;
}
else
{
conn.Close();
conn.Open();
SqlCommand cmd;
cmd = new SqlCommand(sqlstr, conn);
if (tongyi.Checked == true)
{
cmd.ExecuteNonQuery();
}
conn.Close();
conn.Dispose();
Response.Redirect("successful_zc.aspx");
}
登陆按钮事件
public void checklogin_click(Object sender, EventArgs e)
{
String strconn;
strconn = ConfigurationManager.ConnectionStrings["strconn"].ConnectionString;
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd;
String sqlstr;
String pass1, pass2;
pass1 = (FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text.Trim(), "MD5"));
pass2 = pass1.Substring(8,pass1.Length-16);
sqlstr = "Select username,password From [company] where username = '" + username.Text + "' and password='" + pass2 + "'";
cmd = new SqlCommand(sqlstr,conn);
SqlDataReader rd;
conn.Open();
rd = cmd.ExecuteReader();
if (check.Text != Label1.Text)
{
jc.Text = "附加码输入错误!";
return;
}
if (rd.Read())
{
Session["UserName"]= username.Text;
String key;
key = Session["UserName"].ToString();
String strsql;
strsql = "update [company] set deng=deng+1 where username = '" + key + "' ";
conn.Close();
conn.Open();
cmd = new SqlCommand(strsql, conn);
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
Response.Redirect("../member/ad_xx.aspx");
}
else
{
jc.Text = "用户名或密码错误,请重新输入!";
}
conn.Close();
conn.Dispose();
}
展开全部
其实,在asp.net2。0就不建议使用Response.Write()
如果你使用了.net 3.5 new feature using a scriptmanager control你的code会出现运行时错误的
如果你使用了.net 3.5 new feature using a scriptmanager control你的code会出现运行时错误的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
* 输入用户名的文本框是txt_UserName;
* 输入真实姓名的文本框时txt_UserRegName
* 输入密码的文本框是txt_Password
* 注册提交按钮是btn_Register
*/
//注册提交按钮点击事件
private void btn_Register_Click(object sender, EventArgs e)
{
using(SqlConnection connection = new SqlConnection("[<数据库连接字符串>]"))//初始化到数据库的连接
{
try
{
connection.Open();//打开此数据库连接
SqlCommand sqlText = new SqlCommand("insert into [<用户名表>] values (@nm,@rn,@pw)", connection);//建立数据库命令对象
//添加数据库命令参数
sqlText.Parameters[0].Value = txt_UserName.Text;
sqlText.Parameters[1].Value = txt_UserRegName.Text;
sqlText.Parameters[2].Value = txt_Password.Text;
if (sqlText.ExecuteNonQuery())
MessageBox.Show(this, "注册失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
else
MessageBox.Show(this, "恭喜您,注册成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
catch (SqlException exception)
{
MessageBox.Show(this, "注册失败:\n" + exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
finally
{
connection.Close();
}
}
}
[<>]的不是代码,是要根据你自己情况写的
同样,SQL语句的参数也是.
* 输入用户名的文本框是txt_UserName;
* 输入真实姓名的文本框时txt_UserRegName
* 输入密码的文本框是txt_Password
* 注册提交按钮是btn_Register
*/
//注册提交按钮点击事件
private void btn_Register_Click(object sender, EventArgs e)
{
using(SqlConnection connection = new SqlConnection("[<数据库连接字符串>]"))//初始化到数据库的连接
{
try
{
connection.Open();//打开此数据库连接
SqlCommand sqlText = new SqlCommand("insert into [<用户名表>] values (@nm,@rn,@pw)", connection);//建立数据库命令对象
//添加数据库命令参数
sqlText.Parameters[0].Value = txt_UserName.Text;
sqlText.Parameters[1].Value = txt_UserRegName.Text;
sqlText.Parameters[2].Value = txt_Password.Text;
if (sqlText.ExecuteNonQuery())
MessageBox.Show(this, "注册失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
else
MessageBox.Show(this, "恭喜您,注册成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
catch (SqlException exception)
{
MessageBox.Show(this, "注册失败:\n" + exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
finally
{
connection.Close();
}
}
}
[<>]的不是代码,是要根据你自己情况写的
同样,SQL语句的参数也是.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
.......你随便拿一本讲C#的书上面基本上都有讲的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
确实很简单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询