如何使用 .NET 操作 Access 数据库中的表

发布于 2024-09-27 11:31:27 字数 197 浏览 3 评论 0原文

的连接。

OleDbConnection con = OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source=" + mdbFileName);

我在访问数据库中有一个名为“Streets”的表,并且我已打开与“如何访问我的表?”

I have a table named "Streets" in an access db, and I have opened a connection with

OleDbConnection con = OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source=" + mdbFileName);

How can I get at my table?

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

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

发布评论

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

评论(2

心房的律动 2024-10-04 11:31:27

您需要“更改”表并使用 ExecuteNonQuery。代码如下:

Dim dbName As String = "<path>\mdbFileName.mdb"
Dim tmpConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbName & ";Persist Security Info=False"

Dim sqlText As String = ""
Using cn As New OleDbConnection(tmpConString)
  cn.Open()
  sqlText = "ALTER TABLE Table1 ADD COLUMN fldNew TEXT(50)"

  Using cm As New OleDbCommand(sqlText, cn)
    cm.ExecuteNonQuery()
  End Using
End Using

这将向您的数据库添加一个名为“fldNew”的新列。然后,您可以使用标准 UPDATE SQL 命令将数据添加到新列中。

You need to 'ALTER' the table and use the ExecuteNonQuery. Code below:

Dim dbName As String = "<path>\mdbFileName.mdb"
Dim tmpConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbName & ";Persist Security Info=False"

Dim sqlText As String = ""
Using cn As New OleDbConnection(tmpConString)
  cn.Open()
  sqlText = "ALTER TABLE Table1 ADD COLUMN fldNew TEXT(50)"

  Using cm As New OleDbCommand(sqlText, cn)
    cm.ExecuteNonQuery()
  End Using
End Using

This will add a new column called 'fldNew' to your database. Then you can use a standard UPDATE SQL command to add the data into your new column.

从此见与不见 2024-10-04 11:31:27

将 ADOX COM 引用添加到您的项目并尝试以下代码:

using ADOX;

//...
private void Test() {
    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<path to your .mdb>";
    CatalogClass cat = new CatalogClass();
    cat.let_ActiveConnection(connString);

    RenameField(cat, "YourTableName", "OriginalFieldName", "NewFieldName");
    AddField(cat, "YourTableName", "YourNewFieldName", DataTypeEnum.adVarWChar, 50, "");
}

// Rename a field.
private void RenameField(CatalogClass cat, string tableName, string originalFieldName, string newFieldName) {
    cat.Tables[tableName].Columns[originalFieldName].Name = newFieldName;
}

// Add a field to a specified table.
private void AddField(ADOX.CatalogClass cat, string tableName, string newFieldName, DataTypeEnum varType, int size, string defaultValue) {
    ColumnClass col = new ColumnClass();
    col.Name = newFieldName;
    col.Type = varType;
    col.DefinedSize = size;
    col.Attributes = ColumnAttributesEnum.adColNullable;
    cat.Tables[tableName].Columns.Append((object)col, DataTypeEnum.adInteger, 0);

    if (!string.IsNullOrEmpty(defaultValue)) {
        col.Properties["Default"].Value = defaultValue;
    }

}

详细信息可以在以下博客文章中找到:

使用 ADOX (C#.NET) 以编程方式重命名 MS Access 中的字段
使用 ADOX (C#.Net) 以编程方式在 MS Access 中添加新字段

使用标准 ADO.net 将数据添加到新字段。

Add an ADOX COM reference to your project and try out the following code:

using ADOX;

//...
private void Test() {
    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<path to your .mdb>";
    CatalogClass cat = new CatalogClass();
    cat.let_ActiveConnection(connString);

    RenameField(cat, "YourTableName", "OriginalFieldName", "NewFieldName");
    AddField(cat, "YourTableName", "YourNewFieldName", DataTypeEnum.adVarWChar, 50, "");
}

// Rename a field.
private void RenameField(CatalogClass cat, string tableName, string originalFieldName, string newFieldName) {
    cat.Tables[tableName].Columns[originalFieldName].Name = newFieldName;
}

// Add a field to a specified table.
private void AddField(ADOX.CatalogClass cat, string tableName, string newFieldName, DataTypeEnum varType, int size, string defaultValue) {
    ColumnClass col = new ColumnClass();
    col.Name = newFieldName;
    col.Type = varType;
    col.DefinedSize = size;
    col.Attributes = ColumnAttributesEnum.adColNullable;
    cat.Tables[tableName].Columns.Append((object)col, DataTypeEnum.adInteger, 0);

    if (!string.IsNullOrEmpty(defaultValue)) {
        col.Properties["Default"].Value = defaultValue;
    }

}

Details can be found in the following blog posts:

Rename Field in MS Access Programmatically using ADOX (C#.NET)
Add New Field in MS Access Programmatically using ADOX (C#.Net)

Add data to your new field by using standard ADO.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文