One thing that sticks out like a sore thumb is SQLInjection and the use of parameterized queries, for example:
OleDbCommand command = new OleDbCommand("SELECT * from Users WHERE LastName='@1'", connection); command.Parameters.AddWithValue("@1", textBox8.Text)
What you do is perfectly acceptable, although usually you are better off using an SQL database.
Edit: This is how you separate your business logic from the GUI:
Class BusLogic { public List<string> ListboxItems = new List<string>(); public void PopulateListBoxItems(string userName) { string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\redgabanan\Desktop\Gabanan_Red_dbaseCon\Red_Database.accdb"; using (OleDbConnection connection = new OleDbConnection(connString)) { connection.Open(); OleDbDataReader reader = null; OleDbCommand command = new OleDbCommand("SELECT * from Users WHERE LastName='@1'", connection); command.Parameters.AddWithValue("@1", userName) reader = command.ExecuteReader(); while (reader.Read()) { ListboxItems.Add(reader[1].ToString()+","+reader[2].ToString()); } } } }
GUI
private void button3_Click(object sender, EventArgs e) { var busLogic = new BusLogic(); busLogic.PopulateListBoxItems(textBox8.Text); \\listBox1.Items.Clear(); ListboxItems.DataSource = busLogic.ListboxItems; }
Jeremy thompson
source share