您的位置:首页 > 编程语言 > C#

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)

        {

            

        }

       

 

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# winform 登录注册