简体   繁体   English

使用Winforms从SQL Server 2008r2检索数据

[英]Retrieving data from SQL server 2008r2 using winforms

I am creating ac# windows form app, which will retrieve the data from a DB that is already created in sql server2008r2. 我正在创建ac#Windows窗体应用程序,它将从已在sql server2008r2中创建的数据库中检索数据。 There are 2 win forms in my app, 1st one is for getting the login details, the 2nd one is for display the data that are relevant to given UserID & Password. 我的应用程序中有2种获胜形式,第一种是获取登录详细信息,第二种是显示与给定UserID和密码相关的数据。 I can't get the data to my 2nd form. 我无法将数据保存到第二个表格中。 This is my code: 这是我的代码:

** * **** 1st form * ** * ** * **** 第一种形式 * ** *

 public partial class FormLog_in : Form
 {
    SqlConnection con = new SqlConnection("Data source=CHINTHAK-PC ; Initial Catalog=FlintecTest; Integrated Security = yes;");
    SqlDataAdapter da = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand();

    public FormLog_in()
    {
        InitializeComponent();
    }

    private void btnLogIn_Click(object sender, EventArgs e)
    {
        if (!(string.IsNullOrEmpty(txtUserName.Text)) && !(string.IsNullOrEmpty(txtPassword.Text)))
        {
            con.Open();
            string query = "SELECT count(*) FROM LogIn  WHERE UesrName=@1 AND PassWord=@2 ";
            cmd = new SqlCommand(query, con);
            cmd.Parameters.Add("@1", SqlDbType.NVarChar).Value = txtUserName.Text;
            cmd.Parameters.Add("@2", SqlDbType.NVarChar).Value = txtPassword.Text;
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();

            if (count > 0)
            {
                MessageBox.Show("Valid Username and Password");
                Welcome f1 = new Welcome();
                f1.Show();

            }
            else
                MessageBox.Show("Invalid Username or Password try again");
        }

* **** 2nd form * * * **** 第二种形式 * *

public partial class Welcome : Form
{
    string query = null;

    SqlConnection con = new SqlConnection("Data source=CHINTHAK-PC ; Initial Catalog=FlintecTest; Integrated Security = yes;");
    SqlDataAdapter da = new SqlDataAdapter();
    BindingSource userTable = new BindingSource();
    DataSet ds = new DataSet();

    public Welcome()
    {
        InitializeComponent();

    }

    private void Welcome_Load(object sender, EventArgs e)
    {
        query = "SELECT * FROM  Users WHERE UserName=@x AND Users.Password=@y ";//x should be given username by current login
        da.SelectCommand = new SqlCommand(query, con);                          //y should be given password by current login
        ds.Clear();
        da.Fill(ds, "usr");
        userTable.DataSource = ds.Tables["usr"];

        txtFristName.DataBindings.Add(new Binding("Text", userTable, "FirstName"));
        txtLastName.DataBindings.Add(new Binding("Text", userTable, "LastName"));
        txtAddress.DataBindings.Add(new Binding("Text", userTable, "Address"));
        txtTelephone.DataBindings.Add(new Binding("Text", userTable, "Telephone"));
        txtEmail.DataBindings.Add(new Binding("Text", userTable, "Email"));
        txtFax.DataBindings.Add(new Binding("Text", userTable, "Fax"));
        txtSection.DataBindings.Add(new Binding("Text", userTable, "Section"));
        txtPosition.DataBindings.Add(new Binding("Text", userTable, "Position"));
    }
}

Why not not change your constructor in your welcome form to receive parameters. 为什么不更改您的欢迎表单中的构造函数以接收参数。

 public Welcome(String usr, String pword)
{
    InitializeComponent();
    this.Username=usr;
    this.Password=pword; // you should have a form of encryption for your password
}

So, when you call, you do: 因此,当您致电时,您会执行以下操作:

Welcome f1 = new Welcome(txtUsername.Text,txtPassword.Text);

and then you add properties in welcome form like: 然后以欢迎形式添加属性,例如:

private String Username { get; set; }
private String Password { get; set; }

then add the 2 parameters in welcome form load: 然后在欢迎表单加载中添加2个参数:

cmd.Parameters.Add("@x", SqlDbType.NVarChar).Value = Username;
cmd.Parameters.Add("@y", SqlDbType.NVarChar).Value = Password;

You could use constructors to solve your problem 您可以使用构造函数来解决您的问题

When you hit the "login" button (supposing that there's one) you could: 当您点击“登录”按钮(假设有一个按钮)时,您可以:

private void login_Click(object sender, System.EventArgs e)
{
    Form2 frm=new Form2(userName.text,password.tex);
    frm.Show();
}

Your second form constructor could be: 您的第二个表单构造函数可以是:

public Form2(string user, string pass)
{
  InitializeComponent(); 
  //Save your parameters here so you can use them with the query
}

There are other ways to do this. 还有其他方法可以做到这一点。 http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

Of course you cannot retrieve data from database in second form since you are not 当然,您不能以第二种形式从数据库检索数据

1- you didn't send the x and y parameters to to the second Form 1-您没有将x和y参数发送到第二个表单

Form2 frm = new Form2(userName.text,password.tex);
frm.Show();

2- must pass x and y to the query in SqlCommand and execute the query 2-必须将x和y传递给SqlCommand的查询并执行查询

query = "SELECT * FROM  Users WHERE UserName='"+ x +"'AND Password='"+y+"'";

Final code 最终代码

in Form one 在表格一中

if (count > 0)
{ 
    MessageBox.Show("Valid Username and Password");
    Welcome f1 = new Welcome( txtUserName.Text,txtPassword.Text);
    f1.Show();
}

in Form Two 在表格二中

string x, y;

public Welcome(String usr, String pword)
{
    InitializeComponent();
    x = usr; y = pword;
}

private void Welcome_Load(object sender, EventArgs e)
{
    query = "SELECT * FROM  Users WHERE UserName='"+ x +"'AND Password='"+y+"'";

    cmd.SelectCommand = new SqlCommand(query ,con);

    ds.Clear();
    cmd.Fill(ds);

    userTable.DataSource = ds.Tables[0];

    txtFristName.DataBindings.Add(new Binding("Text", userTable, "FirstName"));
    txtLastName.DataBindings.Add(new Binding("Text", userTable, "LastName"));
    txtAddress.DataBindings.Add(new Binding("Text", userTable, "Address"));
    txtTelephone.DataBindings.Add(new Binding("Text", userTable, "Telephone"));
    txtEmail.DataBindings.Add(new Binding("Text", userTable, "Email"));
    txtFax.DataBindings.Add(new Binding("Text", userTable, "Fax"));
    txtSection.DataBindings.Add(new Binding("Text", userTable, "Section"));
    txtPosition.DataBindings.Add(new Binding("Text", userTable, "Position"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM