Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# access数据库文件中的数据_C#_Database_Ms Access - Fatal编程技术网

C# access数据库文件中的数据

C# access数据库文件中的数据,c#,database,ms-access,C#,Database,Ms Access,我不确定,但无法获取文本框中显示的数据。这是我到目前为止编写的代码。任何帮助都会很好。盒子里什么都没有,只是做了测试,我得到了消息框。有什么事情我做得不对吗?如果需要,我可以提供访问文件。但它只有五个字段,其中包含数据 DataSet DataSet1; //use to put data in form System.Data.OleDb.OleDbDataAdapter dataadapter; private void Breed_Load(object sender, EventAr

我不确定,但无法获取文本框中显示的数据。这是我到目前为止编写的代码。任何帮助都会很好。盒子里什么都没有,只是做了测试,我得到了消息框。有什么事情我做得不对吗?如果需要,我可以提供访问文件。但它只有五个字段,其中包含数据

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 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
}

从Access数据库获取数据相当简单。以下是一个例子:

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;
}
您可以这样调用此函数:

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]
}

唯一需要注意的是,此代码必须编译为32位应用程序,因为没有64位Jet驱动程序。默认情况下,VisualStudio将编译为32位和64位的混合程序。更改项目设置中的选项以确保其仅为32位。

为什么不设置断点并检查该行是否实际包含数据。考虑使用“dBrw(0).toStRun()),也应该考虑使用DATAGIDVIEW。我设置断点,没有看到任何东西。但我并不是百分之百地去寻找。好的,我设置了断点,看到数据库正在打开,然后断点将使用数据库填充表单,但当它到达navrecords方法时,我什么也看不到。我不理解dbrow to string,但会尝试。没有64位Jet驱动程序,但您可以在64位版本中使用ACE(可从MS下载)在64位上下文中访问MDB文件。