开发者

data from access db file

I am unsure but i am unable to get the data to show in the text box. Here is the code i have written so far. Any help would be great. There is nothing going in the boxes but doing the test i got the message box. Is there something i am not doing correct ? I can provide the access file if needed. But is is just five fields with data in it.

DataSet DataSet1; //use to put data in form

System.Data.OleDb.OleDbDataAdapter dataadapter;

private void Breed_Load(object sender, EventArgs e)
{
    dbconnect = new System.Data.OleDb.OleDbConnection();//database connection variable
    DataSet1 = new DataSet(); //variable to help get info 开发者_JS百科from DB

    dbconnect.ConnectionString = "PROVIDER= Microsoft.Jet.OLEDB.4.0; Data Source=C:/Pets.mdb"; //location of DB to open

    dbconnect.Open(); //open command for DB

    string sql = "SELECT * From tblPets"; //sql string to select all records from the table pets
    dataadapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbconnect); // pulls the records from sql command

    MessageBox.Show("Database is Open");

    dataadapter.Fill(DataSet1, "Pets"); // used the database to fill in the form.
    NavRecords(); //calls NavRecords Method

    dbconnect.Close();

   MessageBox.Show("Database is Closed");

    dbconnect.Dispose(); 


}


private void NavRecords()
{
    DataRow DBrow = DataSet1.Tables["Pets"].Rows[0];

    //PetNametextBox.Text = DBrow.ItemArray.GetValue(1).ToString(); //puts data in textbox
    TypeofPettextBox.Text = DBrow.ItemArray.GetValue(1).ToString();//puts data in textbox
    PetWeighttextBox.Text = DBrow.ItemArray.GetValue(2).ToString();//puts data in textbox
    ShotsUpdatedtextBox.Text = DBrow.ItemArray.GetValue(3).ToString();//puts data in textbox
    AdoptabletextBox.Text = DBrow.ItemArray.GetValue(4).ToString();//puts data in textbox
    BreedtextBox.Text = DBrow.ItemArray.GetValue(5).ToString();//puts data in textbox
}


Fetching data from an Access database is fairly simple. Here is an example:

public static DataTable GetBySQLStatement(string SQLText)
{
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Pets.MDB";
    System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection();

    Conn.ConnectionString = ConnectionString;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = Conn;
    cmd.CommandText = SQLText;

    DataSet ds;
    System.Data.OleDb.OleDbDataAdapter da;
    DataTable Table = null;

    Conn.Open();
    da = new System.Data.OleDb.OleDbDataAdapter();
    da.SelectCommand = cmd;
    ds = new DataSet();
    da.Fill(ds);

    if (ds.Tables.Count > 0)
        Table = ds.Tables[0];
    Conn.Close();
    return Table;
}

You call this function like so:

DataTable dt = GetBySQLStatement("SELECT * FROM tblPets");

if (dt != null) {
    // If all goes well, execution should get to this line and
    // You can pull your data from dt, like dt[0][0]
}

The only "gotcha" to be aware of is that this code must be compiled as a 32-bit application because there are no 64-bit Jet drivers. By default, Visual Studio will compile as a mixed 32-bit and 64-bit program. Change the option in your project settings to make sure its 32-bit only.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜