⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 数据库c#的登录和注册
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Windowszhongyi2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
                String username, password,textBox2 ;
                username = this.username.Text;
                password = this.pwd1.Text;
                textBox2 = this.textBox2.Text;
                if (textBox2 == "" && username==""&&password == "")
                {
                    message.Text = "您未注册帐户";
                }

                else if (textBox2 == "")
                {

                    try
                    {
                        SqlConnection con = new SqlConnection("Server=.;uid=sa;pwd='';database=登陆系统");
                        SqlCommand cmd = new SqlCommand("select count(*)from t_nzhy where username='" + username + "'", con);
                        con.Open();
                        int count = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                        if (count > 0)
                        {
                            message.Text = "这个帐户已存在";

                        }
                        else
                        {
                            String strCon = "server=.;uid=sa;pwd='';database=登陆系统";
                            SqlConnection conn = new SqlConnection(strCon);
                            String strsql = "insert into t_nzhy(username,[password]) values('" + this.username.Text + "','" + pwd1.Text + "')";
                            SqlCommand cmmd = new SqlCommand(strsql, conn);
                            conn.Open();
                            cmmd.ExecuteNonQuery();
                            conn.Close();
                            message.Text = "注册成功";

                        }

                        this.username.Clear();
                        this.pwd1.Clear();


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("错误" + ex.ToString(), "错误");
                    }
                }
                else if (username == "")
                {
                    
                    try
                    {
                        SqlConnection con = new SqlConnection("Server=.;uid=sa;pwd='';database=登陆系统");
                        SqlCommand cmd = new SqlCommand("select count (*) from t_nzhy where username='" + textBox2 + "'and password='" + password  + "'", con);
                        con.Open();
                        int count = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                        if (count > 0)
                        {
                            message.Text = "登录成功";
                        }
                        else
                        {
                            message.Text = "你的帐户或密码出错";

                        }
                        this.textBox2.Clear();
                        this.pwd1.Clear();
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
       
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    

      
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -