一个操作firebird的helper类

荒凉一梦 提交于 2020-04-03 07:38:10
参照了pet shop以及DAAB,

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using FirebirdSql.Data.FirebirdClient;

namespace ADOHelper
{
    
public class Firebird
    
{
        
private string connStrTemplate = "Server={0};User={1};Password={2};Charser={3};Database={4}";
        
private string connStr;

        
protected char ParameterToken
        
{
            
get return '@'; }
        }


        
//连接字符串
        public string ConnectString
        
{
            
set { connStr = value; }
        }


        
//构造函数
        public Firebird()
        
{

        }


        
//构造函数2
        public Firebird(string host, string user, string password, string charset, string database)
        
{
            connStr 
= String.Format(connStrTemplate, host, user, password, charset, database);
        }


        
/// <summary>
        
/// Open FbConnection
        
/// </summary>
        
/// <remarks>
        
/// e.g.:  
        
///  FbConnection conn = OpenConnection();
        
/// </remarks>
        
/// <returns>an FbConnection</returns>

        public FbConnection OpenConnection()
        
{
            FbConnection connection 
= null;
            
try
            
{
                
try
                
{
                    connection 
= new FbConnection(connStr);
                    connection.Open();
                }

                
catch (Exception e)
                
{
                    
throw e;
                }

            }

            
catch
            
{
                
if (connection != null)
                    connection.Close();
            }

            
return connection;
        }


        
/// <summary>
        
/// Execute a FbCommand (that returns no resultset) using an existing SQL Transaction 
        
/// using the provided parameters.
        
/// </summary>
        
/// <remarks>
        
/// e.g.:  
        
///  int result = ExecuteNonQuery( CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
        
/// </remarks>
        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
        
/// <param name="commandText">the stored procedure name or T-SQL command</param>
        
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
        
/// <returns>an int representing the number of rows affected by the command</returns>

        public int ExecuteNonQuery(CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
        
{
            FbCommand cmd 
= new FbCommand();
            
using (FbConnection conn = OpenConnection())
            
{

                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
                
int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                
return val;
            }

        }



        
/// <summary>
        
/// Execute a FbCommand (that returns no resultset) using an existing SQL Transaction 
        
/// using the provided parameters.
        
/// </summary>
        
/// <remarks>
        
/// e.g.:  
        
///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
        
/// </remarks>
        
/// <param name="trans">an existing sql transaction</param>
        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
        
/// <param name="commandText">the stored procedure name or T-SQL command</param>
        
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
        
/// <returns>an int representing the number of rows affected by the command</returns>

        public int ExecuteNonQuery(FbTransaction trans, CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
        
{
            FbCommand cmd 
= new FbCommand();
            
using (FbConnection conn = OpenConnection())
            
{

                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
                
int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                
return val;
            }

        }


        
/// <summary>
        
/// Execute a FbCommand that returns a resultset against the database specified in the connection string 
        
/// using the provided parameters.
        
/// </summary>
        
/// <remarks>
        
/// e.g.:  
        
///  SqlDataReader r = ExecuteReader( CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
        
/// </remarks>
        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
        
/// <param name="commandText">the stored procedure name or T-SQL command</param>
        
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
        
/// <returns>A SqlDataReader containing the results</returns>

        public FbDataReader ExecuteReader(CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
        
{
            FbCommand cmd 
= new FbCommand();
            FbConnection conn 
= OpenConnection();

            
// we use a try/catch here because if the method throws an exception we want to 
            
// close the connection throw code, because no datareader will exist, hence the 
            
// commandBehaviour.CloseConnection will not work
            try
            
{
                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
                FbDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                
return rdr;
            }

            
catch
            
{
                conn.Close();
                
throw;
            }

        }



        
/// <summary>
        
/// Execute a FbCommand that returns the first column of the first record against an existing database connection 
        
/// using the provided parameters.
        
/// </summary>
        
/// <remarks>
        
/// e.g.:  
        
///  Object obj = ExecuteScalar(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
        
/// </remarks>
        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
        
/// <param name="commandText">the stored procedure name or T-SQL command</param>
        
/// <param name="commandParameters">an array of FbParameter used to execute the command</param>
        
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>

        public object ExecuteScalar(CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
        
{
            FbCommand cmd 
= new FbCommand();
            
using (FbConnection conn = OpenConnection())
            
{
                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
                
object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                
return val;
            }

        }


        
/// <summary>
        
/// Return a DataSet
        
/// </summary>
        
/// <param name="cmd">FbCommand object</param>
        
/// <param name="conn">FbConnection object</param>
        
/// <param name="trans">FbTransaction object</param>
        
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
        
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
        
/// <param name="cmdParms">FbParameter to use in the command</param>

        public void DoLoadDataSet(DataSet dataSet, string[] tableNames, CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
        
{
            
if (tableNames == nullthrow new ArgumentNullException("tableNames");
            
if (tableNames.Length == 0)
            
{
                
throw new ArgumentNullException("tableNames");
            }

            
for (int i = 0; i < tableNames.Length; i++)
            
{
                
if (string.IsNullOrEmpty(tableNames[i])) throw new ArgumentException(string.Concat("tableNames[", i, "]"));
            }


            
using (FbDataAdapter adapter = GetDataAdapter(UpdateBehavior.Standard))
            
{
                ((IDbDataAdapter)adapter).SelectCommand 
= command;

                
try
                
{
                    DateTime startTime 
= DateTime.Now;
                    
string systemCreatedTableNameRoot = "Table";
                    
for (int i = 0; i < tableNames.Length; i++)
                    
{
                        
string systemCreatedTableName = (i == 0)
                             
? systemCreatedTableNameRoot
                             : systemCreatedTableNameRoot 
+ i;

                        adapter.TableMappings.Add(systemCreatedTableName, tableNames[i]);
                    }


                    adapter.Fill(dataSet);
                    instrumentationProvider.FireCommandExecutedEvent(startTime);
                }

                
catch (Exception e)
                
{
                    instrumentationProvider.FireCommandFailedEvent(command.CommandText, ConnectionStringNoCredentials, e);
                    
throw;
                }

            }


        }


        
/// <summary>
        
/// Prepare a command for execution
        
/// </summary>
        
/// <param name="cmd">FbCommand object</param>
        
/// <param name="conn">FbConnection object</param>
        
/// <param name="trans">FbTransaction object</param>
        
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
        
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
        
/// <param name="cmdParms">FbParameter to use in the command</param>

        private void PrepareCommand(FbCommand cmd, FbConnection conn, FbTransaction trans, CommandType cmdType, string cmdText, FbParameter[] cmdParms)
        
{

            
if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection 
= conn;
            cmd.CommandText 
= cmdText;

            
if (trans != null)
                cmd.Transaction 
= trans;

            cmd.CommandType 
= cmdType;

            
if (cmdParms != null)
            
{
                
foreach (FbParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }

        }






    }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!