通过c#增删查改数据库,并且显示在datagridview上,
通过c#增删查改数据库,并且显示在datagridview上,表是登陆表,有两列数据,登录名和密码...
通过c#增删查改数据库,并且显示在datagridview上,表是登陆表,有两列数据,登录名和密码
展开
2个回答
展开全部
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 = "";
}
}
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询