【发布时间】:2015-03-11 19:27:59
【问题描述】:
这是我在这里的第一个问题,所以请耐心等待。 :) 所以,我花了几个小时试图解决这个问题,但还没有找到解决方案。 问题很简单:
我正在使用 Visual Studio 2013 Ultimate 90 天试用版和 ASP.Net Framework 4.5 和 C# 创建一个简单的网站,用户可以在其中创建帐户,这意味着他们的帐户数据需要保存到一个数据库。
我使用的数据库工具是 SQLite,因为我读到这是一个用于小型数据库的好工具。
所以,我的应用程序运行良好,直到我真正输入测试用户的信息并点击我的“创建帐户!”按钮。此时我的应用程序给了我以下错误:
----------------------------------------------- -----------------------------------------
访问路径'C:\Program Files (x86)\IIS Express\databaseFile.db3' 被拒绝。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.UnauthorizedAccessException:访问 路径 'C:\Program Files (x86)\IIS Express\databaseFile.db3' 被拒绝。
ASP.NET 无权访问请求的资源。考虑 向 ASP.NET 请求授予对资源的访问权限 身份。 ASP.NET 有一个基本进程标识(通常 IIS 5 上的 {MACHINE}\ASPNET 或 IIS 6 和 IIS 7 上的网络服务,以及 IIS 7.5 上配置的应用程序池标识),如果 该应用程序不是模拟的。如果应用程序是 冒充 via ,身份将是 匿名用户(通常是 IUSR_MACHINENAME)或经过身份验证的用户 请求用户。
要授予 ASP.NET 对文件的访问权限,请右键单击文件中的文件 资源管理器,选择“属性”并选择“安全”选项卡。点击“添加” 添加适当的用户或组。突出显示 ASP.NET 帐户, 并选中所需访问权限的复选框。
来源错误:
第 28 行:)";第 29 行:第 30 行:
System.Data.SQLite.SQLiteConnection.CreateFile("databaseFile.db3");
// 创建将托管我们的数据库的文件 第 31 行:
使用(System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection("数据源=databaseFile.db3")) 第 32 行:{源文件:c:\Users\Alex\Documents\Visual Studio 2013\Projects\WebDataBase\WebDataBase\SignUpForm.aspx.cs 行:30
堆栈跟踪:
[UnauthorizedAccessException: 访问路径 'C:\Program Files (x86)\IIS Express\databaseFile.db3' 被拒绝。]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +217 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,FileOptions 选项,SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1305 System.IO.FileStream..ctor(字符串路径,文件模式 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize) +63
System.Data.SQLite.SQLiteConnection.CreateFile(字符串 数据库文件名)+38 WebDataBase.SignUpForm.AddNewUser(字符串密码) 在 c:\Users\Alex\Documents\Visual Studio 2013\Projects\WebDataBase\WebDataBase\SignUpForm.aspx.cs:30
WebDataBase.SignUpForm.Button_CREATE_ACCOUNT_Click1(对象发送者, c:\Users\Alex\Documents\Visual Studio 中的 EventArgs e) 2013\Projects\WebDataBase\WebDataBase\SignUpForm.aspx.cs:71
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9653178
System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 事件参数)+103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) +1724
----------------------------------------------- -----------------------------------------
好的,所以,我读到要解决这个问题,我所要做的只是授予对数据库文件的访问权限,但我不能这样做,因为数据库文件还不存在。
谁能帮帮我?
谢谢! :D
哦,如果你需要,这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SQLite;
namespace WebDataBase
{
public partial class SignUpForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AddNewUser(string pw)
{
string email = TextBox_EMAIL.Text;
string username = TextBox_USERNAME.Text;
string createTableQuery = @"CREATE TABLE IF NOT EXISTS [UserData] (
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Email] NVARCHAR(30) NULL,
[Username] NVARCHAR(12) NULL,
[Password] NVARCHAR(12) NULL
)";
System.Data.SQLite.SQLiteConnection.CreateFile("databaseFile.db3"); // Create the file which will be hosting our database
using (System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection("data source=databaseFile.db3"))
{
using (System.Data.SQLite.SQLiteCommand com = new System.Data.SQLite.SQLiteCommand(con))
{
con.Open(); // Open the connection to the database
com.CommandText = createTableQuery; // Set CommandText to our query that will create the table
com.ExecuteNonQuery(); // Execute the query
com.CommandText = "INSERT INTO UserData (ID,Email,Username,Password) Values ('" + email + "','" + username + "','" + pw + "')"; // Add the first entry into our database
com.ExecuteNonQuery(); // Execute the query
//com.CommandText = "INSERT INTO UserData (ID,Email,Username,Password) Values ('key two','value value')"; // Add another entry into our database
// com.ExecuteNonQuery(); // Execute the query
com.CommandText = "Select * FROM UserData"; // Select all rows from our database table
using (System.Data.SQLite.SQLiteDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
string row = (reader["ID"] + " : " + reader["Email"] + " : " +
reader["Username"] + " : " + reader["Password"]); // Display the value of the key and value column for every row
Label1.Text = row;
}
}
con.Close(); // Close the connection to the database
}
}
}
protected void Button_CREATE_ACCOUNT_Click1(object sender, EventArgs e)
{
Label1.Text = "";
string pw = TextBox_PASSWORD.Text;
string confirmedPw = TextBox_CONFIRM_PASSWORD.Text;
// Check if "password" and "confirm password" values are the same
if (pw.Equals(confirmedPw, StringComparison.Ordinal))
{
AddNewUser(pw);
}
else
{
Label1.Text = "Passwords are not matching. Please make sure they are matching.";
}
}
}
}
【问题讨论】:
-
您是否以管理员身份运行 Visual Studio?
标签: c# asp.net database sqlite