c# 编写winform登录注册程序(一,登录部分)
2013-09-27 15:09
846 查看
学习c#编程语言不久,尝试编写一个学生信息管理系统的form程序。在这跟大家分享一下。如有不足之处,请轻喷。谢谢!
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)//确定,登录
{
try
{
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空");
}
else
{
if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空!");
}
else
{
//登录是一定要连接数据库验证的,所以要建立sql数据库,建立登录信息的表admin_login,vs2010自带的sql数据库足够我们用,所以用起来也比较方便。
string admin_id = textBox1.Text;
string admin_psw = textBox2.Text;
string conn = "Data Source=LJQA2ILTGDR8QRN;Initial Catalog=admin_login;Integrated Security=True";//数据库的连接,source是服务器的名字,catalog是 //数据库的名字,Integrated Security表示windows系统验证。其实这个可以在数据库属性中直接粘贴过来,非常方便。
SqlConnection connection = new SqlConnection(conn);
connection.Open();//一定要记得打开连接
string sql = string.Format("select count(*) from admin where admin_id='{0}' and admin_psw='{1}'", admin_id , admin_psw);//sql语句,将指定的
string中
//的每个格式项替换为相应对象的值的文本等效项。
SqlCommand command = new SqlCommand(sql, connection);//sqlcommand表示要向向数据库执行sql语句或存储过程,
int i = Convert.ToInt32(command.ExecuteScalar());
if (i > 0)
{
MessageBox.Show("登录成功!");
Form3 f3 = new Form3();
this.Hide();
f3.ShowDialog();
}
else
{
MessageBox.Show("用户名或者密码错误!");
}
connection.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("异常错误" + ex);//显示出错的原因
}
finally
{ }
}
private void button2_Click(object sender, EventArgs e)//取消,关闭登录界面
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)//注册,切换到form2(注册form窗口)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)//确定,登录
{
try
{
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空");
}
else
{
if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空!");
}
else
{
//登录是一定要连接数据库验证的,所以要建立sql数据库,建立登录信息的表admin_login,vs2010自带的sql数据库足够我们用,所以用起来也比较方便。
string admin_id = textBox1.Text;
string admin_psw = textBox2.Text;
string conn = "Data Source=LJQA2ILTGDR8QRN;Initial Catalog=admin_login;Integrated Security=True";//数据库的连接,source是服务器的名字,catalog是 //数据库的名字,Integrated Security表示windows系统验证。其实这个可以在数据库属性中直接粘贴过来,非常方便。
SqlConnection connection = new SqlConnection(conn);
connection.Open();//一定要记得打开连接
string sql = string.Format("select count(*) from admin where admin_id='{0}' and admin_psw='{1}'", admin_id , admin_psw);//sql语句,将指定的
string中
//的每个格式项替换为相应对象的值的文本等效项。
SqlCommand command = new SqlCommand(sql, connection);//sqlcommand表示要向向数据库执行sql语句或存储过程,
int i = Convert.ToInt32(command.ExecuteScalar());
if (i > 0)
{
MessageBox.Show("登录成功!");
Form3 f3 = new Form3();
this.Hide();
f3.ShowDialog();
}
else
{
MessageBox.Show("用户名或者密码错误!");
}
connection.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("异常错误" + ex);//显示出错的原因
}
finally
{ }
}
private void button2_Click(object sender, EventArgs e)//取消,关闭登录界面
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)//注册,切换到form2(注册form窗口)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
相关文章推荐
- c# 编写winform登录注册程序(二,注册部分)
- 初学python-练习_4使用python编写本地登录程序(带注册)
- 如何在C#Winform程序中正确使用登录窗体
- 浅谈如何在C#Winform程序中正确使用登录窗体
- C#编写的winform程序打包方法
- 利用C#编写修改注册表的程序代码
- C# Winform程序 使用登录窗体到主窗体 且传登录信息
- C#编写的winform程序绑定comboBox成功,插入默认选项"请选择"
- 实现记录和查看用户的系统登录和退出历史和利用C#编写的网络聊天程序
- C# winform 编写一键排班软件遇到的程序问题
- 浅谈如何在C#Winform程序中正确使用登录窗体
- 比较经典的为C# Winform程序设置登录窗体先启动,成功后再启动主窗口
- 浅谈:如何用C#实现简单的带有验证码及密码使用MD5加密的“登录”WinForm小程序
- C#编写的winform程序脱离.net框架
- C#基础:用记事本编写简单WinForm窗体程序
- c# 实现 通过写入注册表 记住 winform程序 登录名
- Python小练习 - 用字典编写简单的注册、登录程序
- 利用C#编写修改注册表的程序代码
- C#编写的winform程序绑定comboBox成功,添加一个默认的文字选项"请选择"
- C# Winform程序设置登录窗体先启动