【问题标题】:Passing MySql DB data between forms在表单之间传递 MySql DB 数据
【发布时间】:2013-11-15 07:02:10
【问题描述】:

好的,所以我做了两个表格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;需要能够在整个表单中使用 form2 中的字符串 rankid(在 form1 上获得)。

【问题讨论】:

    标签: c# mysql database forms


    【解决方案1】:

    您可以使用 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"]);
    

    对于窗体 U 可以在打开窗体 2 的窗体 1 上使用以下代码:

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

    在 Form2 上使用以下代码:

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

    谢谢

    【讨论】:

    • 我不断收到当前上下文中不存在会话。我尝试寻找解决方案找不到任何.. 有这方面的个人经验吗?
    • 嗨,你可以使用我在我的回答中提到的窗口形式的代码。会话用于 Web 应用程序。请使用以下代码: var frm2 = new Form2(reader2.GetString("rank");); frm2.Show();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多