登录界面连接数据库问题 数据库界面c#密码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就是winform+ado.net,你多去看看winform和ado.net的基础吧,没有学会走,你就开始跑了 所谓“业务逻辑层”,对于你来说,其实就是(在这里就仅仅有)一个 public static bool 验证用户登录(string userName, string password){ .....}这样的代码。它把什么“界面”跟什么“数据库”隔离开来,省得你“两头找借口”。如果你只是关心数据库操作,那么你应该单独提出一个关于如何“访问关系数据库然后返回一个 bool 值来判断登录结果”的问题。应该自己先设计程序流程,不要一味地求别人给你把“所有功能”最终代码写出来。 http://sxfenglei.blog.163.com/blog/static/1702604912009184345826/ using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace SelectCourse{ public partial class FrmInformation : Form { bool set = true; public FrmInformation() { InitializeComponent(); } private void FrmInformation_Load(object sender, EventArgs e) { try { string sqlStr = "select userName,name,answer1,answer2,answer3 from tbl_Information where userName='" + CPublic.userInfo[0] + "'"; SqlCommand cmd = new SqlCommand(sqlStr, CDataBase.conn); CDataBase.conn.Open(); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) { txtName.Text = sdr["name"].ToString().Trim(); txtAnswer1.Text = sdr["answer1"].ToString().Trim(); txtAnswer2.Text = sdr["answer2"].ToString().Trim(); txtAnswer3.Text = sdr["answer3"].ToString().Trim(); set = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { CDataBase.conn.Close(); } } private void btnSetInformation_Click(object sender, EventArgs e) { try { if(txtName.Text.Trim()=="") { MessageBox.Show("姓名不能为空","提示"); txtName.Focus(); return; } else if(txtAnswer1.Text.Trim()=="") { MessageBox.Show("请输入第一个问题的答案","提示"); txtAnswer1.Focus(); return; } else if(txtAnswer2.Text.Trim()=="") { MessageBox.Show("请输入第二个问题的答案","提示"); txtAnswer2.Focus(); return; } else if(txtAnswer3.Text.Trim() =="") { MessageBox.Show("请输入第三个问题的答案","提示"); txtAnswer3.Focus(); return; } string sqlStr; if(set) { sqlStr = "insert into tbl_Information values('"+CPublic.userInfo[0] + "','"+txtName.Text.Trim()+"','"+txtAnswer1.Text.Trim()+"','"+txtAnswer2.Text.Trim()+"','"+txtAnswer3.Text.Trim()+"')"; if(CDataBase.UpdateDB(sqlStr)) { MessageBox.Show("设置个人信息成功","恭喜"); set = false; sqlStr = "update tbl_User set firstLogin='否' where userName='" + CPublic.userInfo[0] + "'"; CDataBase.UpdateDB(sqlStr); } } else { sqlStr = "update tbl_Information set name= '"+txtName.Text.Trim()+"',Answer1='"+txtAnswer1.Text.Trim()+"',Answer2='"+txtAnswer2.Text.Trim()+"',Answer3='"+txtAnswer3.Text.Trim()+"'where userName='"+CPublic.userInfo[0]+"'"; if(CDataBase.UpdateDB(sqlStr)) { MessageBox.Show("修改个人信息成功","恭喜"); } } } catch(Exception ex) { CDataBase.conn.Close(); MessageBox.Show(ex.Message); } } private void btnDeleteInformation_Click(object sender, EventArgs e) { try { if (MessageBox.Show("删除个人信息后,一旦忘记用户密码,将无法找回,确定删除吗?", "删除个人信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { string sqlStr = "delete from tbl_Information where userName='" + CPublic.userInfo[0] + "'"; if (CDataBase.UpdateDB(sqlStr)) { txtName.Text = ""; txtAnswer1.Text = ""; txtAnswer2.Text = ""; txtAnswer3.Text = ""; sqlStr = "update tbl_User set firstLogin='是' where userName='" + CPublic.userInfo[0] + "'"; CDataBase.UpdateDB(sqlStr); MessageBox.Show("删除个人信息成功", "恭喜"); set = true; txtName.Focus(); } } } catch (Exception ex) { CDataBase.conn.Close(); MessageBox.Show(ex.Message); } } private void btnChagePassword_Click(object sender, EventArgs e) { try { if (CPublic.GetMd5Str(txtOldPassword.Text.Trim()) != CPublic.userInfo[1]) { MessageBox.Show("旧密码错误,请重新输入", "提示"); txtOldPassword.Text = ""; txtNewPassword1.Text = ""; txtNewPassword2.Text = ""; txtOldPassword.Focus(); } else if (txtNewPassword1.Text.Trim() == "" && txtNewPassword2.Text.Trim() == "") { MessageBox.Show("密码不能为空", "提示"); txtNewPassword1.Focus(); } else if (txtNewPassword1.Text.Trim() == txtNewPassword2.Text.Trim()) { string psw = CPublic.GetMd5Str(txtNewPassword1.Text.Trim()); string sqlStr = "update tbl_User set userPassword='" + psw.Trim() + "'where userName='" + CPublic.userInfo[0] + "'"; if (CDataBase.UpdateDB(sqlStr)) { CPublic.userInfo[1] = psw.Trim(); MessageBox.Show("修改成功,请记住您的新密码", "修改密码"); txtOldPassword.Text = ""; txtNewPassword1.Text = ""; txtNewPassword2.Text = ""; } } else { MessageBox.Show("两次输入的密码不一致", "提示"); txtNewPassword1.Text = ""; txtNewPassword2.Text = ""; txtNewPassword1.Focus(); } } catch (Exception ex) { CDataBase.conn.Close(); MessageBox.Show(ex.Message); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void txtNewPassword2_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnChagePassword_Click(sender, e); } } }} 登陆界面连接数据库实现登陆示例//登陆页面设置<asp:TextBox ID="txtUid" runat="server" Font-Size="9pt" Width="120px" BackColor="White"></asp:TextBox>//新用户登陆<asp:TextBox ID="txtPwd" runat="server" Font-Size="9pt" TextMode="Password" Width="120px" style="margin-left: 0px" BackColor="White"> </asp:TextBox> <asp:ImageButton ID="ImageManage" runat="server" ImageUrl="~/image/b1.jpg" OnClick="btnLoad_Click"/>//登陆按钮//连接数据库实现登陆 protected void btnLoad_Click(object sender, ImageClickEventArgs e) { DataSet ds = DB.reDs("select * from tb_HuenLian where UserName='" + txtUid.Text.Trim() + "' and PassWord='" + txtPwd.Text.Trim() + "'"); int i = this.checkLogin(txtUid.Text, txtPwd.Text); if (i > 0) { Session["id"] = ds.Tables[0].Rows[0][0].ToString(); Session["UserName"] = this.txtUid.Text; Session["PassWord"] = this.txtPwd.Text; Page.Response.Redirect("Yonghu.aspx"); } else { Response.Write("<script lanuage=javascript>alert('用户名称或密码错误!');location='javascript:history.go(-1)'</script>"); }//CodeGo.net/ } public int checkLogin(string loginName, string loginPwd)//查询数据表用户名、密码是否正确 { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]); SqlCommand myCommand = new SqlCommand("select count(*) from tb_HuenLian where UserName=@loginName and PassWord=@loginPwd", con); myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20)); myCommand.Parameters["@loginName"].Value = loginName; myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 50)); myCommand.Parameters["@loginPwd"].Value = loginPwd; myCommand.Connection.Open(); int i = (int)myCommand.ExecuteScalar(); myCommand.Connection.Close(); return i; } 请问如何通过客户端脚本判断是否触发某事件 我这的System.Windows.Forms名称空间下为什么没有WebBrowser和HTMLDocument之类的类 请问能用c#开发一个网络游戏的服务端吗?如果能该怎么做? 谁能解说一下下面的执行过程??? 如何使用operator == List<>读取问题 如何在ASP.NET中限制TextBox的输入内容? 如何抓取当前屏幕的快照? 大家来看下这个remoting问题有何解决方案? 问个C sharp一个小问题: 一个关联表的问题 c#帮助文件我看不懂?帮我看一下这段帮助如何理解。
{
.....
}这样的代码。它把什么“界面”跟什么“数据库”隔离开来,省得你“两头找借口”。如果你只是关心数据库操作,那么你应该单独提出一个关于如何“访问关系数据库然后返回一个 bool 值来判断登录结果”的问题。应该自己先设计程序流程,不要一味地求别人给你把“所有功能”最终代码写出来。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace SelectCourse
{
public partial class FrmInformation : Form
{
bool set = true;
public FrmInformation()
{
InitializeComponent();
} private void FrmInformation_Load(object sender, EventArgs e)
{
try
{
string sqlStr = "select userName,name,answer1,answer2,answer3 from tbl_Information where userName='" + CPublic.userInfo[0] + "'";
SqlCommand cmd = new SqlCommand(sqlStr, CDataBase.conn);
CDataBase.conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
txtName.Text = sdr["name"].ToString().Trim();
txtAnswer1.Text = sdr["answer1"].ToString().Trim();
txtAnswer2.Text = sdr["answer2"].ToString().Trim();
txtAnswer3.Text = sdr["answer3"].ToString().Trim();
set = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
CDataBase.conn.Close();
}
} private void btnSetInformation_Click(object sender, EventArgs e)
{
try
{
if(txtName.Text.Trim()=="")
{
MessageBox.Show("姓名不能为空","提示");
txtName.Focus();
return;
}
else if(txtAnswer1.Text.Trim()=="")
{
MessageBox.Show("请输入第一个问题的答案","提示");
txtAnswer1.Focus();
return;
}
else if(txtAnswer2.Text.Trim()=="")
{
MessageBox.Show("请输入第二个问题的答案","提示");
txtAnswer2.Focus();
return;
}
else if(txtAnswer3.Text.Trim() =="")
{
MessageBox.Show("请输入第三个问题的答案","提示");
txtAnswer3.Focus();
return;
}
string sqlStr;
if(set)
{
sqlStr = "insert into tbl_Information values('"+CPublic.userInfo[0] + "','"+txtName.Text.Trim()+"','"+txtAnswer1.Text.Trim()+"','"+txtAnswer2.Text.Trim()+"','"+txtAnswer3.Text.Trim()+"')";
if(CDataBase.UpdateDB(sqlStr))
{
MessageBox.Show("设置个人信息成功","恭喜");
set = false;
sqlStr = "update tbl_User set firstLogin='否' where userName='" + CPublic.userInfo[0] + "'";
CDataBase.UpdateDB(sqlStr);
}
}
else
{
sqlStr = "update tbl_Information set name= '"+txtName.Text.Trim()+"',Answer1='"+txtAnswer1.Text.Trim()+"',Answer2='"+txtAnswer2.Text.Trim()+"',Answer3='"+txtAnswer3.Text.Trim()+"'where userName='"+CPublic.userInfo[0]+"'";
if(CDataBase.UpdateDB(sqlStr))
{
MessageBox.Show("修改个人信息成功","恭喜");
}
}
}
catch(Exception ex)
{
CDataBase.conn.Close();
MessageBox.Show(ex.Message);
}
} private void btnDeleteInformation_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("删除个人信息后,一旦忘记用户密码,将无法找回,确定删除吗?", "删除个人信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
string sqlStr = "delete from tbl_Information where userName='" + CPublic.userInfo[0] + "'";
if (CDataBase.UpdateDB(sqlStr))
{
txtName.Text = "";
txtAnswer1.Text = "";
txtAnswer2.Text = "";
txtAnswer3.Text = "";
sqlStr = "update tbl_User set firstLogin='是' where userName='" + CPublic.userInfo[0] + "'";
CDataBase.UpdateDB(sqlStr);
MessageBox.Show("删除个人信息成功", "恭喜");
set = true;
txtName.Focus();
}
}
}
catch (Exception ex)
{
CDataBase.conn.Close();
MessageBox.Show(ex.Message);
}
} private void btnChagePassword_Click(object sender, EventArgs e)
{
try
{
if (CPublic.GetMd5Str(txtOldPassword.Text.Trim()) != CPublic.userInfo[1])
{
MessageBox.Show("旧密码错误,请重新输入", "提示");
txtOldPassword.Text = "";
txtNewPassword1.Text = "";
txtNewPassword2.Text = "";
txtOldPassword.Focus();
}
else if (txtNewPassword1.Text.Trim() == "" && txtNewPassword2.Text.Trim() == "")
{
MessageBox.Show("密码不能为空", "提示");
txtNewPassword1.Focus();
}
else if (txtNewPassword1.Text.Trim() == txtNewPassword2.Text.Trim())
{
string psw = CPublic.GetMd5Str(txtNewPassword1.Text.Trim());
string sqlStr = "update tbl_User set userPassword='" + psw.Trim() + "'where userName='" + CPublic.userInfo[0] + "'";
if (CDataBase.UpdateDB(sqlStr))
{
CPublic.userInfo[1] = psw.Trim();
MessageBox.Show("修改成功,请记住您的新密码", "修改密码");
txtOldPassword.Text = "";
txtNewPassword1.Text = "";
txtNewPassword2.Text = "";
}
}
else
{
MessageBox.Show("两次输入的密码不一致", "提示");
txtNewPassword1.Text = "";
txtNewPassword2.Text = "";
txtNewPassword1.Focus();
}
}
catch (Exception ex)
{
CDataBase.conn.Close();
MessageBox.Show(ex.Message);
}
} private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
} private void txtNewPassword2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnChagePassword_Click(sender, e);
}
}
}
}
//登陆页面设置
<asp:TextBox ID="txtUid" runat="server" Font-Size="9pt" Width="120px"
BackColor="White"></asp:TextBox>//新用户登陆
<asp:TextBox ID="txtPwd" runat="server" Font-Size="9pt" TextMode="Password"
Width="120px" style="margin-left: 0px" BackColor="White">
</asp:TextBox>
<asp:ImageButton ID="ImageManage" runat="server" ImageUrl="~/image/b1.jpg"
OnClick="btnLoad_Click"/>//登陆按钮
//连接数据库实现登陆
protected void btnLoad_Click(object sender, ImageClickEventArgs e)
{
DataSet ds = DB.reDs("select * from tb_HuenLian where UserName='" + txtUid.Text.Trim() + "' and PassWord='" + txtPwd.Text.Trim() + "'");
int i = this.checkLogin(txtUid.Text, txtPwd.Text);
if (i > 0)
{
Session["id"] = ds.Tables[0].Rows[0][0].ToString();
Session["UserName"] = this.txtUid.Text;
Session["PassWord"] = this.txtPwd.Text;
Page.Response.Redirect("Yonghu.aspx");
}
else
{
Response.Write("<script lanuage=javascript>alert('用户名称或密码错误!');location='javascript:history.go(-1)'</script>");
}//CodeGo.net/
}
public int checkLogin(string loginName, string loginPwd)//查询数据表用户名、密码是否正确
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
SqlCommand myCommand = new SqlCommand("select count(*) from tb_HuenLian where UserName=@loginName and PassWord=@loginPwd", con);
myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20));
myCommand.Parameters["@loginName"].Value = loginName;
myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 50));
myCommand.Parameters["@loginPwd"].Value = loginPwd;
myCommand.Connection.Open();
int i = (int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
return i;
}