60 likes | 195 Views
Simple MS Access operations. Monday Nov 14, 2005. Sample Application. Step One: create the database. Connecting to the Database. private void Form1_Load(object sender, System.EventArgs e) { // connect to the existing Access Database string cmd = "Select name,qnty from Table1";
E N D
Simple MS Access operations Monday Nov 14, 2005
Connecting to the Database private void Form1_Load(object sender, System.EventArgs e) { // connect to the existing Access Database string cmd = "Select name,qnty from Table1"; string connect = "provider=Microsoft.JET.OLEDB.4.0; data source = c:\\products.mdb"; myAdapter = new OleDbDataAdapter(cmd, connect); // get the data from the database DataSet mydata = new DataSet(); myAdapter.Fill (mydata, "Table1"); mytable = mydata.Tables[0]; // fill up the comboBox with the rows foreach (DataRow dataRow in mytable.Rows) { comboBox1.Items.Add(dataRow["name"]); } }
Really Simple Query private void btn_quantity_Click(object sender, System.EventArgs e) { // connect again string cmd = "Select name,qnty from Table1"; string connect = "provider=Microsoft.JET.OLEDB.4.0; data source = c:\\products.mdb"; myAdapter = new OleDbDataAdapter(cmd, connect); // get a fresh copy of the database DataSet mydata = new DataSet(); myAdapter.Fill (mydata, "Table1"); mytable = mydata.Tables[0]; // look for the item and show its qnty foreach (DataRow dataRow in mytable.Rows) { if (String.Compare(dataRow["name"].ToString(),comboBox1.Text) == 0) MessageBox.Show("There are " + dataRow["qnty"].ToString(), } }
Really Simple Remove private void btn_Delete_Click(object sender, System.EventArgs e) { // update the database foreach (DataRow dataRow in mytable.Rows) { if (String.Compare(dataRow["name"].ToString(),comboBox1.Text) == 0) dataRow.Delete(); } comboBox1.Items.Remove(comboBox1.Text); }