2012-04-19 39 views
0

這是我的代碼創建一個.ACCDB文件,如果它不能找到一個:錯誤,當我試圖創建在C#中.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) 

我想不出有什麼用文件名的問題,因此,如果任何人有任何想法,請幫助。

+1

的路徑是我會使用的格式不。試試它與一個實際的FullPath srting即C:\ SomeDir \ AnotherDir \ File.accdb ... – MoonKnight 2012-04-19 15:43:21

+0

我想了解,但我不能這樣做,因爲我會把這個程序給其他人,我不知道在哪裏他們會安裝它。 – 2012-04-19 15:48:54

+0

我修好了。我會發布解決方案時,我發現當網站允許我(7小時)。 – 2012-04-19 16:13:39

回答

0

嗯,我終於找到了一個基於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); 
相關問題