如何将 acces 数据库的字段表放入列表框中?

发布于 2024-07-30 04:56:31 字数 1465 浏览 3 评论 0原文

需要创建一个表单,在其中我必须浏览和打开 mdb 文件 ---> 我使用 oprnfile 对话完成了这部分!

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog oDlg = new OpenFileDialog();
    oDlg.Title = "Select MDB";
    oDlg.Filter = "MDB (*.Mdb)|*.mdb";
    oDlg.RestoreDirectory = true;

    string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    oDlg.InitialDirectory = dir;

    DialogResult result = oDlg.ShowDialog();

    if (result == DialogResult.OK)
    {
        textBox1.Text = oDlg.FileName.ToString();
    }

    string strFileName = oDlg.FileName.ToString();

    OleDbConnection cn = new OleDbConnection();

    DataTable schemaTable;
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Mode=Share Deny None;Data Source="+strFileName;
    cn.Open();

    schemaTable = cn.GetOleDbSchemaTable(

    OleDbSchemaGuid.Tables,

    new Object[] { null, null, null, "TABLE" });

    //List the table name from each row in the schema table.

    for (int i = 0; i < schemaTable.Rows.Count; i++)
    {
         strTableName = schemaTable.Rows[i].ItemArray[2].ToString();
        listBox1.Items.Add(strTableName);
    }

    //Explicitly close - don't wait on garbage collection.

    cn.Close();

}

这是我到目前为止的代码!!!

现在我需要制作 3 个列表框!

第一个显示数据库的表名称<-----到这里就完成了!!!!

第二步,点击表名时显示字段名!!!---> 完成! 第三步在单击字段时显示属性!!!---> 完成!

v 可以编辑属性值,然后单击“保存”按钮,它应该更新数据库!

need to create a form in which i hav to browse and open mdb files ---> i did this part usin oprnfile dialogue!

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog oDlg = new OpenFileDialog();
    oDlg.Title = "Select MDB";
    oDlg.Filter = "MDB (*.Mdb)|*.mdb";
    oDlg.RestoreDirectory = true;

    string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    oDlg.InitialDirectory = dir;

    DialogResult result = oDlg.ShowDialog();

    if (result == DialogResult.OK)
    {
        textBox1.Text = oDlg.FileName.ToString();
    }

    string strFileName = oDlg.FileName.ToString();

    OleDbConnection cn = new OleDbConnection();

    DataTable schemaTable;
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Mode=Share Deny None;Data Source="+strFileName;
    cn.Open();

    schemaTable = cn.GetOleDbSchemaTable(

    OleDbSchemaGuid.Tables,

    new Object[] { null, null, null, "TABLE" });

    //List the table name from each row in the schema table.

    for (int i = 0; i < schemaTable.Rows.Count; i++)
    {
         strTableName = schemaTable.Rows[i].ItemArray[2].ToString();
        listBox1.Items.Add(strTableName);
    }

    //Explicitly close - don't wait on garbage collection.

    cn.Close();

}

this is my code so far!!!

now i need to make 3 list boxes!!

1st one to display the table names of the db<-----done till here!!!!

2nd to to display field names when clicked on table name!!!---> to b done!
3rd to display attributes on fiels on clickin on it!!!---> to b done!

v can edit the attribute values and on clickin of save button it should update the database!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-08-06 04:56:31

检查 schemaTable 对象的列数组:

for(int i=0;i<schemaTable.Columns.Length;i++)
    listBox2.Items.Add(schemaTable.Columns[i].ToString());

Check the columns array of your schemaTable object:

for(int i=0;i<schemaTable.Columns.Length;i++)
    listBox2.Items.Add(schemaTable.Columns[i].ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文