private bool newUser(string username, string pw, int type)
 {
     try
     {
         DataRow row = dataset.Tables["users"].NewRow();
         row["users_name"]     = username;
         row["users_password"] = PasswordUtil.toMd5(pw);
         row["users_type"]     = type;
         dataset.Tables["users"].Rows.Add(row);
         dataAdapter.Update(dataset, "users");
     }
     catch (Exception e)
     {
         MessageBox.Show(e.ToString());
         return(false);
     }
     return(true);
 }
        private void bt2_Click(object sender, EventArgs e)
        {
            DataRow[] rows = dataset.Tables["users"].Select("users_name = '" + tb_username2.Text + "'");
            if (rows.Count() == 1)
            {
                if (string.Compare(rows[0]["users_password"].ToString(), PasswordUtil.toMd5(tb_oldpw2.Text)) == 0)
                {
                    rows[0]["users_password"] = PasswordUtil.toMd5(tb_newpw2.Text);
                    dataAdapter.Update(dataset, "users");

                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("旧密码输入错误");
                }
            }
        }
Esempio n. 3
0
        private void login(string username, string pw)
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder();

            sqlbuilder.DataSource         = cb_server_ip.Text;
            sqlbuilder.InitialCatalog     = "PosDB"; //数据库名
            sqlbuilder.IntegratedSecurity = true;

            using (SqlConnection connection = new SqlConnection())
            {
                try
                {
                    connection.ConnectionString = sqlbuilder.ConnectionString;
                    connection.Open();
                }
                catch (Exception e)
                {
                    MessageBox.Show("连接至服务器失败,请检查连接.\n" + e.ToString(), "连接错误");
                }

                if (connection.State != ConnectionState.Open)
                {
                    MessageBox.Show("连接至服务器失败,请检查连接.", "连接错误");
                    return;
                }

                string md5Pw = PasswordUtil.toMd5(pw);

                StringBuilder builder = new StringBuilder();
                builder.Append("select * from tb_users where users_name = ");
                builder.Append("@users_name ");
                builder.Append("and users_password = "******"@users_pw");
                SqlParameter para1 = new SqlParameter("@users_name", SqlDbType.Char);
                SqlParameter para2 = new SqlParameter("@users_pw", SqlDbType.Char);

                para1.Value = username;
                para2.Value = md5Pw;

                SqlCommand cmd = new SqlCommand(builder.ToString(), connection);

                cmd.Parameters.Add(para1);
                cmd.Parameters.Add(para2);

                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    //检查身份
                    int userType = Convert.ToInt32(reader.GetValue(3));

                    if (rb_stock.Checked)
                    {
                        if (userType != 2 && userType != 3)
                        {
                            MessageBox.Show("权限不足");
                            return;
                        }
                        else
                        {
                            Thread ShowMainThread = new Thread(
                                new ThreadStart(delegate { System.Windows.Forms.Application.Run(new PurchaseForm(username)); }));
                            ShowMainThread.SetApartmentState(ApartmentState.STA);
                            Connection.ServerIP = cb_server_ip.Text;
                            ShowMainThread.Start();
                            this.Close();
                        }
                    }
                    else
                    {
                        Thread ShowMainThread = new Thread(
                            new ThreadStart(delegate { System.Windows.Forms.Application.Run(new UsersManagerForm(userType)); }));
                        ShowMainThread.SetApartmentState(ApartmentState.STA);
                        Connection.ServerIP = cb_server_ip.Text;
                        ShowMainThread.Start();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码错误!", "错误");
                }
            }
        }