【第二次实验进度博客】C#登录界面可视化


第二次更新:今天又趁着课间找了找问题,终于找到了:

主要原因在于

我发现和原来的form1这不太一样所以按着form1的格式改成了

"insert into login1(account,pass) values('"+s1+"','"+s2+"')"

果然成功了!

同时对if部分进行修改:

            SqlCommand sqlCommand = new SqlCommand(sql , sqlConnection);
            //SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//上面注释部分只有在select时才有返回值,通过查阅资料,判断插入是否成功使用了以下语句
            if (sqlCommand.ExecuteNonQuery()!=0)    //返回值为受影响的行数
            {
                MessageBox.Show("注册成功!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);             //登录成功
                //  label1.Text = "Log in :" + username;
                //Form2 form2 = new Form2();
                //form2.Show();
                this.Hide();
                Form1 form1 = new Form1();
                form1.Show();
            }
            else
            {
                MessageBox.Show("注册失败!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            sqlConnection.Close();

本次使用数据库来和登陆界面的窗口连接起来。
首先在数据库中创建账户管理库:

create table login1(account char(13),
    pass char(13)
    )
insert into login1(account,pass) values('qqabc','123')
insert into login1(account,pass) values('12345','qq123')


 

最终效果:

登陆成功:

本想仿照着连接数据库获取账户密码的方法做一个注册的页面,将下面的text1和text2里的数据插入到数据库login1中,不过始终都有错误:

查了一些资料,感觉可能是数据库里面的定义的数据类型是char,而在注册页面里的text框里获取的字符类型为string,本想把数据库里的数据类型也改成string却发现数据库里的数据类型没有string。这部分有点超出我的认知范围了,注册还是等后面学完如何插入数据后再来继续修改吧。

Form1代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Sign sign = new Sign();
            sign.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string username = textBoxUserName.Text.Trim();  //取出账号

            string password = textBoxPassWord.Text.Trim();  //取出密码



            //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串

            string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=admin";



            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象

            sqlConnection.Open();



            string sql = "select account,pass from login1 where account = '" + username + "' and pass = '" + password + "'";                                            //编写SQL命令

            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);



            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();



            if (sqlDataReader.HasRows)

            {

                MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);             //登录成功

                //  label1.Text = "Log in :" + username;

                //Form2 form2 = new Form2();

                //form2.Show();

                this.Hide();
                FormMain formMain = new FormMain();
                formMain.Show();

            }

            else

            {

                MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            sqlConnection.Close();
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

FormMain代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginDemo
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“tESTDataSet.Student”中。您可以根据需要移动或删除它。
            this.studentTableAdapter.Fill(this.tESTDataSet.Student);

        }
    }
}

注册部分代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LoginDemo
{
    public partial class Sign : Form
    {
        public Sign()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text.Trim();
            string s2 = textBox2.Text.Trim();
            string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=admin";
            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
            sqlConnection.Open();
            string sql = "insert into login1(account,pass) values(s1,s2)";                                            //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.HasRows)
            {
                MessageBox.Show("注册成功!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);             //登录成功
                //  label1.Text = "Log in :" + username;
                //Form2 form2 = new Form2();
                //form2.Show();
                this.Hide();
                Form1 form1 = new Form1();
                form1.Show();
            }
            else
            {
                MessageBox.Show("注册失败!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            sqlConnection.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}


 

原创文章 25 获赞 20 访问量 9727

猜你喜欢

转载自blog.csdn.net/Freedomhy/article/details/105957337
今日推荐