使用服务器路径在vb.net中登录 [英] login in vb.net using server path
问题描述
我想使用服务器路径登录。但是我无法做到。
i want to login using server path. But i am not to able to.
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
' Dim info As DirectoryInfo = New DirectoryInfo("App_Data\CM.mdb")
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = HttpContext.Current.Server.MapPath("/App_Data/CM.mdb")
con.ConnectionString = dbProvider & dbSource
con.Open()
MsgBox("Database is now open")
con.Close()
MsgBox("Database is now Closed")
i收到错误消息,表示已处理Null Reference Exception。
Plz帮助..
问候,
Megha
i am getting an error saying Null Reference Exception was handled.
Plz help..
Regards,
Megha
推荐答案
第一个问题:您的连接字符串无效。您在提供程序和数据库路径之间缺少Data Source =
。
http://www.connectionstrings.com/access/ [ ^ ]
第二个问题:您的数据库路径将仅当您的应用程序位于站点的根目录时才起作用。改为使用app相对路径:
First problem: Your connection string is invalid. You're missing aData Source=
between the provider and the database path.
http://www.connectionstrings.com/access/[^]
Second problem: Your database path will only work if your application is at the root of a site. Use an app-relative path instead:
dbSource = HttpContext.Current.Server.MapPath("~/App_Data/CM.mdb")
第三个问题:在ASP.NET应用程序中调用 MsgBox
将无法正常工作。如果你很幸运,你会得到一个异常,表明当前进程不是交互式的。如果没有,你会弹出一个消息框在服务器上,没有人会看到它,你的代码将等待有人点击确定。
第四个问题:你实际上没有显示或描述你的代码是如何被调用的,或是异常被抛出的那一行,但我猜你是尝试在 HttpContext.Current
不可用时调用代码。尝试将 MapPath
行更改为:
Third problem: Calling MsgBox
in an ASP.NET application will not work. If you're lucky, you'll get an exception indicating that the current process isn't interactive. If not, you'll pop up a message box on the server, where nobody will ever see it, and your code will hang waiting for someone to hit "OK".
Fourth problem: You haven't actually shown or described how your code is called, or what line the exception is thrown from, but I'm guessing that you're trying to call the code at a point when HttpContext.Current
is not available. Try changing the MapPath
line to:
dbSource = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/CM.mdb")
编辑:根据评论,此代码适用于 Windows 应用程序,而不是ASP.NET应用程序。
Windows应用程序没有当前的 HttpContext
。 HttpContext.Current
将始终返回 Nothing
。当您尝试访问 HttpContext.Current.Server
时,它将始终抛出 NullReferenceException
。
此外,Windows应用程序没有 App_Data
文件夹。
您需要为访问数据库指定正确的文件系统路径。假设数据库文件与应用程序位于同一文件夹中,您可以使用 | DataDirectory |
作为快捷方式:
As per the comment, this code is for a Windows application, not an ASP.NET application.
Windows applications do not have a current HttpContext
. HttpContext.Current
will always return Nothing
. When you try to access HttpContext.Current.Server
, it will always throw a NullReferenceException
.
Furthermore, Windows applications don't have an App_Data
folder.
You will need to specify the correct file-system path to your access database. Assuming the database file is in the same folder as your application, you can use |DataDirectory|
as a shortcut:
Dim con As New OleDb.OleDbConnection
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\CM.mdb;Persist Security Info=False;"
con.ConnectionString = connectionString
con.Open()
MsgBox("Database is now open")
con.Close()
MsgBox("Database is now Closed")
注意:运行应用程序的用户将会要求对包含数据库文件的目录进行写访问。
NB: The user running your application will require write access to the directory containing the database file.
这篇关于使用服务器路径在vb.net中登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!