使用c#代码创建MS Access数据库时出现异常 [英] Exception while creating MS Access database using c# code

查看:74
本文介绍了使用c#代码创建MS Access数据库时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我理解InvokeMember方法引发的错误:



System.Reflection.TargetInvocationException - 调用目标抛出了异常。$ / $
InnerException - 多步OLE DB操作生成错误。检查每个OLE DB状态值(如果可用)。没有工作。 {System.Runtime.InteropServices.COMException}

Can any one help me in understanding the error raised on the InvokeMember method:

System.Reflection.TargetInvocationException - Exception has been thrown by the target of an invocation.
InnerException - Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done. {System.Runtime.InteropServices.COMException}

//var conStr = GetConnectionString(strFileOutput, String.Empty);
var conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Access 12.0;";
conStr += "Data Source=C:\\test.accdb;Persist Security Info=False;";

var catType = Type.GetTypeFromProgID("ADOX.Catalog");

object obj = Activator.CreateInstance(catType);

catType.InvokeMember("Create", BindingFlags.InvokeMethod, null, obj, new object[] { conStr });

OleDbConnection cnn = new OleDbConnection(conStr);

cnn.Open();

推荐答案

试试这个:



Try this:

using System;
 using ADOX;

 namespace ConsoleApplication1
 {
     class Class1
     {
         [STAThread]
         static void Main(string[] args)
         {
             ADOX.Catalog cat = new ADOX.Catalog();

             cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\test.accdb;");

             Console.WriteLine("Database Created Successfully");

             cat = null;

         }
     }
 }





我会避免使用Activator并进行设置如果你不需要反思。我认为你得到的错误是因为它试图创建一个连接,该连接具有在不存在的数据库上设置的许多属性。您需要简化连接字符串,然后当您想要打开它时,您可以使用其余的属性。



I would avoid the Activator and setting through reflection if you don't need to. I think the error you are getting is because it's trying to create a connection with a lot of properties set on a database that doesn't exist. You need to simplify your connection string, then when you want to open it you can use the rest of the properties.


这篇关于使用c#代码创建MS Access数据库时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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