我常用的数据库操作bin

None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Data.SqlClient;
None.gif
using System.Collections;
None.gif
None.gif
namespace Xhduan.DBLayer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for DB.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DB
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public DB()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//缺点:没有错误处理
InBlock.gif
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Global Variables#region Global Variables
InBlock.gif        
public static string ConnectionString=connStr.getConnStr();
InBlock.gif        
public static SqlConnection Connection=new SqlConnection(ConnectionString);
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Global Methods#region Global Methods
InBlock.gif        
public static DataSet Execute(string Sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand Command
=new SqlCommand(Sql,Connection);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            SqlDataAdapter adapter
=new SqlDataAdapter(Command);
InBlock.gif            adapter.Fill(ds);
InBlock.gif            Connection.Close();
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static DataSet Execute(SqlCommand Command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            SqlDataAdapter adapter
=new SqlDataAdapter(Command);
InBlock.gif            adapter.Fill(ds);
InBlock.gif            Connection.Close();
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static SqlDataReader ExecuteReader(string Sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand Command
=new SqlCommand(Sql,Connection);
InBlock.gif            Connection.Open();
InBlock.gif            
return Command.ExecuteReader(CommandBehavior.CloseConnection);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static SqlDataAdapter getAdapter(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand Command
=new SqlCommand(sql,Connection);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            SqlDataAdapter adapter
=new SqlDataAdapter(Command);
InBlock.gif            
return adapter;
ExpandedSubBlockEnd.gif         }

InBlock.gif        
public static SqlDataAdapter getAdapter(SqlCommand Command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            SqlDataAdapter adapter
=new SqlDataAdapter(Command);
InBlock.gif             
InBlock.gif             
return adapter;
InBlock.gif             
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static SqlDataReader ExecuteReader(SqlCommand Command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Command.Connection.Open();
InBlock.gif            
return Command.ExecuteReader(CommandBehavior.CloseConnection);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static object ExecuteScalar(string Sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand Command
=new SqlCommand(Sql,Connection);
InBlock.gif            
object result=null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Connection.Open();
InBlock.gif                result
=Command.ExecuteScalar();
ExpandedSubBlockEnd.gif            }

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

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Connection.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static int ExecuteNonQuery(string Sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand Command
=new SqlCommand(Sql,Connection);
InBlock.gif            
int result=-1;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Connection.Open();
InBlock.gif                result
=Command.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

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

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Connection.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static int ExecuteNonQuery(SqlCommand Command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int result=-1;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Command.Connection.Open();
InBlock.gif                result
=Command.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

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

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Command.Connection.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static DataSet ExecuteStoredProcedure(string SPName,ref ArrayList Parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsResult
=new DataSet();
InBlock.gif            SqlCommand Command
=new SqlCommand(SPName,Connection);
InBlock.gif            Command.CommandType
=CommandType.StoredProcedure;
InBlock.gif            
InBlock.gif            
if(Parameters!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=0;i<Parameters.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Command.Parameters.Add( Parameters[i]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlDataAdapter adapter
=new SqlDataAdapter(Command);
InBlock.gif                adapter.Fill(dsResult);
ExpandedSubBlockEnd.gif            }

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

InBlock.gif            
return dsResult;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static SqlDataReader ExecuteStoredProcedureReader(string SPName,ref ArrayList Parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand cmd
=new SqlCommand();
InBlock.gif            cmd.Connection
=Connection;
InBlock.gif            cmd.CommandText
=SPName;
InBlock.gif            cmd.CommandType
=CommandType.StoredProcedure;
InBlock.gif            
InBlock.gif            
if(Parameters!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=0;i<Parameters.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    cmd.Parameters.Add( Parameters[i]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            Connection.Open();
InBlock.gif            
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static bool ExecuteTransaction(string []Sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList CommandList
=new ArrayList(0);
InBlock.gif            Connection.Open();
InBlock.gif            SqlTransaction Transaction
=Connection.BeginTransaction(IsolationLevel.Serializable,"Transaction");
InBlock.gif            
InBlock.gif            
for(int i=0;i<Sql.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlCommand TransactionCommand
=new SqlCommand(Sql[i],Connection,Transaction);
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    TransactionCommand.ExecuteNonQuery();
InBlock.gif                    CommandList.Add(TransactionCommand);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Connection.Close();
InBlock.gif                    
throw ex;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            Transaction.Commit();
InBlock.gif            Connection.Close();
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/dxxhh/archive/2006/01/12/315683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值