0

好的,所以我做了两个表格form1和form2。Form1s 代码看起来像这样..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
using System.Data.SqlClient;

namespace ACIDeXe_DBTEST
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void nsButton1_Click(object sender, EventArgs e)
    {
        try
        {
            SHA1HASH();
            string myConnection = "datasource=localhost;port=3306;username=encrypti_dropem;password=PASSWORD";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            MySqlCommand SelectCommand = new MySqlCommand("select * from encrypti_dropem.users where username='" + this.username_txt.Text + "' and password='" + password_txt.Text + "' ;", myConn);

            MySqlDataReader myReader;
            myConn.Open();
            myReader = SelectCommand.ExecuteReader();
            int count = 0;
            while (myReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                Stresser hub = new Stresser(username_txt.Text);
                this.Hide();
                hub.Show();
            }
            else if (count > 1)
            {
                MessageBox.Show("Database error code: 1");
            }
            else
                MessageBox.Show("Invalid Username or Password");
            myConn.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
    }
    public void SHA1HASH()
    {
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        UTF8Encoding utf8 = new UTF8Encoding();
        string rpassword = BitConverter.ToString(sha1.ComputeHash(utf8.GetBytes(password_txt.Text)));
        string lpassword = rpassword.Replace("-", "");
        password_txt.Text = String.Empty;
        password_txt.Text = lpassword;
    }
}
}

所以它登录,并将用户名传递给 form2(目前)。我尝试使用此代码来获取用户的等级并直接传递,但我无法弄清楚..

string myConnection =  datasource=localhost;port=3306;username=encrypti_dropem;password=PASSWORD";       
            MySqlConnection myCon = new MySqlConnection(myConnection);
            MySqlCommand SelectCommand = new MySqlCommand("select * from         encrypti_dropem.users where username='" + label9.Text + "';", myCon);
            MySqlDataReader reader2;
            myCon.Open();
            reader2 = SelectCommand.ExecuteReader();
            while (reader2.Read())
            {
                string rankid = reader2.GetString("rank");              
            }
            myCon.Close();

在尝试学习 c# 时,我感觉好像错过了很多东西,比如在表单之间传递数据之类的?任何人都想帮助我朝着正确的方向前进。

TLDR;需要能够在整个表单中使用表单 2 中的字符串 rankid(在表单 1 上获得)。

4

1 回答 1

0

您可以使用 sesseion 变量将数据传递给其他形式。

string myConnection =  datasource=localhost;port=3306;username=encrypti_dropem;password=PASSWORD";       
        MySqlConnection myCon = new MySqlConnection(myConnection);
        MySqlCommand SelectCommand = new MySqlCommand("select * from         encrypti_dropem.users where username='" + label9.Text + "';", myCon);
        MySqlDataReader reader2;
        myCon.Open();
        reader2 = SelectCommand.ExecuteReader();
        while (reader2.Read())
        {
            Session["rankid"] = reader2.GetString("rank");              
        }
        myCon.Close();

在其他页面上,您可以通过以下代码访问 rankid:

      string strRank  =  Convert.ToString(Session["rankid"]);

对于 Window Form U 可以在 Form1 上使用以下代码,您可以在其中打开 form2:

    var frm2 = new Form2(reader2.GetString("rank"););
    frm2.Show();

在 Form2 上使用以下代码:

 public Form2(string s)
{
    InitializeComponent();
    textBox1.Text = s;
}

谢谢

于 2013-11-15T07:08:06.327 回答