在给定路径创建sqlite数据库时,拒绝访问路径 [英] Access to path is denied while creating sqlite database at given path

查看:264
本文介绍了在给定路径创建sqlite数据库时,拒绝访问路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新的SQLite数据库文件,如果它不存在,但是每次它都抛出一个拒绝访问的异常。





我尝试了不同驱动器的路径,但是它创建了一个没有任何表的空数据库文件。

并且用户可能没有另一个驱动器,因此试图创建它仅限C盘。



有谁能告诉我们如何解决这个问题?



以下是我试过的代码。



我尝试了什么:



I am trying to create a new SQLite database file if it doesn't exist,But every time it is throwing an exception that access is denied.


I tried path of different drive,but then it creates an empty database file without any table.
and the user may not have the another drive,hence was trying to create it in C drive only.

Can anyone please tell how to solve this issue?

Below is the code that I have tried.

What I have tried:

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tthhSensorCalibrator
{
   public class DatabaseOperations
    {


       // OutputOfPort portOutput = new OutputOfPort();
        
        public void databaseMethod(string s1l, string s1h, string s2l, string s2h)
        {
            // This is the query which will create a new table in our database file with three columns. An auto increment column called "ID".
            string createTableQuery = @"CREATE TABLE IF NOT EXISTS [Log] (
                          [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                          [Date] VARCHAR(2048)  NULL,
                          [Time] VARCHAR(2048)  NULL,
                          [S1L] VARCHAR(2048) NULL,
                          [S1H] VARCHAR(2048) NULL,
                          [S2L] VARCHAR(2048) NULL,
                          [S2H] VARCHAR(2048) NULL
                          )";

            string path = "C:\\";
            if (!System.IO.File.Exists(path + "databaseFile.db3"))
            {
                SQLiteConnection.CreateFile(path + "databaseFile.db3");        // Create the file which will be hosting our database
            }
              
            else
            {
                using (System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection(@"Data source="+ path + "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

                        string Date = DateTime.Now.ToShortDateString();
                        string Time = DateTime.Now.ToShortTimeString();

                        com.CommandText = "INSERT INTO Log (Date,Time,S1L,S1H,S2L,S2H) Values (@param1,@param2,@param3,@param4,@param5,@param6)";     // Add the first entry into our database 
                        com.CommandType = System.Data.CommandType.Text;
                        com.Parameters.Add(new SQLiteParameter("@param1", Date));
                        com.Parameters.Add(new SQLiteParameter("@param2", Time));
                        com.Parameters.Add(new SQLiteParameter("@param3", s1l));
                        com.Parameters.Add(new SQLiteParameter("@param4", s1h));
                        com.Parameters.Add(new SQLiteParameter("@param5", s2l));
                        com.Parameters.Add(new SQLiteParameter("@param6", s2h));
                        com.ExecuteNonQuery();      // Execute the query



                        com.CommandText = "Select * FROM Log";      // Select all rows from our database table

                        using (System.Data.SQLite.SQLiteDataReader reader = com.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Console.WriteLine(reader["Date"] + " : " + reader["Time"]);     // Display the value
                            }
                        }
                        con.Close();        // Close the connection to the database
                    }
                }
            }
         
        }

       
    }
}

推荐答案

要添加Thaddeus Jones - 非常正确 - 说,请看这里:我应该在哪里存储我的数据? [ ^ ] - 它显示了一些更好的地方,以及如何使用它们。
To add to what Thaddeus Jones - very correctly - said, see here: Where should I store my data?[^] - it shows some better places, and how to use them.


Windows驱动器的根路径由系统拥有,并且具有受限制的访问权限。



选择运行应用程序的用户具有的其他路径写入权限。对于应用程序相关数据,这些是所有(通用)或每个用户的预定义应用程序数据文件夹(取决于数据库是否应该可供所有用户访问,或者每个用户都有自己的数据库)。 Application.UserAppDataPath Property(System.Windows) .Forms) [ ^ ]和 Application.CommonAppDataPath属性(System.Windows.Forms) [ ^ ]为您的应用程序提供相应的路径。
The root path of the Windows drive is owned by the system and has restricted access.

Select a different path where the user running your application has write access. For application related data these are the predefined application data folders for all (common) or each user (depending if the databse should be accessible by all users or each user has its own database). The Application.UserAppDataPath Property (System.Windows.Forms)[^] and the Application.CommonAppDataPath Property (System.Windows.Forms)[^] provide you with the corresponding pathes for your application.


Windows不允许您写入C:\驱动器的根目录。您可以将数据库放在%appdata%或用户的Documents文件夹中,具体取决于具体意义。
Windows doesn't allow you to write to the root of the C:\ drive. You could put your database in %appdata% or the user's Documents folder, depending on what makes sense.


这篇关于在给定路径创建sqlite数据库时,拒绝访问路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆