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

时间:2020-03-19 06:06:22

标签: c# sql-server class

这周我刚刚开始使用C#和SQL进行编码,以创建一个桌面应用程序。登录后,我想将登录其他表格的用户数据放入仪表板,但是我找不到解决方法。我找到了一种创建类并将该用户数据放入其中的方法,以便您可以抓取;但我真的被困在这里。

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

2 个答案:

答案 0 :(得分:2)

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

//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;
     }
     ....

答案 1 :(得分:2)

修改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");