ADO.Net中的类型化DataSet登录
时间:2011-06-15 来源:smxlcs
1.新建 DL.mdf
2.Program.cs中加入“神奇代码”
3.添加数据集 DataSetUsers.xsd
FillByUserName.....的sql语句 SELECT Id, Name, Password, ErrorTimes FROM dbo.T_Users
where Name=@Name
IncErrorTimesByID的sql语句 UPDATE [dbo].[T_Users] SET ErrorTimes=ErrorTimes+1 WHERE (Id = @Id)
ResetErrorTimes的sql语句 UPDATE [dbo].[T_Users] SET ErrorTimes=0 WHERE (Id = @Id)
4.在form1上拖两个textbox name为txtUserName txtPasword 拖一个 button
5.Form1.cs 代码
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 强类型登录.DataSetUsersTableAdapters;
namespace 强类型登录
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
T_UsersTableAdapter adapter = new T_UsersTableAdapter();
强类型登录.DataSetUsers.T_UsersDataTable users = adapter.GetDataByUserName(txtUserName.Text);
if (users.Count <= 0)
{
MessageBox.Show("用户名错误 ");
}
else
{
强类型登录.DataSetUsers.T_UsersRow user = users[0];
if (user.ErrorTimes > 3)
{
MessageBox.Show("错误次数过多");
return;
}
if (user.Password == txtPassword.Text)
{
MessageBox.Show("登录成功");
adapter.ResetErrorTimes(user.Id);
}
else
{
adapter.IncErrorTimesByID(user.Id );
MessageBox.Show("密码错误");
}
}
}
}
}
感谢杨中科老师的视频 第五季