Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在C中创建.accdb文件时出错#_C#_Ms Access_Adox - Fatal编程技术网

C# 尝试在C中创建.accdb文件时出错#

C# 尝试在C中创建.accdb文件时出错#,c#,ms-access,adox,C#,Ms Access,Adox,这是我的代码,用于在找不到.accdb文件时创建该文件: Catalog cat = new CatalogClass(); string createStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/saveDB.accdb;"; cat.Create(createStr); Table tbl = new Table(); tbl.Name = "saveDB"; tbl.Columns.Append("

这是我的代码,用于在找不到.accdb文件时创建该文件:

Catalog cat = new CatalogClass();
string createStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/saveDB.accdb;";
cat.Create(createStr);
Table tbl = new Table();
tbl.Name = "saveDB";
tbl.Columns.Append("ID", DataTypeEnum.adGUID);
tbl.Columns.Append("Tensiune", DataTypeEnum.adDouble);
tbl.Columns.Append("Frecventa", DataTypeEnum.adDouble);
tbl.Columns.Append("Rezistenta", DataTypeEnum.adDouble);
tbl.Columns.Append("Inductanta", DataTypeEnum.adDouble);
tbl.Columns.Append("Capacitate", DataTypeEnum.adDouble);
tbl.Columns.Append("Elemente", DataTypeEnum.adVarWChar, 25);
tbl.Columns.Append("Tip", DataTypeEnum.adVarWChar, 25);
cat.Tables.Append(tbl);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(tbl);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.Tables);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);
但当我在程序中运行它时,我会得到以下运行时错误:

System.Runtime.InteropServices.COMException (0x80004005): Not a valid file name.
   at ADOX.CatalogClass.Create(String ConnectString)

我不知道文件名有什么问题,所以如果有人有任何想法,请帮助。

好吧,我终于根据Killercam在评论中发表的想法找到了一个解决方法,我会在这里发布,以防有人需要它。 按如下方式构建连接字符串:

Catalog cat = new CatalogClass();
string cntPath = System.IO.Directory.GetCurrentDirectory();
string createStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + cntPath + "\\saveDB.accdb;";
cat.Create(createStr);

路径不是我将使用的格式。尝试使用实际的完整路径srting,例如C:\SomeDir\AnotherDir\File.accdb…我考虑过这一点,但我不能这样做,因为我会把这个程序给其他人,我不知道他们会在哪里安装。我修复了它。我会在网站允许的时候(7小时)发布我找到的解决方案。