如何使用.NET从Access数据库中处理表 [英] How to manipulate a table from an Access Database with .NET
本文介绍了如何使用.NET从Access数据库中处理表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Access数据库中有一个名为"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);
我怎么到餐桌旁?
推荐答案
向您的项目添加ADOX COM引用,然后尝试以下代码:
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:
重命名字段使用ADOX(C#.NET)以编程方式访问MS
使用ADOX(C#.Net)以编程方式在MS Access中添加新字段
使用标准ADO.net将数据添加到新字段中.
Add data to your new field by using standard ADO.net.
这篇关于如何使用.NET从Access数据库中处理表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文