通过c#增删查改数据库,并且显示在datagridview上,

2024-12-18 02:18:34
推荐回答(2个)
回答1:

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 WindowsFormsApplication1
{
    public partial class 用户登录 : Form
    {
        public 用户登录()
        {
            InitializeComponent();
        }
        private void Incerrortimes()
        {
            using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                con.Open();
                using (SqlCommand updateCmd = con.CreateCommand())
                {
                    updateCmd.CommandText = "update.Table1 Set errortimes 困姿=errortimes + 1 where username = @username";
                    updateCmd.Parameters.Add(new SqlParameter("username", textBox1.Text));
                    updateCmd.ExecuteNonQuery();
                }
            }
        }

        private void Reseterrortimes()
        {
            using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database2.mdf;Integrated Security=True;User Instance=True"))
            {
                con.Open();
                using (SqlCommand updateCmd = con.CreateCommand())
    信尺慧            {
                    updateCmd.CommandText = "update.table1 Set errortimes =0 where username = @username";
                    updateCmd.Parameters.Add(new SqlParameter("username", textBox1.Text));
                    updateCmd.ExecuteNonQuery();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database2.mdf;Integrated Security=True;User Instance=True"))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
   滑答                 cmd.CommandText = "select * from table1 where username = @username";
                    cmd.Parameters.Add(new SqlParameter("username", textBox1.Text));
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            int errortimes = reader.GetInt32 (reader.GetOrdinal("errortimes"));
                            if (errortimes > 3)
                            {
                                MessageBox.Show("登录错误次数过多,禁止登陆");
                                return;                                
                            }
                            string dbpassword = reader.GetString(reader.GetOrdinal("password"));
                            if (textBox2 .Text  == dbpassword)
                            {
                                MessageBox.Show("登录成功");
                                Reseterrortimes();
                                this.Hide();
                                Form1  f1 = new Form1();
                                this.Visible = false;
                                f1.ShowDialog();
                                con.Close();
                                this.Show();
                            }
                            else
                            {
                                Incerrortimes();
                                MessageBox.Show("密码错误");
                                textBox2.Text = "";
                                textBox2.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("用户不存在,请重新输入");
                            textBox1.Text = "";
                            textBox2.Text = "";
                        }
                    }
                }
            }
        }
     
    }
 }

回答2:

所以问题是什么