2013-11-15 48 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