Using System;
Using System.Collections.Generic;
Using System.Data;
Using System.Data.SqlClient;
Using System.Data.Common;
Using Microsoft.Practices.EnterpriseLibrary.Data;
Using EFWCoreLib.CoreFrame.EntLib;
Namespace EFWCoreLib.CoreFrame.DbProvider
{
<summary>
Entlibdb
</summary>
public class Entlibdb:abstractdatabase
{
Public Entlibdb ()
: Base ()
{
Database = Zhycontainer.createdatabase ();
_connstring = database. ConnectionString;
Switch (database. GetType (). Name)
{
Case "SQLDatabase":
DbType = databasetype.sqlserver2005;
Break
Case "Oracledatabase":
DbType = databasetype.oracle;
Break
Default
DbType = Databasetype.unknown;
Break
}
}
Public entlibdb (String key)
: Base ()
{
Database = Zhycontainer.createdatabase (key);
_connstring = database. ConnectionString;
Switch (database. GetType (). Name)
{
Case "SQLDatabase":
DbType = databasetype.sqlserver2005;
Break
Case "Oracledatabase":
DbType = databasetype.oracle;
Break
Default
DbType = Databasetype.unknown;
Break
}
}
public override void Testdbconnection ()
{
Database. CreateConnection (). Open ();
}
public override void BeginTransaction ()
{
Try
{
if (isintransaction = = False)
{
Connection = database. CreateConnection ();
Connection. Open ();
Transaction = connection. BeginTransaction ();
IsInTransaction = true;
}
Else
{
throw new Exception ("The transaction is in progress, an object cannot open multiple transactions at the same time!") ");
}
}
catch (Exception e)
{
Connection. Close ();
IsInTransaction = false;
throw new Exception ("The transaction failed to start, please try again!") \ n "+ e.message);
}
}
public override void CommitTransaction ()
{
if (transaction! = NULL)
{
Transaction.commit ();
IsInTransaction = false;
Connection. Close ();
}
Else
throw new Exception ("No Transaction available! ");
}
public override void RollbackTransaction ()
{
if (transaction! = NULL)
{
Transaction. Rollback ();
IsInTransaction = false;
Connection. Close ();
}
Else
throw new Exception ("No Transaction available! ");
}
public override int Insertrecord (string commandtext)
{
Switch (DbType)
{
Case Databasetype.oracle:
String strSQL = "Select Test_sql.nextval from Dual"; SELECT @ @IDENTITY
if (isintransaction)
{
Command = database. Getsqlstringcommand (CommandText);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Command.commandtext = Command.commandtext + "; SELECT @ @IDENTITY ";
return Convert.ToInt32 (database. ExecuteScalar (command, transaction));
}
Else
{
Command = database. Getsqlstringcommand (CommandText);
Command.commandtext = Command.commandtext + "; SELECT @ @IDENTITY ";
return Convert.ToInt32 (database. ExecuteScalar (command));
}
Case DATABASETYPE.SQLSERVER2005:
Case databasetype.sqlserver2000:
if (isintransaction)
{
Command = database. Getsqlstringcommand (CommandText);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Command.commandtext = Command.commandtext + "; SELECT @ @IDENTITY ";
return Convert.ToInt32 (database. ExecuteScalar (command, transaction));
}
Else
{
Command = database. Getsqlstringcommand (CommandText);
Command.commandtext = Command.commandtext + "; SELECT @ @IDENTITY ";
return Convert.ToInt32 (database. ExecuteScalar (command));
}
Case DATABASETYPE.IBMDB2:
throw new Exception ("database operation not implemented IBMDB2! ");
Break
Case DATABASETYPE.MYSQL:
throw new Exception ("MySQL database operation not implemented! ");
Break
Default
throw new Exception ("Database operation not implemented! ");
Break
}
}
public override DataTable Getdatatable (string commandtext)
{
DataSet ds = null;
if (isintransaction)
{
Command = new SqlCommand (commandtext);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
ds = database. ExecuteDataset (command, transaction);
}
Else
{
ds = database. ExecuteDataset (CommandType.Text, CommandText);
}
if (ds! = null && ds. Tables.count > 0)
{
Return DS. Tables[0];
}
throw new Exception ("no data");
}
public override DataTable Getdatatable (string storeprocedurename, params object[] parameters)
{
DataSet ds = null;
if (isintransaction)
{
Command = database. Getstoredproccommand (storeprocedurename, parameters);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
ds = database. ExecuteDataset (command, transaction);
}
Else
{
ds = database. ExecuteDataset (storeprocedurename, parameters);
}
if (ds! = null && ds. Tables.count > 0)
{
Return DS. Tables[0];
}
throw new Exception ("no data");
}
Public override IDataReader Getdatareader (string commandtext)
{
if (isintransaction)
{
Command = Datab Ase. Getsqlstringcommand (CommandText);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Return to database. ExecuteReader (command, transaction);
}
Else
{
return database. ExecuteReader (CommandType.Text, CommandText);
}
}
public override int DoCommand (string commandtext)
{
if (isintransaction)
{
Command = database. Getsqlstringcommand (CommandText);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Return to database. ExecuteNonQuery (command, transaction);
}
Else
{
return database. ExecuteNonQuery (CommandType.Text, CommandText);
}
}
public override int DoCommand (string storeprocedurename, params object[] parameters)
{
if (isintr Ansaction)
{
Command = database. Getstoredproccommand (storeprocedurename, parameters);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Return to database. ExecuteNonQuery (command, transaction);
}
Else
{
return database. ExecuteNonQuery (storeprocedurename, parameters);
}
}
Public override Object Getdataresult (string commandtext)
{
if (isintransaction)
{
Command = database.g Etsqlstringcommand (CommandText);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Return to database. ExecuteScalar (command, transaction);
}
Else
{
return database. ExecuteScalar (CommandType.Text, CommandText);
}
}
public override Object Getdataresult (string storeprocedurename, params object[] parameters)
{
If (isintransaction)
{
Command = database. Getstoredproccommand (storeprocedurename, parameters);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
Return to database. ExecuteScalar (command, transaction);
}
Else
{
return database. ExecuteScalar (storeprocedurename, parameters);
}
}
public override DataSet GetDataSet (string storeprocedurename, params object[] parameters)
{
DataSet ds = null;
if (isintransaction)
{
Command = database. Getstoredproccommand (storeprocedurename, parameters);
Command. Connection = Connection;
Command. Transaction = Transaction;
Command.commandtype = CommandType.Text;
ds = database. ExecuteDataset (command, transaction);
}
Else
{
ds = database. ExecuteDataset (storeprocedurename, parameters);
}
if (ds! = null && ds. Tables.count > 0)
{
return DS;
}
throw new Exception ("no data");
}
}
}
Multi-type database common operation-corresponding database operation instance class