Skip to content

Commit ae56cec

Browse files
committed
File Import
1 parent 4b75a6a commit ae56cec

5 files changed

Lines changed: 405 additions & 0 deletions

File tree

Properties/AssemblyInfo.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SQLiteWrapper")]
9+
[assembly: AssemblyDescription("Wrapper for SQLite database")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Digitalroot.net")]
12+
[assembly: AssemblyProduct("SQLiteWrapper")]
13+
[assembly: AssemblyCopyright("Copyright © Digitalroot.net 2008")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("70d01176-8393-4058-a74b-1b947a1e776f")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Revision and Build Numbers
33+
// by using the '*' as shown below:
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

SQLiteWrapper.cs

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/**
2+
* SQLite Wrapper
3+
*
4+
* Author: Nicholas Dunnaway
5+
* Copyright: digitalroot.net 2008 - 2013
6+
* Web: http://digitalroot.net
7+
*
8+
* This program was written to make reading and writing to a SQLite database easy.
9+
*
10+
* Requirements:
11+
* .Net Runtime Files
12+
* System.Data.SQLite.dll
13+
*
14+
*/
15+
using System;
16+
using System.IO;
17+
using System.Data;
18+
using System.Data.SQLite;
19+
20+
namespace SQLiteWrapper
21+
{
22+
/// <summary>
23+
/// This class makes reading and writing to a SQLite database easy.
24+
/// </summary>
25+
public class SQLiteWrapper
26+
{
27+
/// <summary>
28+
/// Path used to locate the database file.
29+
/// </summary>
30+
private string _pathToDatabase;
31+
32+
/// <summary>
33+
/// Name of the database file.
34+
/// </summary>
35+
private readonly string _databaseFileName;
36+
37+
/// <summary>
38+
/// This is the connection to the database. This holds it open
39+
/// so we do not have to reconnect each time we want to run a query.
40+
/// </summary>
41+
private static SQLiteConnection _cnn;
42+
43+
/// <summary>
44+
/// This is what we use to issue commands to the database.
45+
/// </summary>
46+
private static SQLiteCommand _cmd;
47+
48+
/// <summary>
49+
/// Public method for getting and settings the Path to the database file.
50+
/// </summary>
51+
public string PathToDatabase
52+
{
53+
get { return _pathToDatabase; }
54+
set
55+
{
56+
if (Directory.Exists(value))
57+
{
58+
char[] charsToTrim = { '\\', '/' }; // Remove trailing slash.
59+
_pathToDatabase = value.TrimEnd(charsToTrim);
60+
}
61+
else
62+
{
63+
throw new Exception("Path for database is invalid.");
64+
}
65+
}
66+
}
67+
68+
/// <summary>
69+
/// This creates the database for the first time. It creates a by default a settings table.
70+
/// </summary>
71+
private void CreateSettingsTable()
72+
{
73+
using (_cnn = new SQLiteConnection("Data Source=" + _pathToDatabase + '\\' + _databaseFileName))
74+
{
75+
using (_cmd = _cnn.CreateCommand())
76+
{
77+
_cnn.Open(); // Open the database connection.
78+
79+
// Create a new table
80+
_cmd.CommandText = "CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(150) UNIQUE, value VARCHAR(250))";
81+
_cmd.ExecuteNonQuery(); // Create the table, don't expect returned data
82+
83+
// Insert something into the table
84+
_cmd.CommandText = "INSERT INTO settings (name, value) VALUES ('dbInstalled', 'TRUE')";
85+
_cmd.ExecuteNonQuery();
86+
87+
// Read the values back out
88+
_cmd.CommandText = "SELECT value FROM settings WHERE name = 'dbInstalled'";
89+
using (SQLiteDataReader reader = _cmd.ExecuteReader())
90+
{
91+
while (reader.Read())
92+
{
93+
//Console.WriteLine(String.Format("id = {0}, ServerName = {1}", reader[0], reader[1]));
94+
if (reader[0].ToString() != "TRUE")
95+
{
96+
// Database was not setup correctly. We should prob delete the database file.
97+
File.Delete(_pathToDatabase + '\\' + _databaseFileName);
98+
throw new Exception("Error creating database");
99+
}
100+
}
101+
}
102+
}
103+
}
104+
}
105+
106+
/// <summary>
107+
/// This queries the database and returns the values of a setting.
108+
/// </summary>
109+
/// <param name="name">Name of the settings you would like the value for.</param>
110+
public string Get(string name)
111+
{
112+
using (_cmd = _cnn.CreateCommand())
113+
{
114+
// check if the connection is already open. If not Open it.
115+
if (_cnn.State != ConnectionState.Open)
116+
{
117+
_cnn.Open();
118+
}
119+
120+
// Read the values back out
121+
_cmd.CommandText = "SELECT value FROM settings WHERE name = '" + name + "'";
122+
using (SQLiteDataReader reader = _cmd.ExecuteReader())
123+
{
124+
while (reader.Read())
125+
{
126+
return reader[0].ToString();
127+
}
128+
}
129+
}
130+
return "";
131+
}
132+
133+
/// <summary>
134+
/// This is for passing raw commands to the database.
135+
/// </summary>
136+
/// <param name="sqlCommand">SQL Query</param>
137+
/// <returns>True or throws an Exception</returns>
138+
public bool RawCmd(string sqlCommand)
139+
{
140+
try
141+
{
142+
using (_cmd = _cnn.CreateCommand())
143+
{
144+
// check if the connection is already open. If not Open it.
145+
if (_cnn.State != ConnectionState.Open)
146+
{
147+
_cnn.Open();
148+
}
149+
150+
// Run command from user
151+
_cmd.CommandText = sqlCommand;
152+
_cmd.ExecuteNonQuery();
153+
}
154+
}
155+
catch (Exception e)
156+
{
157+
throw new Exception(e.Message);
158+
}
159+
return true;
160+
}
161+
162+
/// <summary>
163+
/// This is for passing raw select statements to the database.
164+
/// </summary>
165+
/// <param name="sqlCommand">SQL Query</param>
166+
/// <returns>True or throws an Exception</returns>
167+
public DataTable Select(string sqlCommand)
168+
{
169+
using (_cmd = _cnn.CreateCommand())
170+
{
171+
// check if the connection is already open. If not Open it.
172+
if (_cnn.State != ConnectionState.Open)
173+
{
174+
_cnn.Open();
175+
}
176+
177+
// Read the values back out
178+
var myTable = new DataTable();
179+
180+
using (var myDataAdp = new SQLiteDataAdapter(sqlCommand, _cnn))
181+
{
182+
using (new SQLiteCommandBuilder(myDataAdp))
183+
{
184+
myDataAdp.Fill(myTable);
185+
}
186+
}
187+
return myTable;
188+
}
189+
}
190+
191+
/// <summary>
192+
/// Save the value of something to the database so we can get it back later.
193+
/// </summary>
194+
/// <param name="name">Name of the settings you would like to set.</param>
195+
/// <param name="value">Value you of settings we are saving.</param>
196+
/// <returns>Returns True if the value was saved to the database.</returns>
197+
public bool Set(string name, string value)
198+
{
199+
try
200+
{
201+
using (_cmd = _cnn.CreateCommand())
202+
{
203+
// check if the connection is already open. If not Open it.
204+
if (_cnn.State != ConnectionState.Open)
205+
{
206+
_cnn.Open();
207+
}
208+
209+
// Insert something into the table
210+
_cmd.CommandText = "DELETE FROM settings WHERE name = '" + name + "'";
211+
_cmd.ExecuteNonQuery();
212+
213+
// Insert something into the table
214+
_cmd.CommandText = "INSERT INTO settings (name, value) VALUES ('" + name + "', '" + value + "')";
215+
_cmd.ExecuteNonQuery();
216+
}
217+
}
218+
catch (Exception e)
219+
{
220+
throw new Exception(e.Message);
221+
}
222+
return true;
223+
}
224+
225+
/// <summary>
226+
/// Constructor
227+
/// </summary>
228+
/// <param name="databaseName">Database File Name</param>
229+
/// <param name="pathToDatabase">Path to the database file.</param>
230+
public SQLiteWrapper(string databaseName, string pathToDatabase)
231+
{
232+
_pathToDatabase = pathToDatabase; // Set the path
233+
_databaseFileName = databaseName; // Set File Name
234+
if (File.Exists(_pathToDatabase + '\\' + _databaseFileName) == false)
235+
{
236+
CreateSettingsTable();
237+
}
238+
else
239+
{
240+
using (_cnn = new SQLiteConnection("Data Source=" + _pathToDatabase + '\\' + _databaseFileName))
241+
{
242+
using (_cmd = _cnn.CreateCommand())
243+
{
244+
_cnn.Open(); // Open the database connection.
245+
}
246+
}
247+
}
248+
}
249+
250+
/// <summary>
251+
/// Constructor
252+
/// </summary>
253+
/// <param name="databaseName">Database File Name</param>
254+
public SQLiteWrapper(string databaseName)
255+
: this(databaseName, ".")
256+
{ }
257+
}
258+
}

SQLiteWrapper.csproj

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.50727</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{DED7A7A9-8C36-4B4C-AF89-FEBB682A4DC4}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>SQLiteWrapper</RootNamespace>
12+
<AssemblyName>SQLiteWrapper</AssemblyName>
13+
<FileUpgradeFlags>
14+
</FileUpgradeFlags>
15+
<UpgradeBackupLocation>
16+
</UpgradeBackupLocation>
17+
<OldToolsVersion>3.5</OldToolsVersion>
18+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
19+
<TargetFrameworkProfile />
20+
<PublishUrl>publish\</PublishUrl>
21+
<Install>true</Install>
22+
<InstallFrom>Disk</InstallFrom>
23+
<UpdateEnabled>false</UpdateEnabled>
24+
<UpdateMode>Foreground</UpdateMode>
25+
<UpdateInterval>7</UpdateInterval>
26+
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
27+
<UpdatePeriodically>false</UpdatePeriodically>
28+
<UpdateRequired>false</UpdateRequired>
29+
<MapFileExtensions>true</MapFileExtensions>
30+
<ApplicationRevision>0</ApplicationRevision>
31+
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
32+
<IsWebBootstrapper>false</IsWebBootstrapper>
33+
<UseApplicationTrust>false</UseApplicationTrust>
34+
<BootstrapperEnabled>true</BootstrapperEnabled>
35+
</PropertyGroup>
36+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
37+
<DebugSymbols>true</DebugSymbols>
38+
<DebugType>full</DebugType>
39+
<Optimize>false</Optimize>
40+
<OutputPath>bin\Debug\</OutputPath>
41+
<DefineConstants>DEBUG;TRACE</DefineConstants>
42+
<ErrorReport>prompt</ErrorReport>
43+
<WarningLevel>4</WarningLevel>
44+
<Prefer32Bit>false</Prefer32Bit>
45+
</PropertyGroup>
46+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
47+
<DebugType>pdbonly</DebugType>
48+
<Optimize>true</Optimize>
49+
<OutputPath>bin\Release\</OutputPath>
50+
<DefineConstants>TRACE</DefineConstants>
51+
<ErrorReport>prompt</ErrorReport>
52+
<WarningLevel>4</WarningLevel>
53+
<Prefer32Bit>false</Prefer32Bit>
54+
</PropertyGroup>
55+
<ItemGroup>
56+
<Reference Include="System" />
57+
<Reference Include="System.Data" />
58+
<Reference Include="System.Data.SQLite, Version=1.0.43.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86" />
59+
<Reference Include="System.Xml" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<Compile Include="SQLiteWrapper.cs" />
63+
<Compile Include="Properties\AssemblyInfo.cs" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<Content Include="libs\System.Data.SQLite.dll">
67+
</Content>
68+
</ItemGroup>
69+
<ItemGroup>
70+
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
71+
<Visible>False</Visible>
72+
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
73+
<Install>false</Install>
74+
</BootstrapperPackage>
75+
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
76+
<Visible>False</Visible>
77+
<ProductName>.NET Framework 3.5 SP1</ProductName>
78+
<Install>true</Install>
79+
</BootstrapperPackage>
80+
</ItemGroup>
81+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
82+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
83+
Other similar extension points exist, see Microsoft.Common.targets.
84+
<Target Name="BeforeBuild">
85+
</Target>
86+
<Target Name="AfterBuild">
87+
</Target>
88+
-->
89+
</Project>

0 commit comments

Comments
 (0)