Server.MapPath in ASP.Net
-
15-03-2021 - |
문제
I have developed a Web Application using ASP.Net
with Microsoft Access
as database. Now i have connected to the database using OleDB connection. The problem is when i give the database source in Connection String Source="C:/WebApp/DB/data.mdb"
. The connection throws an error stating the path is invalid and there is no such file.
I tried using
Server.MapPath("~/DB/data.mdb")
which gives the path as "C:/WebApp/DB/data.mdb" but throws the same error
Colud not locate file 'C:/WebApp/DB/data.mdb'
What should be the problem, even though if it works good in loalhost when i publish it to the server using Plesk, it shows the same error stating "C:/inetpub/xyx.com/httpdocs/..." not found
해결책
Try using the wndows path (backslash rather than forward slash).
If you are shipping an access database with a web application, you can also investigate putting it in the App_Data folder. Read here for more info.
A fully worked example is here
Note that in your current example, if DB is a folder under the web root, anyone could potentially download your database if they know the file name. By default, items stored in App_Data will NOT be served to clients.
Also note that, usefully, when you store the item in App_Data, your connection string becomes:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\data.mdb;Persist Security Info=False;"
(plus any user/password details). ASP.Net will resolve the |DataDirectory| path at runtime.
다른 팁
did you try with the windows path? (note backslash)
C:\WebApp\DB\data.mdb
There might have several problems, would you please check below items:
- Check the
DB
folder is exists at root level of your application - Make sure the database file
data.mdb
is exist uderDB
folder
Take appropriate action based on any option is missing as listed above.