-
May 20th, 2011, 08:45 AM
#1
Read Database Data
Hello all. I am new here and I appreciate any attention and responces that you give. I am simply trying to use ACE OLEDB provider to establish a connection to a backend Access database for reading and writing to. I am having trouble understanding where I need to go with this to get it to read Names from a table I have specified and populate a combo box with the results. Here is what I have so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CalendarDemo
{
public partial class Add_Event : Form
{
private OleDbConnection mycon;
public Add_Event()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Add_Event_Load(object sender, EventArgs e)
{
string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Charles Branham\Desktop
\System_Windows_Forms_Calendar_0031\System.Windows.Forms.Calendar
\CalendarDemo\SystemData.accdb;Persist Security Info=True";
string strAccessSelect = @"SELECT [Last Name], Location FROM tblPersonal WHERE (Location = 'FMB')";
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
myAccessConn = new OleDbConnection(strConnection);
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
//DataColumnCollection dr = myDataSet.Tables["tblPersonal"].Columns;
myDataAdapter.Fill(myDataSet);
DataRowCollection dr = myDataAdapter
Console.WriteLine(dr.Count);
//MessageBox.Show("Number of Records:" + dr.Count, "Testing");
//mycon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Charles Branham\Desktop
//\System_Windows_Forms_Calendar_0031\System.Windows.Forms.Calendar
//\CalendarDemo\SystemData.accdb;Persist Security Info=True");
}
}
}
Thank you,
TheChazm
-
May 20th, 2011, 09:40 AM
#2
Re: Read Database Data
You can change this to your use but should give you a good idea.
Code:
OleDbConnection conn = new OleDbConnection("provider = microsoft.jet.oledb.4.0;data source = " + Directory.GetCurrentDirectory() + "\\Data.mdb" + ";");
OleDbCommand cmd = new OleDbCommand("SELECT Location, Last_Name FROM tblPersonal WHERE Location='FMB'", conn);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//Just making sure I dont get blank data
if (reader.GetValue(1).ToString() != string.Empty)
{
combobox1.Items.Add(reader.GetValue(1).ToString());
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|