2013-11-15 52 views
0

好吧,我做了兩種形式form1和form2。 Form1s代碼看起來像這樣..在表單之間傳遞MySql DB數據

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上獲得)ALL THROUGHOUT表單。

回答

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

而且

對於Windows窗體U可以使用下面Form1上的代碼,其中u打開窗口2:

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

和On Form2使用以下代碼:

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

謝謝

+0

我一直在獲取Session在當前上下文中不存在。我試圖尋找解決方案找不到任何..任何與此有關的個人經驗? – WhatsRighteous

+0

你好U可以使用Window窗體的代碼,正如我在我的ans中提到的。會話用於Web應用程序。使用下面的代碼:var frm2 = new Form2(reader2.GetString(「rank」);); frm2.Show(); – Hitesh