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
{
public void databaseMethod(string s1l, string s1h, string s2l, string s2h)
{
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");
}
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();
com.CommandText = createTableQuery;
com.ExecuteNonQuery();
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)";
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();
com.CommandText = "Select * FROM Log";
using (System.Data.SQLite.SQLiteDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["Date"] + " : " + reader["Time"]);
}
}
con.Close();
}
}
}
}
}
}