如何创建无互操作程序集一个的Microsoft Jet(访问)数据库? [英] How do I create a Microsoft Jet (Access) database without an interop assembly?

查看:183
本文介绍了如何创建无互操作程序集一个的Microsoft Jet(访问)数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不使用ADOX互操作程序集创建一个访问(MDB)数据库。



如何才能做到这一点?


解决方案

在我扔掉这个代码,还不如住在计算器



东西沿着这些路线,似乎这样的伎俩:



<$ p (!File.Exists(DB_FILENAME))$ p> 如果
{
VAR cnnStr =供应商= Microsoft.Jet.OLEDB.4.0;数据源=+ DB_FILENAME;

//使用后期绑定的COM对象来创建一个新的目录。这是所以我们避免互操作程序集。
变种catType = Type.GetTypeFromProgID(ADOX.Catalog);
对象o = Activator.CreateInstance(catType);
catType.InvokeMember(创建,BindingFlags.InvokeMethod,空,邻,新的对象[] {cnnStr});

OleDbConnection的CNN =新的OleDbConnection(cnnStr);
cnn.Open();
VAR CMD = cnn.CreateCommand();
cmd.CommandText =CREATE TABLE VideoPosition(文件名TEXT,POS LONG);
cmd.ExecuteNonQuery();

}

这代码说明了可以使用的OleDbConnection一次访问数据库其与ADOX.Catalog COM组件创建。


I need to create an access (mdb) database without using the ADOX interop assembly.

How can this be done?

解决方案

Before I throw away this code, it might as well live on stackoverflow

Something along these lines seems to do the trick:

if (!File.Exists(DB_FILENAME))
{
    var cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_FILENAME;

    // Use a late bound COM object to create a new catalog. This is so we avoid an interop assembly. 
    var catType = Type.GetTypeFromProgID("ADOX.Catalog");
    object o = Activator.CreateInstance(catType);
    catType.InvokeMember("Create", BindingFlags.InvokeMethod, null, o, new object[] {cnnStr});

    OleDbConnection cnn = new OleDbConnection(cnnStr);
    cnn.Open();
    var cmd = cnn.CreateCommand();
    cmd.CommandText = "CREATE TABLE VideoPosition (filename TEXT , pos LONG)";
    cmd.ExecuteNonQuery();

}

This code illustrates that you can access the database using OleDbConnection once its created with the ADOX.Catalog COM component.

这篇关于如何创建无互操作程序集一个的Microsoft Jet(访问)数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆