這幾天回到家都在寫鬧鐘程式,
雖然它看起來似乎是沒什麼難度,可是有些點也著實費了我不少功夫。
當我使用OpenFileDialog這個元件的時候,
每當我瀏覽完檔案要進行資料庫的存取,就會出現錯誤,
訊息內容就是"找不到路徑",
再去看ConnectionString,路徑明明設的好好的阿!
可是只要執行OpenFileDialog,DB的路徑就會變成OpenFileDialog所瀏覽檔案的路徑。
這個問題困擾我很久,而英文不好的我google中文網站卻沒有什麼收穫,
最終,我在外國的論壇網站找到了類似的問題。
http://forums.devx.com/showthread.php?t=160347
Open File Dialog and Database Location Problem
I have come across a problem which I'm unable to solve.
I'm creating a VB.Net application using Visual Studio 2005. On one of my forms, I've save a new record/row to my database. On this form, it also has a Open File Dialog component, which opens a picture, so that I can save the picture to the database. The problem occurs whenever I use the Open File Dialog, and open a picture, it comes up with the error saying it can't find my database. It shows me the path is it looking for it, and its the wrong path.
But when I don't use the Open File Dialog, it works perfectly fine. It saves the new record/row to the database. Its whenever I open a file using the Open File Dialog that it doesn't work.
If I hit the cancel button on the Open File Dialog it will still work, its whenever I open a file that it comes up with the error of not finding my database, as it is looking in the wrong directory.
I tried this in a new project and the same thing happens. It seems that whenever I open a file using the Open File Dialog, it is changing the path of where my database is, to the path of where the file I've just opened.
I have no idea why this is happening, please help me.
Here is my code for my database connection:
'Connection to the Access database
Public Const strConnection As String = _
Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=./myDatabase.mdb;Persist Security Info=False"
and here is my code for my Open File Dialog:
OpenFileDialog.Filter = "Image files (.jpg, .jpeg, .bmp, .gif)|*.jpg;*.jpeg;*.bmp;*.gif"
If (OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
imageFileName = OpenFileDialog.FileName
txtImage.Text = imageFileName
End If
Thanks very much, any help much appreciated.
然後下面有好心人回覆耶!好開心唷!
意思就是說有兩種方法可以試試看,
(1)設定OpenFileDialog的RestoreDirectory屬性為true
(2)將ConnectionString的DataSource設為絕對路徑
我採用了第一種方法,馬上就成功了,感動捏!
留言列表