2013-05-15 64 views
0

我正在創建一個C#窗體窗體應用程序,它將從已在sql server2008r2中創建的數據庫檢索數據。在我的應用程序中有兩個獲勝表格,第一個用於獲取登錄信息,第二個用於顯示與給定UserID &密碼相關的數據。我無法將數據提交給我的第二個表單。這是我的代碼:使用Winforms從SQL Server 2008r2檢索數據

** * ****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 [email protected] AND [email protected] "; 
      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 [email protected] AND [email protected] ";//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")); 
    } 
} 
+1

與命令第2形態 – Habib

+0

@Mora您不添加參數 - 包含在'Welcome_Load'does任何事情,因爲參數列表是空的查詢。讓我們以純文本的形式存儲密碼,這是一個可怕的想法,不管這是用於什麼。 **無論賬號用於什麼密碼都應以純文本格式保存。** –

回答

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; 
0

你可以使用構造函數來解決問題

當你點擊 「登錄」 按鈕(假設有一個),你可以:

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

0

當然你c在第二種形式ANNOT從數據庫中檢索數據,因爲你不是

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")); 
} 
相關問題