一个操作firebird的helper类

参照了pet shop以及DAAB,

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  FirebirdSql.Data.FirebirdClient;
None.gif
None.gif
namespace  ADOHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Firebird
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string connStrTemplate = "Server={0};User={1};Password={2};Charser={3};Database={4}";
InBlock.gif        
private string connStr;
InBlock.gif
InBlock.gif        
protected char ParameterToken
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn '@'; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//连接字符串
InBlock.gif
        public string ConnectString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ connStr = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//构造函数
InBlock.gif
        public Firebird()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Open FbConnection
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  FbConnection conn = OpenConnection();
InBlock.gif        
/// </remarks>
ExpandedSubBlockEnd.gif        
/// <returns>an FbConnection</returns>

InBlock.gif        public FbConnection OpenConnection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FbConnection connection 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    connection 
= new FbConnection(connStr);
InBlock.gif                    connection.Open();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw e;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (connection != null)
InBlock.gif                    connection.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return connection;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public int ExecuteNonQuery(CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FbCommand cmd 
= new FbCommand();
InBlock.gif            
using (FbConnection conn = OpenConnection())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
InBlock.gif                
int val = cmd.ExecuteNonQuery();
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return val;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public int ExecuteNonQuery(FbTransaction trans, CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FbCommand cmd 
= new FbCommand();
InBlock.gif            
using (FbConnection conn = OpenConnection())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
InBlock.gif                
int val = cmd.ExecuteNonQuery();
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return val;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public FbDataReader ExecuteReader(CommandType cmdType, string cmdText, params FbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FbCommand cmd 
= new FbCommand();
InBlock.gif            FbConnection conn 
= OpenConnection();
InBlock.gif
InBlock.gif            
// we use a try/catch here because if the method throws an exception we want to 
InBlock.gif            
// close the connection throw code, because no datareader will exist, hence the 
InBlock.gif            
// commandBehaviour.CloseConnection will not work
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
InBlock.gif                FbDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return rdr;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Close();
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

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

ExpandedSubBlockEnd.gif        }

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

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

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

InBlock.gif
InBlock.gif            
using (FbDataAdapter adapter = GetDataAdapter(UpdateBehavior.Standard))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ((IDbDataAdapter)adapter).SelectCommand 
= command;
InBlock.gif
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DateTime startTime 
= DateTime.Now;
InBlock.gif                    
string systemCreatedTableNameRoot = "Table";
InBlock.gif                    
for (int i = 0; i < tableNames.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string systemCreatedTableName = (i == 0)
InBlock.gif                             
? systemCreatedTableNameRoot
InBlock.gif                             : systemCreatedTableNameRoot 
+ i;
InBlock.gif
InBlock.gif                        adapter.TableMappings.Add(systemCreatedTableName, tableNames[i]);
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    adapter.Fill(dataSet);
InBlock.gif                    instrumentationProvider.FireCommandExecutedEvent(startTime);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    instrumentationProvider.FireCommandFailedEvent(command.CommandText, ConnectionStringNoCredentials, e);
InBlock.gif                    
throw;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        private void PrepareCommand(FbCommand cmd, FbConnection conn, FbTransaction trans, CommandType cmdType, string cmdText, FbParameter[] cmdParms)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
if (conn.State != ConnectionState.Open)
InBlock.gif                conn.Open();
InBlock.gif
InBlock.gif            cmd.Connection 
= conn;
InBlock.gif            cmd.CommandText 
= cmdText;
InBlock.gif
InBlock.gif            
if (trans != null)
InBlock.gif                cmd.Transaction 
= trans;
InBlock.gif
InBlock.gif            cmd.CommandType 
= cmdType;
InBlock.gif
InBlock.gif            
if (cmdParms != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (FbParameter parm in cmdParms)
InBlock.gif                    cmd.Parameters.Add(parm);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/xinbin/archive/2006/09/10/500512.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值