从SQL Server 2008R2使用的WinForms检索数据(Retrieving data

2019-10-29 03:46发布

我创建一个C#Windows窗体应用程序,它会从已在SQL server2008r2创建DB检索数据。 有我的应用程序2种胜形式,第一次一个是用于获取登录信息,第二个是一个用于显示是有关给定用户名和密码的数据。 我无法将数据得到我的第二个形式。 这是我的代码:

** * **** 1形式 * ** *

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

* **** 第二表格 * *

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

Answer 1:

为什么不不改变你的构造你的欢迎形式接收参数。

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

所以,当你打电话,你这样做:

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

然后你像欢迎的形式添加属性:

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

然后添加在欢迎形式负载的2个参数:

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


Answer 2:

你可以使用构造函数来解决问题

当你点击“登录”按钮(假设有一个),你可以:

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

你的第二个窗体的构造可以是:

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

还有其他的方法来做到这一点。 http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms



Answer 3:

当然,你不能在第二种形式从数据库中检索数据,因为你不

1 - 你没有送x和y参数为第二形态

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

2-必须通过x和y在查询SqlCommand并执行查询

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

最终代码

在形成一个

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

在两个表格

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


文章来源: Retrieving data from SQL server 2008r2 using winforms