[VB.NET]怎么用ADO.NET创建ACCESS数据?

VB.NET源码-156个实用实例哦…… <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
怎么用ADO.NET创建ACCESS数据?
用DAO方式的话,里面有一个WorkSpace.CreateDataBase函数可以创建数据库,但在ADO.NET中却没找到,郁闷啊......
__________________________________________________________________________
关注
__________________________________________________________________________
如果实在不行 就放一个空数据库在程序目录里作为模板,需要建库时把这个模板文件复制出一份来取个新名字就OK了。
__________________________________________________________________________
UP!等高人啊.....

TO: billow_chentao(巨浪)
我想在程序中实现创建ACCESS数据库的功能,建个空的数据库复制过去的话感觉不爽.
__________________________________________________________________________
5 楼acptvb(微软全球技术中心 VB技术支持)回复于 2002-05-15 17:02:34 得分 15感谢您使用微软产品。

在.NET中,您依然可以通过COM Interop使用ADOX来创建Access数据库。其基本的步骤如下:
1. 新建一个C# Console应用程序。
2.在Solution Explorer中,右击References节点,选择Add Reference。
3.在Add Reference对话框中,点选COM选项卡,选择Microsoft ADO
Ext. 2.7 for DDL and Security。点击Select按钮将其加入Selected Components框。然后点OK。
4.键入类似以下代码:
using System;
using ADOX;

namespace CSAccessADOX1
{
class Class1
{
static void Main(string[] args)
{
Catalog cat = new Catalog();
cat.Create(@ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NewMDB.mdb;Jet OLEDB:Engine Type=5 ");
Table objTable = new Table();
objTable.Name = "Test_Table ";
objTable.Columns.Append( "Test_Field ", DataTypeEnum.adWChar,10);
cat.Tables.Append(objTable);
objTable = null;
cat = null;
}
}
}

具体的关于COM Interop的信息,您可以参考以下文档:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconexposingcomcomponentstonetframework.asp

具体的关于ADOX的信息,您可以参考以下文档:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/admscadoxfundamentals.asp?frame=true
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/admscadoapireference.asp?frame=true

- 微软全球技术中心 VB技术支持

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。

为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。



------------
我去baidu了一下。有不少。
__________________________________________________________________________
用ADO.NET创建access数据库
用这个标题并不合适。因为ADO.NET是不能创建Access数据库的-_-!! 如果是Sql Server就会好办得多。

在这里只是给出了一种妥协的方法,能够在使用ADO.NET的环境下连接COM达到目的。

准备工作:

1. 在Solution Explorer中,右击References节点,选择Add Reference。
2. 在Add Reference对话框中,点选COM选项卡,选择Microsoft ADO Ext. 2.7 for DDL and Security。点击Select按钮将其加入Selected Components框。然后点OK。

以下是code:

///
/// Create database to current path where specified by dbName
///

///
///
public static OleDbConnection CreateDB(string dbName)
{
if(string.Empty == dbName)
{
throw new Exception();
}
if(!File.Exists(dbName))
{
Catalog cat = new Catalog();

//check whether the directory exist, otherwise create the directory
int path_end = dbName.LastIndexOf( "// ");
string dirPath = dbName.Substring(0, path_end);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}

cat.Create(@ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbName + ";Jet OLEDB:Engine Type=5 ");
Table objTable = new Table();
objTable.Name = "Test_Table ";
objTable.Columns.Append( "Test_Field ", DataTypeEnum.adWChar, 10);
cat.Tables.Append(objTable);
objTable = null;
cat = null;
}
OleDbConnection conn = new OleDbConnection(@ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbName);
return conn;
}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=932118
__________________________________________________________________________
To xiaoku(野蛮人(^v^)) :我就是不知道ADO.NET能不能创建ACCESS数据库才问的问题啊,知道的话就不会问这个问题了.....
__________________________________________________________________________
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值