c#窗体程序注册登陆功能实现的代码怎么写? 5
用c#写一个窗体程序,链接到SqlServer,实现注册和登陆,能够现实注册成功信息,登陆成功信息,现在注册写好了,登陆的代码怎么写呢,这是注册的源码.usingSyst...
用c#写一个窗体程序,链接到SqlServer,实现注册和登陆,能够现实注册成功信息,登陆成功信息,现在注册写好了,登陆的代码怎么写呢,这是注册的源码.
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string cnString = @"Data Source=.\sqlexpress;Initial Catalog=login;Integrated Security=True";
private void btnReg_Click(object sender, EventArgs e)
{
if (txtName.Text.Trim() == "")
{
lblInfo.Text = "姓名不能为空!请输入姓名。";
txtName.Focus();
btnReg.Enabled = false;
return;
}
if (txtPwd.Text.Trim() == "")
{
lblInfo.Text = "密码不能为空!请输入密码。";
txtPwd.Focus();
btnReg.Enabled = false;
return;
}
else
{
btnReg.Enabled = true;
}
SqlConnection cn = new SqlConnection();
cn.ConnectionString = cnString;
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
{
cmd.CommandText = "Insert Into Users(name, password) Values('" + txtName.Text.Trim() + "','" + txtPwd.Text.Trim() + "')";
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblInfo.Text = "注册成功!";
}
else
{
lblInfo.Text = "注册失败!";
}
}
}
最好扣扣我2533812884 好友 谢谢 能详细的说下。 展开
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string cnString = @"Data Source=.\sqlexpress;Initial Catalog=login;Integrated Security=True";
private void btnReg_Click(object sender, EventArgs e)
{
if (txtName.Text.Trim() == "")
{
lblInfo.Text = "姓名不能为空!请输入姓名。";
txtName.Focus();
btnReg.Enabled = false;
return;
}
if (txtPwd.Text.Trim() == "")
{
lblInfo.Text = "密码不能为空!请输入密码。";
txtPwd.Focus();
btnReg.Enabled = false;
return;
}
else
{
btnReg.Enabled = true;
}
SqlConnection cn = new SqlConnection();
cn.ConnectionString = cnString;
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
{
cmd.CommandText = "Insert Into Users(name, password) Values('" + txtName.Text.Trim() + "','" + txtPwd.Text.Trim() + "')";
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblInfo.Text = "注册成功!";
}
else
{
lblInfo.Text = "注册失败!";
}
}
}
最好扣扣我2533812884 好友 谢谢 能详细的说下。 展开
2个回答
展开全部
我给你把sql语句写了吧。
string sql="select count(*) from Users where name='"+name+"' and password='"+password+"' ";
cmd.ExecuteScalar();
然后根据返回值判断
if(num>1)
{
登陆成功..
}
string sql="select count(*) from Users where name='"+name+"' and password='"+password+"' ";
cmd.ExecuteScalar();
然后根据返回值判断
if(num>1)
{
登陆成功..
}
追问
能否加下你 企 鹅 呢 我是初学者,可以明白你说的意思但是我不会写代码啊,能否在详细的辅导下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace AC {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
string uname = this.textBox1.Text.ToString(); string upassword = this.textBox2.Text.ToString();
SqlConnection thisconnection = new SqlConnection(@"Server=SQL连接名;Integrated Security=True;" + "DataBase=数据库名"); thisconnection.Open();
SqlCommand thiscommand = thisconnection.CreateCommand();
thiscommand.CommandText = "select ID,PS from 表名 where ID='" + uname + " '"; SqlDataReader thisreader = thiscommand.ExecuteReader(); if (thisreader.Read()) {
if (thisreader["PS"].ToString().Trim() == upassword) {
MessageBox.Show("恭喜您登陆成功!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
//yhxx tt = new yhxx(); //tt.Show();
//this.Close();//关闭当前窗体 //new Form1().Hide();
this.Visible = false;//当前窗口不可见
new Form2().Show();//form2显示new Form2().Show(); }
else {
this.textBox2.Text = "";
MessageBox.Show("密码错误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); }
}
else {
this.textBox1.Text = ""; this.textBox2.Text = "";
MessageBox.Show("此用户不存在,请您注册!", "注册", MessageBoxButtons.OK, MessageBoxIcon.Information); }
thisconnection.Close(); thisreader.Close(); }
// this.Visible = false;//当前窗口不可见 // new Form2().Show();//form2显示
private void textBox2_TextChanged(object sender, EventArgs e) {
}
private void groupBox1_Enter(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e) {
//this.Visible = false;//当前窗口不可见 //new Form2().Show();//form2显示
//this.Close();//关闭当前窗体 //new Form2().Show(); //Form2.show { };
//this.Dispose(); //释放内存,比第一个好。 Application.Exit();//关闭整个程序 }
private void button3_Click(object sender, EventArgs e) {
this.Visible = false; new Form2().Show(); }
private void button4_Click(object sender, EventArgs e) {
new Form5().Show(); } } }
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace AC {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
string uname = this.textBox1.Text.ToString(); string upassword = this.textBox2.Text.ToString();
SqlConnection thisconnection = new SqlConnection(@"Server=SQL连接名;Integrated Security=True;" + "DataBase=数据库名"); thisconnection.Open();
SqlCommand thiscommand = thisconnection.CreateCommand();
thiscommand.CommandText = "select ID,PS from 表名 where ID='" + uname + " '"; SqlDataReader thisreader = thiscommand.ExecuteReader(); if (thisreader.Read()) {
if (thisreader["PS"].ToString().Trim() == upassword) {
MessageBox.Show("恭喜您登陆成功!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
//yhxx tt = new yhxx(); //tt.Show();
//this.Close();//关闭当前窗体 //new Form1().Hide();
this.Visible = false;//当前窗口不可见
new Form2().Show();//form2显示new Form2().Show(); }
else {
this.textBox2.Text = "";
MessageBox.Show("密码错误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); }
}
else {
this.textBox1.Text = ""; this.textBox2.Text = "";
MessageBox.Show("此用户不存在,请您注册!", "注册", MessageBoxButtons.OK, MessageBoxIcon.Information); }
thisconnection.Close(); thisreader.Close(); }
// this.Visible = false;//当前窗口不可见 // new Form2().Show();//form2显示
private void textBox2_TextChanged(object sender, EventArgs e) {
}
private void groupBox1_Enter(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e) {
//this.Visible = false;//当前窗口不可见 //new Form2().Show();//form2显示
//this.Close();//关闭当前窗体 //new Form2().Show(); //Form2.show { };
//this.Dispose(); //释放内存,比第一个好。 Application.Exit();//关闭整个程序 }
private void button3_Click(object sender, EventArgs e) {
this.Visible = false; new Form2().Show(); }
private void button4_Click(object sender, EventArgs e) {
new Form5().Show(); } } }
追问
大师,这是不是有点太高级了,能否再简单下呢,几行代码让刚才注册的账号密码能登陆成功就行了。
追答
这个直接COPY进去就可以用了~把引用添加上就可以。不是有注释么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询