2012-07-23 47 views
1

首先我想道歉,因爲我很新的C#。這可能聽起來像一個愚蠢的問題;然而,我一直無法在互聯網上的任何地方或在C#上購買的幾本書中找到任何示例。我有一個窗體可以從SQL數據庫中獲取相當多的數據。第一個和最後一個名字被插入到兩個獨立的組合框中,一個將顯示信息爲「First Last」和另一個「Last,First」,以便於瀏覽名稱。組合框項目到多個文本框

由於一些幫助在這裏一天,我學會了如何填充一個文本框與組合框的選擇值。再次感謝您的幫助。

我需要做的是當從下拉框中選擇一個用戶,所有用戶的信息將在窗體上某些文本框內顯示。例子是,

LastName = textBox1 
FirstName = textBox2 
MiddleName = textBox3 

我是否需要寫一個單獨的字符串,其中的每個字段,或者可以將數據從該查詢所有數據的SQL數據庫一根弦拉?

您提供的任何援助將不勝感激。

這裏是我迄今爲止

enter code here 

    namespace Tempus.Menus 
{ 
public partial class Employees : Form 
{ 
    public Employees() 
    { 
     InitializeComponent(); 


     //Connect to database for Employees Table Headers 
     SqlConnection myConnection = new SqlConnection(@"Server=Server4\INSTANCE;Integrated Security=true;" + 
      "user id=userID;password=password;" + 
      "Trusted_Connection=yes;" + 
      "Database=Database;" + 
      "connection timeout=30"); 

     try 
     { 
      myConnection.Open(); 
      string SqlDataPull = String.Format("SELECT * FROM Employees WHERE Lname IS NOT NULL {0}", (checkBox1.Checked ? "AND Active='Y'" : "")); 
      //string SqlDataPull2 = String.Format("SELECT * FROM Employees WHERE Fname IS NOT NULL {0} ORDER By Fname", (checkBox1.Checked ? "AND Active='Y'" : "")); 
      SqlCommand cmd = new SqlCommand(SqlDataPull, myConnection); 
      cmd.CommandType = CommandType.Text; 
      SqlDataReader dr = cmd.ExecuteReader(); 



      while (dr.Read()) 
      { 
       string strEmployee = String.Format("{0} {1}", dr["Fname"], dr["Lname"], dr["Mname"], dr["DOH"], dr["DOT"], dr["Active"], dr["DoNotRehireReason"], dr["PTO-balance"], dr["SNP-balance"], dr["Cred"]); 

       comboBox1.Items.Add(strEmployee); 

       string strEmployee2 = String.Format("{0}, {1}", dr["Lname"], dr["Fname"]); 

       comboBox2.Items.Add(strEmployee2); 

       int Fname = dr.GetInt32(0); 
       string firstName = String.Format("{0}", dr["Fname"]); 

      } 


     } 
     catch (Exception e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 
     finally 
     { 
      if (myConnection != null) 
      { 
       myConnection.Dispose(); 
      } 


     } 
     comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged; 

     comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged; 
    } 
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedIndex == -1) 
     { 
      textBox1.Text = string.Empty; 
     } 
     else 
     { 

      textBox1.Text = comboBox1.SelectedItem.ToString(); 

     } 
    } 


    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox2.SelectedIndex == -1) 
     { 
      textBox1.Text = string.Empty; 
     } 
     else 
     { 

      textBox1.Text = comboBox2.SelectedItem.ToString(); 
     } 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void Employees_Load(object sender, EventArgs e) 
    { 

    } 


    private void label1_Click(object sender, EventArgs e) 
    { 

    } 






    private void button1_Click(object sender, EventArgs e) 
    { 
     Main myNewForm = new Main(); 

     myNewForm.Show(); 

     this.Close(); 
    } 


    private void button2_Click(object sender, EventArgs e) 
    { 
     Reports myNewForm = new Reports(); 

     myNewForm.Show(); 

     this.Close(); 
    } 

    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox3_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox8_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox9_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox7_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox10_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void checkBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     if (checkBox1.Checked) 

       { 

      // logic here for if the box has now been checked 


       } 

      else 

       { 

       // what to do if the box has been unchecked 


       } 

     } 
    } 
} 
+0

@codingbiz:OK,林」有麻煩複制和粘貼我的代碼在此評論框.. – 2012-07-23 18:40:49

+0

不要粘貼註釋。編輯原始問題。 – banging 2012-07-23 18:44:45

+0

請將您的UI邏輯與控制器邏輯分開。將它們寫入兩個不同的類 – nawfal 2012-07-23 19:25:12

回答

2
根據您提供的上下文

,這裏是我的意見。請合併適用的要點並忽略其他要點。希望它也能回答你的問題。

  • 這是一個單層應用程序。建議看看分層結構(閱讀更多here),這在可維護性,可讀性等方面更好。

  • 動態SQL在許多方面被認爲是不良的(包括存在安全風險),所以看看好處由存儲過程(或LINQ-to-SQL或任何ORM提供者)存儲。文章是一個不錯的起點。

  • 你做一個SELECT * FROM EMPLOYEE並閱讀10列(可能更多),但只使用FNameLName列。建議使用類似SELECT FName, LName FROM EMPLOYEE(在一個存儲過程或等價物中)並將它們填充到具有數據層中相關屬性的對象中。在UI層,也可以使一個單一的調用來獲取數據,並結合相同的數據到這兩個組合框與適當的格式(一個空間,而另一個用逗號)。

  • 最後,在數據層中有第二種方法來獲取給定員工(比如基於姓氏)的詳細信息(所有列映射到correpsonding屬性?)。在UI層中,根據組合框中的選定項目,您可以獲取相應的員工詳細信息並將其顯示在文本框中。

如果你不希望走這條路線,並希望「速戰速決」,請記住,該方法將是非常低效的,很難維護,容易出現安全問題。在這裏,你只要把相同的數據庫調用再次在組合框(使用WHERE條款)所選擇的值和細節綁定到文本框。

希望這會有所幫助。

+0

非常感謝您的回覆。我會看看你提到的網站。正如我所說的,我對C#非常陌生,以及一般編程和渴望學習。 – 2012-07-23 19:26:10

+0

很高興幫助。自己嘗試一下,這是最好的學習方式。 – Channs 2012-07-23 19:30:15

2

我建議建立一個Employee類

class Employee 
{ 
    public string firstName { get; set; } 
    public string middleName { get; set;} 
    public string lastName { get; set; } 
    public string fullName { get; set; } 

    //add other attributes here just the same... 

    public Employee(string first, string middle, string last, string full) 
    { 
     firstName = first; 
     middleName = middle; 
     lastName = last; 
     fullName = full; 

     //assign ther other attributes.. 
    } 


} 

中,你可以創建列表List<Employee> employees = new List<Employee>();您返回的SQL數據添加到...

SqlDataAdapter da = new SqlDataAdapter(SqlDataPull, myConnection); 
DataSet ds = new DataSet(); 
da.Fill(ds, "Employees"); 

foreach (DataRow row in ds.Tables["Employees"].Rows) 
{ 
    employees.Add(dr["Fname"], dr["Lname"], dr["Mname"], dr["FullName"]); 
} 

您可以在SQL返回全名查詢RTRIM(Lname) + ',' + RTRIM(Fname) AS FullName

然後,您可以添加該列表作爲comboBoxes的dataSource並引用選定的索引以將該值添加到您的t分機盒。

+0

我要在哪裏添加此行? employees.Add(DR [其中 「fname」],博士[ 「L-NAME」],博士[ 「MNAME」],博士[ 「全名」]);無論我如何試圖合併,我得到一個錯誤,「爲方法,沒有超載‘添加’需要3個arguements。有什麼建議? – 2012-07-23 20:49:27

+0

你會使用DataSet獲得來自各行各值和我添加編輯我的帖子告訴你這是如何完成的。 – Spacemancraig 2012-07-24 12:50:55

1

不熟悉使用SQL,但是如果您擁有組合框中需要的所有信息,則可以使用已有的事件。

comboBox1.SelectedIndexChanged + = comboBox1_SelectedIndexChanged; comboBox2.SelectedIndexChanged + = comboBox2_SelectedIndexChanged;

它看起來像comboBox1包含員工的所有信息,而comboBox2只包含名和姓。基於你用來填充組合框的while循環。

因此,如果用戶將在的SelectedIndexChanged點擊選擇在comboBox1你可以這樣做:

if(comboBox1.SelectedIndex != -1) 
    { 
     string[]parts = comboBox1.Items[comboBox1.SelectedIndex].ToString().Split(' '); 
     textBox1.Text = parts[0];//Index for First Name 
     textBox2.Text = parts[1];//Index for Last Name 
     textBox3.Text = parts[2];//Index for Middle Name 
    } 

注:我用.Split(」「),這將分裂的空間的字符串,但當您從組合框中檢索文本時,使用分隔不同數據片段的字符。

不完全確定你正在試圖用SQL來做什麼等等,但是如果你從SQL中加載後你有組合框中的數據,你可以很容易地獲得這些數據並將它們放入適當的文本框中不得不從數據庫重新加載。

希望這會有所幫助。