|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
namespace DAL.TestDB
{
public class DBTable
{
private String _DatabaseServerName;
private String _DatabaseName;
private String ConnectionStr;
public String LinqConnectionStr;
private String DefaultTableName = "DAL";
public DBTable(string DatabaseServerName, string DatabaseName)
{
_DatabaseServerName = DatabaseServerName;
_DatabaseName = DatabaseName;
// String ComputerName = "ThisPC"; (My.Computer.Name
String ComputerName = System.Net.Dns.GetHostName();
DefaultTableName = this.GetType().Name;
ConnectionStr = "Provider=sqloledb;workstation id='" + ComputerName + "';packet size=4096;integrated security=SSPI;data source='" + DatabaseServerName + "';persist security info=False;initial catalog=" + DatabaseName + "";
//ConnectionStr = "Provider=sqloledb;workstation id='" + ComputerName + "';packet size=4096;integrated security=SSPI;data source='" + DatabaseServerName + "';persist security info=False;initial catalog=" + DatabaseName + ";User Id=" + UserName + ";Password=" + Password + ";";
LinqConnectionStr = "Data Source=" + DatabaseServerName + ";Initial Catalog=" + DatabaseName + ";Integrated Security=True";
}
#region Conversion of objects
public bool ConvertInfoToInfo(object Input, object Output, ref String Message)
{
try
{
FieldInfo[] InputFields;
InputFields = Input.GetType().GetFields();
FieldInfo[] OutputFields;
OutputFields = Output.GetType().GetFields();
for (int i = 0; i <= InputFields.Length - 1; i++)
{
for (int j = 0; j <= OutputFields.Length - 1; j++)
{
if ((String)InputFields[i].Name == (String)OutputFields[j].Name)
{
OutputFields[j].SetValue(Output, InputFields[i].GetValue(Input));
}
}
}
return true;
}
catch (Exception ex)
{
Message = ex.ToString();
return false;
}
}
public bool ConvertTableStructureToLinq(object Input, object Output, ref String Message)
{
try
{
FieldInfo[] InputFields;
InputFields = Input.GetType().GetFields();
PropertyInfo[] OutputProperties;
OutputProperties = Output.GetType().GetProperties();
for (int i = 0; i <= InputFields.Length - 1; i++)
{
for (int j = 0; j <= OutputProperties.Length - 1; j++)
{
if ((String)InputFields[i].Name == (String)OutputProperties[j].Name)
{
OutputProperties[j].SetValue(Output, InputFields[i].GetValue(Input), null);
}
}
}
return true;
}
catch (Exception ex)
{
Message = ex.ToString();
return false;
}
}
public bool ConvertLinqToTableStructure(object Input, object Output, ref String Message)
{
try
{
PropertyInfo[] InputProperties;
InputProperties = Input.GetType().GetProperties();
FieldInfo[] OutputFields;
OutputFields = Output.GetType().GetFields();
for (int i = 0; i <= InputProperties.Length - 1; i++)
{
for (int j = 0; j <= OutputFields.Length - 1; j++)
{
if ((String)InputProperties[i].Name == (String)OutputFields[j].Name)
{
OutputFields[j].SetValue(Output, InputProperties[i].GetValue(Input,null));
}
}
}
return true;
}
catch (Exception ex)
{
Message = ex.ToString();
return false;
}
}
public bool CompareTableStructureToLinq(object TableStructure, object LinqTable, ref String Message)
{
try
{
FieldInfo[] TableStructureFields;
TableStructureFields = TableStructure.GetType().GetFields();
PropertyInfo[] LinqTableProperties;
LinqTableProperties = LinqTable.GetType().GetProperties();
for (int i = 0; i <= TableStructureFields.Length - 1; i++)
{
Boolean Found = false;
for (int j = 0; j <= LinqTableProperties.Length - 1; j++)
{
if ((String)TableStructureFields[i].Name == (String)LinqTableProperties[j].Name)
{
Found = true;
}
}
if (Found == false) return false;
}
return true;
}
catch (Exception ex)
{
Message = ex.ToString();
return false;
}
}
public bool CompareLinqToTableStructure(object LinqTable, object TableStructure, ref String Message)
{
try
{
FieldInfo[] TableStructureFields;
TableStructureFields = TableStructure.GetType().GetFields();
PropertyInfo[] LinqTableProperties;
LinqTableProperties = LinqTable.GetType().GetProperties();
for (int j = 0; j <= LinqTableProperties.Length - 1; j++)
{
Boolean Found = false;
switch (LinqTableProperties[j].PropertyType.FullName )
{
case "System.Int32":
case "System.Boolean":
case "System.Double":
case "System.String":
case "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
case "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
for (int i = 0; i <= TableStructureFields.Length - 1; i++)
{
if ((String)TableStructureFields[i].Name == (String)LinqTableProperties[j].Name)
{
Found = true;
}
}
if (Found == false) return false;
break;
}
}
return true;
}
catch (Exception ex)
{
Message = ex.ToString();
return false;
}
}
#endregion
#region OLEDB
public OleDbConnection objConn = new OleDbConnection();
#region Basic database operations
public OleDbConnection OpenConnection()
{
OleDbConnection objConnection = new OleDbConnection();
if (objConn == null)
{
objConnection.ConnectionString = ConnectionStr;
objConnection.Open();
return objConnection;
};
if (objConn.State != ConnectionState.Open)
{
objConnection.ConnectionString = ConnectionStr;
objConnection.Open();
return objConnection;
}
else
{
return objConn;
}
}
public DataSet SQLDS(string SQL)
{
System.Data.DataSet ds = new DataSet();
objConn = OpenConnection();
OleDbDataAdapter objCmd = new OleDbDataAdapter(SQL, objConn);
objCmd.Fill(ds, DefaultTableName);
objCmd.Dispose();
return ds;
}
public bool ExecuteSQL(string SQL)
{
objConn = OpenConnection();
OleDbCommand objCmd = new OleDbCommand(SQL, objConn);
objCmd.CommandType = CommandType.Text;
objCmd.ExecuteNonQuery();
objCmd.Dispose();
return true;
}
#endregion
#region Higher database operations
public DataSet GetDS(String TableName)
{
string SQL = "SELECT * FROM " + TableName + " ";
DataSet ds = new DataSet();
ds = SQLDS(SQL);
return ds;
}
#endregion
#region Database string manipulation
public string DBSTR(string sStr )
{
// Function to convert strings into a format that the database can take
sStr = sStr.Replace("'", "''");
return sStr;
}
#endregion
#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.