C# 登录到其他表单C后发送数据#

C# 登录到其他表单C后发送数据#,c#,sql-server,class,C#,Sql Server,Class,本周我刚开始用C#和SQL编写代码,以创建一个桌面应用程序;登录后,我想将登录其他表单#dashboard的用户数据放入,但我找不到这样做的方法。我找到了一种方法来创建一个类,并将用户数据放入其中,以便您可以获取;但我真的被困在这里了 public void bunifuFlatButton1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=DESKTOP-R3I

本周我刚开始用C#和SQL编写代码,以创建一个桌面应用程序;登录后,我想将登录其他表单#dashboard的用户数据放入,但我找不到这样做的方法。我找到了一种方法来创建一个类,并将用户数据放入其中,以便您可以获取;但我真的被困在这里了

public void bunifuFlatButton1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-R3ILNJ7;Initial Catalog=Project2;Integrated Security=True");
    String query = "SELECT * FROM USERDB WHERE PRENOM='" + alphaBlendTextBox1.Text + "'AND PASS='" + alphaBlendTextBox2.Text + "'";

    SqlDataAdapter sda = new SqlDataAdapter(query, con);
    DataTable dtbl = new DataTable();
    sda.Fill(dtbl);

    if(dtbl.Rows.Count == 1) 
    {
        string name = dtbl.Rows[0]["Nom"].ToString();
        this.Hide(); 

        Form1 f1 = new Form1();
        f1.ShowDialog();
    }
    else
        MessageBox.Show("mauvais password try again"); 
}

一种方法是创建一个包含从DB读取的数据的对象,然后将其传递到新表单的构造函数中

//This class will store the data from the DB
public class MyClass
{
    Public string Name { get; set; }
    //Repeat for all fields retrieved from the DB that you require.

    public MyClass() 
    {

    }
}


  //I changed below to have Using clauses.  The way you had it you were not correctly disposing your objects and disconnecting from the DB, 
//and you would have memory leaks and other problems later
DataTable dtbl = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-R3ILNJ7;Initial Catalog=Project2;Integrated Security=True"))
{       
    //I Changed this to use Parameters!
    //See https://www.dreamincode.net/forums/topic/268104-the-right-way-to-query-a-database-parameterizing-your-sql-queries/

    String query = "SELECT * FROM USERDB WHERE PRENOM= @PRENOM AND PASS= @PASS";      
    using (SqlCommand command = new SqlCommand(query, con)) 
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(command))
        {               
            //Check the SQLDbType below is correct for you DB schema!  
            sda.SelectCommand.Parameters.Add("@PRENOM", SqlDbType.NVarChar).Value = alphaBlendTextBox1.Text; 
            sda.SelectCommand.Parameters.Add("@PASS", SqlDbType.NVarChar).Value = alphaBlendTextBox2.Text; 
            sda.Fill(dtbl);             
        }
    }
}


//Declare your class here
MyClass mc = new MyClass();

if(dtbl.Rows.Count == 1) 
{
    mc.Name = dtbl.Rows[0]["Nom"].ToString();

    Form1 f1 = new Form1(mc);
    this.Hide(); 
    f1.ShowDialog();
}
else
    MessageBox.Show("mauvais password try again");

dtbl = null;


//Now update your Form code and create a new constructor
public partial class Form1 : Form
{
     //This is where you will store the incoming data
     private MyClass IncomingMyClass { get; set; }

     //Change the existing constructor to Private
     private Form1()
     {
         InitializeComponent();
     }

     //Create a new constructor, which calls the empty (now private) constructor above
     public Form1(MyClass myclass): this()
     {
        this.IncomingMyClass = myclass;
     }
     ....

修改Form1的构造函数,并在创建Form1的对象时传递可在Form1中使用的值。以下是您的Form1的示例代码:

namespace YourNameSpace
{
    public partial class Form1 : Form
    {
        DataTable MyDataTable = new DataTable();

        public Form1(DataTable _MyDataTable)
        {
            InitializeComponent();

            MyDataTable = _MyDataTable;           
        }
    }
}
然后更改代码以将值传递到此表单,如下所示:

if(dtbl.Rows.Count == 1) 
{
    string name = dtbl.Rows[0]["Nom"].ToString();        
    this.Hide();
    Form1 f1 = new Form1(dtbl);
    f1.ShowDialog();
}
else
    MessageBox.Show("mauvais password try again");

如果你刚刚开始学习-从一开始就要正确!永远不要像这样构建SQL语句您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-尽管
AddWithValue
也是
SqlDbType,但请签出。不建议使用Text
,您应该使用
SqlDbType.NVarChar,-1
@DaleK:)再次更新!