DataSet的封装。用户账户登陆

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];

                    }

                }

            }

        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值