简体   繁体   中英

Error when I try to create an .accdb file in C#

This is my code that creates an .accdb file if it can't find one :

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);

But when I run it in the program i get this runtime error :

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

I can't figure what's the problem with the file name, so if anyone has any idea, please help.

Well, i finally found a workaround based on Killercam's idea he posted in the comments and i'll post it here in case someone needs it sometime. Build the connection string like this :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM