在表单之间传递MySql DB数据

时间:2013-11-15 07:02:11

标签: c# mysql database forms

好吧所以我做了两个表单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(在form1上获得),直到形式。

1 个答案:

答案 0 :(得分:0)

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

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

对于窗体窗体U可以在Form1上使用下面的代码,其中你打开form2:

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

和在Form2上使用以下代码:

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

由于