c# - 我在设置使用简单数据库实现的网站时遇到问题

标签 c# asp.net database sqlite

这是我的第一个问题,请耐心等待。 :) 所以,我花了几个小时试图解决这个问题,但还没有找到解决方案。 问题很简单:

我正在使用 Visual Studio 2013 Ultimate 90 天试用版和 ASP.Net Framework 4.5 和 C# 创建一个简单的网站,用户可以在其中创建一个帐户,这意味着他们的帐户数据需要保存到一个数据库。

我使用的数据库工具是 SQLite,因为我读到这是一个用于小型数据库的好工具。

因此,在我实际输入测试用户的信息并单击我的“创建帐户!”之前,我的应用程序运行良好。按钮。此时我的应用程序出现以下错误:

-------------------------------------------- ------------------------------

Access to the path 'C:\Program Files (x86)\IIS Express\databaseFile.db3' is denied.

描述:当前网络请求执行过程中出现未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\IIS Express\databaseFile.db3' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

来源错误:

Line 28: )"; Line 29: Line 30:
System.Data.SQLite.SQLiteConnection.CreateFile("databaseFile.db3");
// Create the file which will be hosting our database Line 31:
using (System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection("data source=databaseFile.db3")) Line 32: {

Source File: c:\Users\Alex\Documents\Visual Studio 2013\Projects\WebDataBase\WebDataBase\SignUpForm.aspx.cs Line: 30

Stack Trace:

[UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\IIS Express\databaseFile.db3' is denied.]
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 options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1305 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) +63
System.Data.SQLite.SQLiteConnection.CreateFile(String databaseFileName) +38 WebDataBase.SignUpForm.AddNewUser(String pw) in c:\Users\Alex\Documents\Visual Studio 2013\Projects\WebDataBase\WebDataBase\SignUpForm.aspx.cs:30
WebDataBase.SignUpForm.Button_CREATE_ACCOUNT_Click1(Object sender, EventArgs e) in c:\Users\Alex\Documents\Visual Studio 2013\Projects\WebDataBase\WebDataBase\SignUpForm.aspx.cs:71
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9653178
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean 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.";
            }
        }
    }
}

最佳答案

您需要授予您对 C:\Program Files (x86)\IIS Express\ 文件夹本身的应用程序权限(我可能不推荐),或者使用实际位于项目文件夹(在你的例子中看起来像是 c:\Users\Alex\Documents\Visual Studio 2013\Projects\WebDataBase\WebDataBase),这是我的建议。

关于c# - 我在设置使用简单数据库实现的网站时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28995630/

相关文章:

C# tweetinvi 代码 408

c# - 删除内联静态事件

asp.net - EADDRINUSE 地址已被使用

javascript - 如何从 ASP :NET? 将数组传递给 jQuery

database - 通过标准化 (3NF) 进行工作

mysql - 如何从 MySQL 中的查询中获取所有数据?

c# - 在 MVC4 中使用 HtmlHelpers

javascript - 将 cliendid 从控件作为参数从 ASP 传递到 javascript 函数

arrays - 将数组与 Cypher (Neo4j) 结合起来

c# - 是什么导致 Calibri 在 9 到 14 pt 之间丢失 ClearType?