Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / XML

Create Data Classes

Rate me:
Please Sign up or sign in to vote.
4.88/5 (31 votes)
4 Mar 2011CPOL10 min read 135.9K   2.5K   167  
An application that creates a C# class to read/write data to/from an Access, SQLite, or XML database.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace JGCreateDataClasses
{
	internal class JTestDataSQLite
	{
		private String m_JAnsiString = "";
		private String m_JAnsiStringFixedLength = "";
		private Object m_JBinary = "";
		private Boolean m_JBoolean;
		private Byte m_JByte = 0;
		private Decimal m_JCurrency = 0;
		private DateTime m_JDate;
		private DateTime m_JDateTime;
		private DateTime m_JDateTime2;
		private DateTime m_JDateTimeOffset;
		private Decimal m_JDecimal = 0;
		private Double m_JDouble = 0;
		private Guid m_JGuid = new Guid();
		private Int16 m_JInt16 = 0;
		private Int32 m_JInt32 = 0;
		private Int64 m_JInt64 = 0;
		private Object m_JObject = "";
		private SByte m_JSByte = 0;
		private Single m_JSingle = 0;
		private String m_JString = "None";
		private String m_JStringFixedLength = "";
		private DateTime m_JTime;
		private UInt16 m_JUInt16 = 0;
		private UInt32 m_JUInt32 = 0;
		private UInt64 m_JUInt64 = 0;
		private Double m_JVarNumeric = 0;
		private String m_JXml = "";
		private JCommon.ClassDataTypes m_JUserDefined = JCommon.ClassDataTypes.ADONET;

		//	Bare constructor
		internal JTestDataSQLite()
		{
			this.m_JInt32 = 0;
		}

		//	Constructor from key field number
		internal JTestDataSQLite(System.Int32 jInt32)
		{
			JTestDataSQLiteUpdater.GetRecord(jInt32, this);
		}

		//	Constructor from string override
		internal JTestDataSQLite(String jString)
		{
			JTestDataSQLiteUpdater.GetRecord(jString, this);
		}

		//	String override for main class - must be public
		public override string ToString()
		{
			return this.m_JString;
		}

		//	Lightweight class for adding to ListView, Combo etc
		//	Contains the Key field and string override
		internal class JTestDataSQLiteIndexData
		{
			internal Int32 JInt32;
			internal string JString = "None";

			internal JTestDataSQLiteIndexData()
			{
			}

			internal JTestDataSQLiteIndexData(JTestDataSQLite testDataSQLite)
			{
				this.JInt32 = testDataSQLite.JInt32;
				this.JString = testDataSQLite.JString;
			}

			//	String override for index data - must be public
			public override string ToString()
			{
				return this.JString;
			}
		}

		#region Public Properties

		//	TODO Check if some should be read only

		internal String JAnsiString
		{
			get { return this.m_JAnsiString; }
			set { this.m_JAnsiString = value; }
		}

		internal String JAnsiStringFixedLength
		{
			get { return this.m_JAnsiStringFixedLength; }
			set { this.m_JAnsiStringFixedLength = value; }
		}

		internal Object JBinary
		{
			get { return this.m_JBinary; }
			set { this.m_JBinary = value; }
		}

		internal Boolean JBoolean
		{
			get { return this.m_JBoolean; }
			set { this.m_JBoolean = value; }
		}

		internal Byte JByte
		{
			get { return this.m_JByte; }
			set { this.m_JByte = value; }
		}

		internal Decimal JCurrency
		{
			get { return this.m_JCurrency; }
			set { this.m_JCurrency = value; }
		}

		internal DateTime JDate
		{
			get { return this.m_JDate; }
			set { this.m_JDate = value; }
		}

		internal DateTime JDateTime
		{
			get { return this.m_JDateTime; }
			set { this.m_JDateTime = value; }
		}

		internal DateTime JDateTime2
		{
			get { return this.m_JDateTime2; }
			set { this.m_JDateTime2 = value; }
		}

		internal DateTime JDateTimeOffset
		{
			get { return this.m_JDateTimeOffset; }
			set { this.m_JDateTimeOffset = value; }
		}

		internal Decimal JDecimal
		{
			get { return this.m_JDecimal; }
			set { this.m_JDecimal = value; }
		}

		internal Double JDouble
		{
			get { return this.m_JDouble; }
			set { this.m_JDouble = value; }
		}

		internal Guid JGuid
		{
			get { return this.m_JGuid; }
			set { this.m_JGuid = value; }
		}

		internal Int16 JInt16
		{
			get { return this.m_JInt16; }
			set { this.m_JInt16 = value; }
		}

		internal Int32 JInt32
		{
			get { return this.m_JInt32; }
			set { this.m_JInt32 = value; }
		}

		internal Int64 JInt64
		{
			get { return this.m_JInt64; }
			set { this.m_JInt64 = value; }
		}

		internal Object JObject
		{
			get { return this.m_JObject; }
			set { this.m_JObject = value; }
		}

		internal SByte JSByte
		{
			get { return this.m_JSByte; }
			set { this.m_JSByte = value; }
		}

		internal Single JSingle
		{
			get { return this.m_JSingle; }
			set { this.m_JSingle = value; }
		}

		internal String JString
		{
			get { return this.m_JString; }
			set { this.m_JString = value; }
		}

		internal String JStringFixedLength
		{
			get { return this.m_JStringFixedLength; }
			set { this.m_JStringFixedLength = value; }
		}

		internal DateTime JTime
		{
			get { return this.m_JTime; }
			set { this.m_JTime = value; }
		}

		internal UInt16 JUInt16
		{
			get { return this.m_JUInt16; }
			set { this.m_JUInt16 = value; }
		}

		internal UInt32 JUInt32
		{
			get { return this.m_JUInt32; }
			set { this.m_JUInt32 = value; }
		}

		internal UInt64 JUInt64
		{
			get { return this.m_JUInt64; }
			set { this.m_JUInt64 = value; }
		}

		internal Double JVarNumeric
		{
			get { return this.m_JVarNumeric; }
			set { this.m_JVarNumeric = value; }
		}

		internal String JXml
		{
			get { return this.m_JXml; }
			set { this.m_JXml = value; }
		}

		internal JCommon.ClassDataTypes JUserDefined
		{
			get { return this.m_JUserDefined; }
			set { this.m_JUserDefined = value; }
		}


		#endregion

		#region Additional Fields

		//	Calculated Fields - NB These are not saved in the database



		#endregion

	}


	#region Updater Class


	static class JTestDataSQLiteUpdater
	{
		internal static string m_DBPath = @"D:\Data\Visual Studio 2008\Projects\JGCreateDataClasses\TestData\JTypes.db3";
		internal static string m_TableName = "tblTest";

		//	AddRecord
		internal static int AddRecord(JTestDataSQLite testDataSQLite)
		{
			//	Add Only - Use UpdateRecord for existing records
			int result = 0;

			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteCommand command = GetInsertCommand(testDataSQLite);
				command.Connection = dbConnection;

				// Execute the command
				command.ExecuteScalar();

				// Create Command to get IDENTITY Value
				SQLiteCommand oleCmdGetIdentity = new SQLiteCommand();
				oleCmdGetIdentity.CommandText = "SELECT last_insert_rowid()";
				oleCmdGetIdentity.Connection = dbConnection;
				result = Int32.Parse(oleCmdGetIdentity.ExecuteScalar().ToString());

				// Close and dispose
				command.Dispose();
				oleCmdGetIdentity.Dispose();
			}
			// Set return value
			return result;
		}

		//	Returns all index data in a List
		internal static List<JTestDataSQLite.JTestDataSQLiteIndexData> AllIndexData()
		{
			List<JTestDataSQLite.JTestDataSQLiteIndexData> listRecords = new List<JTestDataSQLite.JTestDataSQLiteIndexData>();

			//	Create the SelectCommand.
			string selectString = "SELECT JInt32, JString FROM " + m_TableName;
			selectString += " ORDER BY JString";

			return GetIndexDataList(selectString);
		}

		//	Returns all records in a List
		internal static List<JTestDataSQLite> AllRecords()
		{
			//	Create the SelectCommand.
			string selectString = "SELECT *  FROM " + m_TableName;
			selectString += " ORDER BY JString";
			return GetRecordList(selectString);
		}

		//	Returns all records in a List
		internal static List<JTestDataSQLite> AllRecords(string sortField)
		{
			//	Create the SelectCommand.
			string selectString = "SELECT *  FROM " + m_TableName;
			selectString += " ORDER BY " + sortField;
			return GetRecordList(selectString);
		}

		private static SQLiteDataAdapter CreateSelectAdapter(SQLiteConnection connection, string selectString)
		{
			SQLiteDataAdapter oledbAdapter = new SQLiteDataAdapter(selectString, connection);
			return oledbAdapter;
		}

		//	DeleteRecord
		internal static bool DeleteRecord(JTestDataSQLite testDataSQLite)
		{
			//	Create Delete Command
			string deleteString = "DELETE FROM ";
			deleteString += m_TableName;
			deleteString += " WHERE JInt32 = ";
			deleteString += testDataSQLite.JInt32.ToString();

			try
			{
				using (SQLiteConnection dbConnection = GetDBConnection())
				{
					dbConnection.Open();
					SQLiteCommand dbCommand = new SQLiteCommand(deleteString, dbConnection);
					dbCommand.ExecuteScalar();
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		//	ExecuteNonQuery
		internal static bool ExecuteNonQuery(string commandString)
		{
			try
			{
				using (SQLiteConnection dbConnection = GetDBConnection())
				{
					dbConnection.Open();
					SQLiteCommand dbCommand = new SQLiteCommand(commandString, dbConnection);
					dbCommand.ExecuteNonQuery();
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		//	GetField
		internal static object GetField(System.Int32 jInt32, string fieldName)
		{
			object obTemp = null;

			// Create the SelectCommand.
			string selectString = "SELECT " + fieldName + " FROM " + m_TableName;
			selectString += " WHERE JInt32 = ";
			selectString += jInt32.ToString();

			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteDataAdapter dbDataAdapter = CreateSelectAdapter(dbConnection, selectString);

				//	Create / Fill the dataset
				DataSet dataSet = new DataSet();
				dbDataAdapter.Fill(dataSet, m_TableName);

				// Get the Table from the Dataset
				DataTable dataTable = dataSet.Tables[0];
				DataRow dataRow = dataTable.Rows[0];
				obTemp = dataRow[fieldName].ToString();
			}
			return obTemp;
		}

		//	Returns all index data in a List
		internal static List<JTestDataSQLite.JTestDataSQLiteIndexData> GetIndexDataList(string selectString)
		{
			List<JTestDataSQLite.JTestDataSQLiteIndexData> listRecords = new List<JTestDataSQLite.JTestDataSQLiteIndexData>();
			JTestDataSQLite.JTestDataSQLiteIndexData indexData;

			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteDataAdapter dbDataAdapter = CreateSelectAdapter(dbConnection, selectString);

				//	Create / Fill the dataset
				DataSet dataSet = new DataSet();
				dbDataAdapter.Fill(dataSet, m_TableName);

				//	Get the Table from the Dataset
				DataTable dataTable = dataSet.Tables[0];

				// Loop through the Dataset and add each Row to the List
				foreach (DataRow dataRow in dataTable.Rows)
				{
					indexData = new JTestDataSQLite.JTestDataSQLiteIndexData();
					GetRecordDetailsForIndexdata(dataRow, ref indexData);
					listRecords.Add(indexData);
				}
			}
			return listRecords;
		}

		//	GetInsertCommand
		private static SQLiteCommand GetInsertCommand(JTestDataSQLite testDataSQLite)
		{
			string insertCommand;

			insertCommand = "INSERT INTO " + m_TableName;
			insertCommand += "(";
			insertCommand += " JAnsiString,";
			insertCommand += " JAnsiStringFixedLength,";
			insertCommand += " JBinary,";
			insertCommand += " JBoolean,";
			insertCommand += " JByte,";
			insertCommand += " JCurrency,";
			insertCommand += " JDate,";
			insertCommand += " JDateTime,";
			insertCommand += " JDateTime2,";
			insertCommand += " JDateTimeOffset,";
			insertCommand += " JDecimal,";
			insertCommand += " JDouble,";
			insertCommand += " JGuid,";
			insertCommand += " JInt16,";
			insertCommand += " JInt64,";
			insertCommand += " JObject,";
			insertCommand += " JSByte,";
			insertCommand += " JSingle,";
			insertCommand += " JString,";
			insertCommand += " JStringFixedLength,";
			insertCommand += " JTime,";
			insertCommand += " JUInt16,";
			insertCommand += " JUInt32,";
			insertCommand += " JUInt64,";
			insertCommand += " JVarNumeric,";
			insertCommand += " JXml,";
			insertCommand += " JUserDefined";
			insertCommand += " )";

			insertCommand += " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

			// Create the InsertCommand
			SQLiteCommand commandInsert = new SQLiteCommand(insertCommand);

			commandInsert.Parameters.Add(new SQLiteParameter("@JAnsiString", testDataSQLite.JAnsiString));
			commandInsert.Parameters.Add(new SQLiteParameter("@JAnsiStringFixedLength", testDataSQLite.JAnsiStringFixedLength));
			commandInsert.Parameters.Add(new SQLiteParameter("@JBinary", testDataSQLite.JBinary));
			commandInsert.Parameters.Add(new SQLiteParameter("@JBoolean", testDataSQLite.JBoolean));
			commandInsert.Parameters.Add(new SQLiteParameter("@JByte", testDataSQLite.JByte));
			commandInsert.Parameters.Add(new SQLiteParameter("@JCurrency", testDataSQLite.JCurrency));
			commandInsert.Parameters.Add(new SQLiteParameter("@JDate", testDataSQLite.JDate.ToBinary()));
			commandInsert.Parameters.Add(new SQLiteParameter("@JDateTime", testDataSQLite.JDateTime.ToBinary()));
			commandInsert.Parameters.Add(new SQLiteParameter("@JDateTime2", testDataSQLite.JDateTime2.ToBinary()));
			commandInsert.Parameters.Add(new SQLiteParameter("@JDateTimeOffset", testDataSQLite.JDateTimeOffset.ToBinary()));
			commandInsert.Parameters.Add(new SQLiteParameter("@JDecimal", testDataSQLite.JDecimal));
			commandInsert.Parameters.Add(new SQLiteParameter("@JDouble", testDataSQLite.JDouble));
			commandInsert.Parameters.Add(new SQLiteParameter("@JGuid", testDataSQLite.JGuid.ToString()));
			commandInsert.Parameters.Add(new SQLiteParameter("@JInt16", testDataSQLite.JInt16));
			commandInsert.Parameters.Add(new SQLiteParameter("@JInt64", testDataSQLite.JInt64));
			commandInsert.Parameters.Add(new SQLiteParameter("@JObject", testDataSQLite.JObject));
			commandInsert.Parameters.Add(new SQLiteParameter("@JSByte", testDataSQLite.JSByte));
			commandInsert.Parameters.Add(new SQLiteParameter("@JSingle", testDataSQLite.JSingle));
			commandInsert.Parameters.Add(new SQLiteParameter("@JString", testDataSQLite.JString));
			commandInsert.Parameters.Add(new SQLiteParameter("@JStringFixedLength", testDataSQLite.JStringFixedLength));
			commandInsert.Parameters.Add(new SQLiteParameter("@JTime", testDataSQLite.JTime.ToBinary()));
			commandInsert.Parameters.Add(new SQLiteParameter("@JUInt16", testDataSQLite.JUInt16));
			commandInsert.Parameters.Add(new SQLiteParameter("@JUInt32", testDataSQLite.JUInt32));
			commandInsert.Parameters.Add(new SQLiteParameter("@JUInt64", testDataSQLite.JUInt64));
			commandInsert.Parameters.Add(new SQLiteParameter("@JVarNumeric", testDataSQLite.JVarNumeric));
			commandInsert.Parameters.Add(new SQLiteParameter("@JXml", testDataSQLite.JXml));
			commandInsert.Parameters.Add(new SQLiteParameter("@JUserDefined", (Int32)testDataSQLite.JUserDefined));

			return commandInsert;
		}

		//	GetDBConnection
		internal static SQLiteConnection GetDBConnection()
		{
			string connectionString = "Data Source = ";
			connectionString += m_DBPath;
			SQLiteConnection connection = null;
			try
			{
				connection = new SQLiteConnection(connectionString);
			}
			catch
			{
			}
			return connection;
		}

		//	GetRecord (From Key Field)
		internal static void GetRecord(System.Int32 jInt32, JTestDataSQLite testDataSQLite)
		{
			//	Create the SelectCommand
			string selectString = "SELECT *  FROM " + m_TableName;
			selectString += " WHERE JInt32 = ";
			selectString += jInt32.ToString();

			GetSingleRecord(selectString, ref testDataSQLite);
		}

		//	GetRecord (From string Override)
		internal static void GetRecord(string jString, JTestDataSQLite testDataSQLite)
		{
			//	Create the SelectCommand.
			string selectString = "SELECT *  FROM " + m_TableName;
			selectString += " WHERE JString = " + "\"" + jString + "\"";

			GetSingleRecord(selectString, ref testDataSQLite);
		}

		//	GetRecordDetails
		private static bool GetRecordDetails(DataRow dataRow, ref JTestDataSQLite testDataSQLite)
		{
			bool result = true;

			//	Use try catch for numeric fields which may not have been initialised

			testDataSQLite.JAnsiString = Convert.ToString(dataRow["JAnsiString"]);
			testDataSQLite.JAnsiStringFixedLength = Convert.ToString(dataRow["JAnsiStringFixedLength"]);
			testDataSQLite.JBinary = dataRow["JBinary"];
			testDataSQLite.JBoolean = Convert.ToBoolean(dataRow["JBoolean"]);
			try
			{
				testDataSQLite.JByte = Convert.ToByte(dataRow["JByte"]);
			}
			catch { }
			try
			{
				testDataSQLite.JCurrency = Convert.ToDecimal(dataRow["JCurrency"]);
			}
			catch { }
			testDataSQLite.JDate = Convert.ToDateTime(DateTime.FromBinary(Convert.ToInt64(dataRow["JDate"])));
			testDataSQLite.JDateTime = Convert.ToDateTime(DateTime.FromBinary(Convert.ToInt64(dataRow["JDateTime"])));
			testDataSQLite.JDateTime2 = Convert.ToDateTime(DateTime.FromBinary(Convert.ToInt64(dataRow["JDateTime2"])));
			testDataSQLite.JDateTimeOffset = Convert.ToDateTime(DateTime.FromBinary(Convert.ToInt64(dataRow["JDateTimeOffset"])));
			try
			{
				testDataSQLite.JDecimal = Convert.ToDecimal(dataRow["JDecimal"]);
			}
			catch { }
			try
			{
				testDataSQLite.JDouble = Convert.ToDouble(dataRow["JDouble"]);
			}
			catch { }
			testDataSQLite.JGuid = new Guid(Convert.ToString(dataRow["JGuid"]));
			try
			{
				testDataSQLite.JInt16 = Convert.ToInt16(dataRow["JInt16"]);
			}
			catch { }
			try
			{
				testDataSQLite.JInt32 = Convert.ToInt32(dataRow["JInt32"]);
			}
			catch { }
			try
			{
				testDataSQLite.JInt64 = Convert.ToInt64(dataRow["JInt64"]);
			}
			catch { }
			testDataSQLite.JObject = dataRow["JObject"];
			try
			{
				testDataSQLite.JSByte = Convert.ToSByte(dataRow["JSByte"]);
			}
			catch { }
			try
			{
				testDataSQLite.JSingle = Convert.ToSingle(dataRow["JSingle"]);
			}
			catch { }
			testDataSQLite.JString = Convert.ToString(dataRow["JString"]);
			testDataSQLite.JStringFixedLength = Convert.ToString(dataRow["JStringFixedLength"]);
			testDataSQLite.JTime = Convert.ToDateTime(DateTime.FromBinary(Convert.ToInt64(dataRow["JTime"])));
			try
			{
				testDataSQLite.JUInt16 = Convert.ToUInt16(dataRow["JUInt16"]);
			}
			catch { }
			try
			{
				testDataSQLite.JUInt32 = Convert.ToUInt32(dataRow["JUInt32"]);
			}
			catch { }
			try
			{
				testDataSQLite.JUInt64 = Convert.ToUInt64(dataRow["JUInt64"]);
			}
			catch { }
			try
			{
				testDataSQLite.JVarNumeric = Convert.ToDouble(dataRow["JVarNumeric"]);
			}
			catch { }
			testDataSQLite.JXml = Convert.ToString(dataRow["JXml"]);
			try
			{
				testDataSQLite.JUserDefined = (JCommon.ClassDataTypes)Convert.ToInt32(dataRow["JUserDefined"]);
			}
			catch { }

			return result;
		}

		//	GetRecordDetailsForIndexdata
		private static bool GetRecordDetailsForIndexdata(DataRow dataRow, ref JTestDataSQLite.JTestDataSQLiteIndexData indexData)
		{
			bool result = true;

			indexData.JInt32 = Convert.ToInt32(dataRow["JInt32"]);
			indexData.JString = Convert.ToString(dataRow["JString"]);

			return result;
		}

		//	Returns required records in a List
		internal static List<JTestDataSQLite> GetRecordList(string selectString)
		{
			List<JTestDataSQLite> listRecords = new List<JTestDataSQLite>();
			JTestDataSQLite testDataSQLite;

			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteDataAdapter dbDataAdapter = CreateSelectAdapter(dbConnection, selectString);

				//	Create / Fill the dataset
				DataSet dataSet = new DataSet();
				dbDataAdapter.Fill(dataSet, m_TableName);

				//	Get the Table from the Dataset
				DataTable dataTable = dataSet.Tables[0];

				// Loop through the Dataset and add each Row to the List
				foreach (DataRow dataRow in dataTable.Rows)
				{
					testDataSQLite = new JTestDataSQLite();
					GetRecordDetails(dataRow, ref testDataSQLite);
					listRecords.Add(testDataSQLite);
				}
			}
			return listRecords;
		}

		//	GetSingleRecord
		internal static JTestDataSQLite GetSingleRecord(string selectString)
		{
			JTestDataSQLite testDataSQLite = new JTestDataSQLite();

			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteDataAdapter dbDataAdapter = CreateSelectAdapter(dbConnection, selectString);

				//	Create / Fill the dataset
				DataSet dataSet = new DataSet();
				dbDataAdapter.Fill(dataSet, m_TableName);

				//	Get the Table from the Dataset
				DataTable dataTable = dataSet.Tables[0];

				if (dataTable.Rows.Count > 0)
				{
					DataRow dataRow = dataTable.Rows[0];
					GetRecordDetails(dataRow, ref testDataSQLite);
				}
			}
			return testDataSQLite;
		}

		//	GetSingleRecord
		private static void GetSingleRecord(string selectString, ref JTestDataSQLite testDataSQLite)
		{
			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteDataAdapter dbDataAdapter = CreateSelectAdapter(dbConnection, selectString);

				//	Create / Fill the dataset
				DataSet dataSet = new DataSet();
				dbDataAdapter.Fill(dataSet, m_TableName);

				//	Get the Table from the Dataset
				DataTable dataTable = dataSet.Tables[0];

				if (dataTable.Rows.Count > 0)
				{
					DataRow dataRow = dataTable.Rows[0];
					GetRecordDetails(dataRow, ref testDataSQLite);
				}
			}
		}

		//	GetUpdateCommand
		private static SQLiteCommand GetUpdateCommand(JTestDataSQLite testDataSQLite)
		{
			string cmdText;

			cmdText = "UPDATE " + m_TableName;
			cmdText += " set JAnsiString = ?,";
			cmdText += " JAnsiStringFixedLength = ?,";
			cmdText += " JBinary = ?,";
			cmdText += " JBoolean = ?,";
			cmdText += " JByte = ?,";
			cmdText += " JCurrency = ?,";
			cmdText += " JDate = ?,";
			cmdText += " JDateTime = ?,";
			cmdText += " JDateTime2 = ?,";
			cmdText += " JDateTimeOffset = ?,";
			cmdText += " JDecimal = ?,";
			cmdText += " JDouble = ?,";
			cmdText += " JGuid = ?,";
			cmdText += " JInt16 = ?,";
			cmdText += " JInt64 = ?,";
			cmdText += " JObject = ?,";
			cmdText += " JSByte = ?,";
			cmdText += " JSingle = ?,";
			cmdText += " JString = ?,";
			cmdText += " JStringFixedLength = ?,";
			cmdText += " JTime = ?,";
			cmdText += " JUInt16 = ?,";
			cmdText += " JUInt32 = ?,";
			cmdText += " JUInt64 = ?,";
			cmdText += " JVarNumeric = ?,";
			cmdText += " JXml = ?,";
			cmdText += " JUserDefined = ?";
			cmdText += " WHERE JInt32 = ";
			cmdText += testDataSQLite.JInt32.ToString();

			// Create the UpdateCommand
			SQLiteCommand commandUpdate = new SQLiteCommand(cmdText);

			commandUpdate.Parameters.Add(new SQLiteParameter("JAnsiString", testDataSQLite.JAnsiString));
			commandUpdate.Parameters.Add(new SQLiteParameter("JAnsiStringFixedLength", testDataSQLite.JAnsiStringFixedLength));
			commandUpdate.Parameters.Add(new SQLiteParameter("JBinary", testDataSQLite.JBinary));
			commandUpdate.Parameters.Add(new SQLiteParameter("JBoolean", testDataSQLite.JBoolean));
			commandUpdate.Parameters.Add(new SQLiteParameter("JByte", testDataSQLite.JByte));
			commandUpdate.Parameters.Add(new SQLiteParameter("JCurrency", testDataSQLite.JCurrency));
			commandUpdate.Parameters.Add(new SQLiteParameter("JDate", testDataSQLite.JDate.ToBinary()));
			commandUpdate.Parameters.Add(new SQLiteParameter("JDateTime", testDataSQLite.JDateTime.ToBinary()));
			commandUpdate.Parameters.Add(new SQLiteParameter("JDateTime2", testDataSQLite.JDateTime2.ToBinary()));
			commandUpdate.Parameters.Add(new SQLiteParameter("JDateTimeOffset", testDataSQLite.JDateTimeOffset.ToBinary()));
			commandUpdate.Parameters.Add(new SQLiteParameter("JDecimal", testDataSQLite.JDecimal));
			commandUpdate.Parameters.Add(new SQLiteParameter("JDouble", testDataSQLite.JDouble));
			commandUpdate.Parameters.Add(new SQLiteParameter("JGuid", testDataSQLite.JGuid.ToString()));
			commandUpdate.Parameters.Add(new SQLiteParameter("JInt16", testDataSQLite.JInt16));
			commandUpdate.Parameters.Add(new SQLiteParameter("JInt64", testDataSQLite.JInt64));
			commandUpdate.Parameters.Add(new SQLiteParameter("JObject", testDataSQLite.JObject));
			commandUpdate.Parameters.Add(new SQLiteParameter("JSByte", testDataSQLite.JSByte));
			commandUpdate.Parameters.Add(new SQLiteParameter("JSingle", testDataSQLite.JSingle));
			commandUpdate.Parameters.Add(new SQLiteParameter("JString", testDataSQLite.JString));
			commandUpdate.Parameters.Add(new SQLiteParameter("JStringFixedLength", testDataSQLite.JStringFixedLength));
			commandUpdate.Parameters.Add(new SQLiteParameter("JTime", testDataSQLite.JTime.ToBinary()));
			commandUpdate.Parameters.Add(new SQLiteParameter("JUInt16", testDataSQLite.JUInt16));
			commandUpdate.Parameters.Add(new SQLiteParameter("JUInt32", testDataSQLite.JUInt32));
			commandUpdate.Parameters.Add(new SQLiteParameter("JUInt64", testDataSQLite.JUInt64));
			commandUpdate.Parameters.Add(new SQLiteParameter("JVarNumeric", testDataSQLite.JVarNumeric));
			commandUpdate.Parameters.Add(new SQLiteParameter("JXml", testDataSQLite.JXml));
			commandUpdate.Parameters.Add(new SQLiteParameter("JUserDefined", (Int32)testDataSQLite.JUserDefined));

			return commandUpdate;
		}

		//	UpdateField
		internal static bool UpdateField(System.Int32 JInt32, string fieldName, object value)
		{
			// Create the UpdateCommand
			string cmdText = "UPDATE " + m_TableName;
			cmdText += " set " + fieldName + " = ?";
			cmdText += " WHERE JInt32 = ";
			cmdText += JInt32.ToString();

			try
			{
				//	Create Connection)
				using (SQLiteConnection dbConnection = GetDBConnection())
				{
					dbConnection.Open();

					SQLiteCommand commandUpdate = new SQLiteCommand(cmdText, dbConnection);
					commandUpdate.Parameters.Add(new SQLiteParameter(fieldName, value));

					//	Execute
					commandUpdate.ExecuteNonQuery();
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		internal static bool UpdateRecord(JTestDataSQLite testDataSQLite)
		{
			//	Update Only - Use AddRecord for new records
			bool result = false;

			//	Create Connection
			using (SQLiteConnection dbConnection = GetDBConnection())
			{
				dbConnection.Open();

				SQLiteCommand command = GetUpdateCommand(testDataSQLite);
				command.Connection = dbConnection;

				//Execute
				try
				{
					command.ExecuteNonQuery();
					result = true;
				}
				catch
				{
					result = false;
				}

			}
			return result;
		}

		internal static object GetJDate(System.Int32 JInt32)
		{
			return GetField(JInt32, "JDate");
		}

		internal static bool UpdateJDate(System.Int32 JInt32, object fieldValue)
		{
			return UpdateField(JInt32, "JDate", fieldValue);
		}

		internal static object GetJDouble(System.Int32 JInt32)
		{
			return GetField(JInt32, "JDouble");
		}

		internal static bool UpdateJDouble(System.Int32 JInt32, object fieldValue)
		{
			return UpdateField(JInt32, "JDouble", fieldValue);
		}

	}


	#endregion



	#region Additional Functions Class


	static class JTestDataSQLiteFunctions
	{
	}


	#endregion


}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
I have been a keen hobbyist programmer since getting my first computer - a Vic 20 (you had to be able to write programs then since few programs were available and all were expensive).
Retired and now living in Pewsey, Wiltshire, where I spend (far too much of) my time writing computer programs to keep my mind active.

Comments and Discussions