using System.Data .SqlClient ;
using System.Configuration ;
namespace DateSet封装及更新
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoid button1_Click_1(objectsender,EventArgs e)
{
DataTabledt= SQLHelper.ExecuteDatable("select * from t_user where name=@name",newSqlParameter("name", txtUserName.Text));
if(dt.Rows.Count <= 0)
{
MessageBox.Show("用户名不存在");
return;
}
DataRow row=dt .Rows [0];
if(Convert.ToInt32(row["ErrorPawItems"]) >= 3)
{
MessageBox.Show("错误次数过多,请稍后再试");
return;
}
if(Convert.ToString(row["password"]) == texUserPaw.Text)
{
MessageBox.Show("登陆成功");
SQLHelper.ExecuteNonQuery("update t_user set errorpawitems=0 wherename=@name",new SqlParameter("name",txtUserName.Text));
}
else
{
MessageBox.Show("密码错误");
texUserPaw.Text = "";
SQLHelper.ExecuteNonQuery("update t_user set ErrorPawitems=errorpawitems+1where name=@name",newSqlParameter ("name",txtUserName.Text ));
}
}
privatevoid Form1_Load (objectsender,EventArgs e)
{
//将TextBox控件的 PasswordChar属性设置为*号,用*来屏蔽用户输入的密码。以规避输入密码时被别人偷窥。哈哈
texUserPaw.PasswordChar = '*';
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
class SQLHelper
{
public static intExecuteNonQuery(string sql,paramsSqlParameter[]pars)
{
using(SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString))
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
foreach(SqlParameter parinpars)
{
cmd.Parameters.Add(par);
}
returncmd .ExecuteNonQuery();
}
}
}
public static DataTableExecuteDatable(string sql,paramsSqlParameter[]pars)
{
using(SqlConnection conn=newSqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString))
{
conn.Open();
using(SqlCommand cmd=conn .CreateCommand())
{
cmd .CommandText=sql;
foreach(SqlParameter parinpars )
{
cmd .Parameters.Add(par);
}
DataSet ds=newDataSet ();
using(SqlDataAdapter da=newSqlDataAdapter (cmd ))
{
da .Fill (ds );
return ds .Tables [0];
}
}
}
}
}
}