mirror of
https://github.com/ckaczor/FeedCenter.git
synced 2026-02-16 18:47:28 -05:00
Update to EF6
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
using System;
|
||||
using Common.Debug;
|
||||
using FeedCenter.Properties;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlServerCe;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Common.Debug;
|
||||
|
||||
using FeedCenter.Properties;
|
||||
|
||||
namespace FeedCenter.Data
|
||||
{
|
||||
public static class Database
|
||||
@@ -45,10 +43,10 @@ namespace FeedCenter.Data
|
||||
try
|
||||
{
|
||||
// Open the database file
|
||||
using (FileStream stream = new FileStream(databasePath, FileMode.Open, FileAccess.Read))
|
||||
using (var stream = new FileStream(databasePath, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
// Read the file using the binary reader
|
||||
BinaryReader reader = new BinaryReader(stream);
|
||||
var reader = new BinaryReader(stream);
|
||||
|
||||
// Seek to the version signature
|
||||
stream.Seek(16, SeekOrigin.Begin);
|
||||
@@ -81,7 +79,7 @@ namespace FeedCenter.Data
|
||||
Tracer.WriteLine("Creating database engine");
|
||||
|
||||
// Create the database engine
|
||||
using (SqlCeEngine engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath)))
|
||||
using (var engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath)))
|
||||
{
|
||||
Tracer.WriteLine("Creating database");
|
||||
|
||||
@@ -95,20 +93,20 @@ namespace FeedCenter.Data
|
||||
}
|
||||
}
|
||||
|
||||
private static int getVersion(SqlCeConnection connection)
|
||||
private static int GetVersion(SqlCeConnection connection)
|
||||
{
|
||||
string versionString = string.Empty;
|
||||
string versionString;
|
||||
|
||||
try
|
||||
{
|
||||
// Check the database version table
|
||||
using (SqlCeCommand command = new SqlCeCommand("SELECT Value FROM DatabaseVersion", connection))
|
||||
using (var command = new SqlCeCommand("SELECT Value FROM DatabaseVersion", connection))
|
||||
versionString = command.ExecuteScalar().ToString();
|
||||
}
|
||||
catch (SqlCeException)
|
||||
{
|
||||
// Check the setting table for the version
|
||||
using (SqlCeCommand command = new SqlCeCommand("SELECT Value FROM Setting WHERE Name = 'DatabaseVersion'", connection))
|
||||
using (var command = new SqlCeCommand("SELECT Value FROM Setting WHERE Name = 'DatabaseVersion'", connection))
|
||||
versionString = command.ExecuteScalar().ToString();
|
||||
}
|
||||
|
||||
@@ -125,7 +123,7 @@ namespace FeedCenter.Data
|
||||
Tracer.WriteLine("Getting database file version");
|
||||
|
||||
// Get the database file version
|
||||
SqlServerCeFileVersion fileVersion = GetFileVersion(DatabasePath);
|
||||
var fileVersion = GetFileVersion(DatabasePath);
|
||||
|
||||
Tracer.WriteLine("Database file version: {0}", fileVersion);
|
||||
|
||||
@@ -135,7 +133,7 @@ namespace FeedCenter.Data
|
||||
Tracer.WriteLine("Creating database engine");
|
||||
|
||||
// Create the database engine
|
||||
using (SqlCeEngine engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath)))
|
||||
using (var engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath)))
|
||||
{
|
||||
Tracer.WriteLine("Upgrading database");
|
||||
|
||||
@@ -147,13 +145,13 @@ namespace FeedCenter.Data
|
||||
Tracer.WriteLine("Getting database version");
|
||||
|
||||
// Create a database connection
|
||||
using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", DatabasePath)))
|
||||
using (var connection = new SqlCeConnection(string.Format("Data Source={0}", DatabasePath)))
|
||||
{
|
||||
// Open the connection
|
||||
connection.Open();
|
||||
|
||||
// Get the database version
|
||||
int databaseVersion = getVersion(connection);
|
||||
var databaseVersion = GetVersion(connection);
|
||||
|
||||
// Create a dictionary of database upgrade scripts and their version numbers
|
||||
var scriptList = new Dictionary<int, string>();
|
||||
@@ -162,10 +160,10 @@ namespace FeedCenter.Data
|
||||
foreach (var property in typeof(Resources).GetProperties().Where(property => property.Name.StartsWith("DatabaseUpdate")))
|
||||
{
|
||||
// Get the name of the property
|
||||
string propertyName = property.Name;
|
||||
var propertyName = property.Name;
|
||||
|
||||
// Extract the version from the name
|
||||
int version = int.Parse(propertyName.Substring(propertyName.IndexOf("_", StringComparison.Ordinal) + 1));
|
||||
var version = int.Parse(propertyName.Substring(propertyName.IndexOf("_", StringComparison.Ordinal) + 1));
|
||||
|
||||
// Add to the script list
|
||||
scriptList[version] = propertyName;
|
||||
@@ -178,7 +176,7 @@ namespace FeedCenter.Data
|
||||
if (databaseVersion <= pair.Key)
|
||||
{
|
||||
// Get the script text
|
||||
string scriptText = Resources.ResourceManager.GetString(pair.Value);
|
||||
var scriptText = Resources.ResourceManager.GetString(pair.Value);
|
||||
|
||||
// Run the script
|
||||
ExecuteScript(scriptText);
|
||||
@@ -192,7 +190,7 @@ namespace FeedCenter.Data
|
||||
Tracer.WriteLine("Creating database engine");
|
||||
|
||||
// Create the database engine
|
||||
using (SqlCeEngine engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath)))
|
||||
using (var engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath)))
|
||||
{
|
||||
Tracer.WriteLine("Shrinking database");
|
||||
|
||||
@@ -204,22 +202,22 @@ namespace FeedCenter.Data
|
||||
private static void ExecuteScript(string scriptText)
|
||||
{
|
||||
// Create a database connection
|
||||
using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", DatabasePath)))
|
||||
using (var connection = new SqlCeConnection(string.Format("Data Source={0}", DatabasePath)))
|
||||
{
|
||||
// Open the connection
|
||||
connection.Open();
|
||||
|
||||
// Setup the delimiters
|
||||
string[] delimiters = new[] { "\r\nGO\r\n" };
|
||||
var delimiters = new[] { "\r\nGO\r\n" };
|
||||
|
||||
// Split the script at the delimiters
|
||||
string[] statements = scriptText.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
|
||||
var statements = scriptText.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
// Loop over each statement in the script
|
||||
foreach (string statement in statements)
|
||||
foreach (var statement in statements)
|
||||
{
|
||||
// Execute the statement
|
||||
using (SqlCeCommand command = new SqlCeCommand(statement, connection))
|
||||
using (var command = new SqlCeCommand(statement, connection))
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace FeedCenter.Data
|
||||
return null;
|
||||
|
||||
// Get the first table
|
||||
DataTable firstTable = dataSet.Tables[0];
|
||||
var firstTable = dataSet.Tables[0];
|
||||
|
||||
// If the table has no rows then return nothing
|
||||
if (firstTable.Rows.Count == 0)
|
||||
@@ -43,13 +43,13 @@ namespace FeedCenter.Data
|
||||
public static void SetStatement(this SqlCeCommand command, string statement, params object[] parameters)
|
||||
{
|
||||
// Create a new array to hold the updated parameters
|
||||
object[] formattedParameters = new object[parameters.Length];
|
||||
var formattedParameters = new object[parameters.Length];
|
||||
|
||||
// Initialize our position
|
||||
int position = 0;
|
||||
var position = 0;
|
||||
|
||||
// Loop over each parameter
|
||||
foreach (object parameter in parameters)
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
// If the parameter is a DateTime then we need to reformat
|
||||
if (parameter == null)
|
||||
@@ -60,10 +60,10 @@ namespace FeedCenter.Data
|
||||
else if (parameter is DateTime)
|
||||
{
|
||||
// Cast the parameter back to a DateTime
|
||||
DateTime dateTime = (DateTime) parameter;
|
||||
var dateTime = (DateTime) parameter;
|
||||
|
||||
// Convert the DateTime to sortable format
|
||||
string formatted = dateTime.ToString("s");
|
||||
var formatted = dateTime.ToString("s");
|
||||
|
||||
// Set into the formatted array
|
||||
formattedParameters[position++] = formatted;
|
||||
@@ -101,7 +101,7 @@ namespace FeedCenter.Data
|
||||
public static void ExecuteNonQuery(this SqlCeConnection connection, string query, params object[] parameters)
|
||||
{
|
||||
// Create the command object
|
||||
SqlCeCommand command = connection.CreateCommand();
|
||||
var command = connection.CreateCommand();
|
||||
|
||||
// Set the statement based on the query and parameters
|
||||
command.SetStatement(query, parameters);
|
||||
@@ -115,16 +115,16 @@ namespace FeedCenter.Data
|
||||
public static DataSet ExecuteDataSet(this SqlCeConnection connection, string query, params object[] parameters)
|
||||
{
|
||||
// Create the command object
|
||||
SqlCeCommand command = connection.CreateCommand();
|
||||
var command = connection.CreateCommand();
|
||||
|
||||
// Set the statement based on the query and parameters
|
||||
command.SetStatement(query, parameters);
|
||||
|
||||
// Create a new data adapter
|
||||
using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(command))
|
||||
using (var adapter = new SqlCeDataAdapter(command))
|
||||
{
|
||||
// Create the new data set
|
||||
using (DataSet dataSet = new DataSet())
|
||||
using (var dataSet = new DataSet())
|
||||
{
|
||||
//Tracer.WriteLine("Executing SQL query: {0}", command.CommandText);
|
||||
|
||||
@@ -139,7 +139,7 @@ namespace FeedCenter.Data
|
||||
public static object ExecuteScalar(this SqlCeConnection connection, string query, params object[] parameters)
|
||||
{
|
||||
// Create the command object
|
||||
SqlCeCommand command = connection.CreateCommand();
|
||||
var command = connection.CreateCommand();
|
||||
|
||||
// Set the statement based on the query and parameters
|
||||
command.SetStatement(query, parameters);
|
||||
|
||||
Reference in New Issue
Block a user