访问数据库的通用类

 

 

using  System;
using  System.Data;
using  System.Data.SqlClient;


namespace  CommonClass
{
    
/// <summary>
    
/// DBClient 的摘要说明。
    
/// </summary>

    public class DBClient
    
{
        
//数据库连接字符串,(开发过程中)到app.config里修改
        private string connectionString;
        
        
public DBClient()
        
{
            
// 读取数据库连接字符串并赋给字符串引用变量connectionString;
            connectionString=System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
        }

        
public SqlConnection createConnection()
        

            
// 返回一个用户连接数据库的一个实例。
            return new SqlConnection(connectionString);

        }

    
     
      
/// <summary>
      
///  
      
/// </summary>
      
/// <param name="selectSQL"></param>
      
/// <param name="tableName"></param>
      
/// <returns></returns>

        
        
public DataTable getSQLTable(string selectSQL,string tableName)
        
{
            DataTable table
=new DataTable(tableName);

              SqlConnection sqlConnection
=this.createConnection();
            
try
            
{
                SqlDataAdapter sqlDataAdapter
=new SqlDataAdapter(selectSQL,sqlConnection);
                sqlDataAdapter.Fill(table);
            }

            
catch(System.Data.SqlClient.SqlException ex)
            

                
throw ex;
            }

            
catch(System.Exception ex)
            

                
throw ex;

            }

            
finally
            

                
if(sqlConnection.State != ConnectionState.Closed)
                
{
                    sqlConnection.Close();
                }

            }

            
return table;

        }
 
    
        
public DataTable getSQLTable(string selectSQL,string tableName,string []sourceColumn ,string []strMapField)
        
{
            
if(sourceColumn.Length!=strMapField.Length)
            

                
throw new Exception("源数据例名与映射例名个数不相等!");
            }

            DataTable table
=new DataTable(tableName);
            
            SqlConnection sqlConnection
=this.createConnection();
            
try
            
{
                
                System.Data.Common.DataTableMapping dtMappings
=new System.Data.Common.DataTableMapping(tableName,tableName);

                
for(int i=0;i<strMapField.Length;i++)
                  
{
                   dtMappings.ColumnMappings.Add(sourceColumn[i],strMapField[i]);
                    
                }


                SqlDataAdapter sqlDataAdapter
=new SqlDataAdapter(selectSQL,sqlConnection);
                sqlDataAdapter.TableMappings.Add(dtMappings);
                sqlDataAdapter.Fill(table);
            }

            
catch(System.Data.SqlClient.SqlException ex)
            

                
throw ex;
            }

            
catch(System.Exception ex)
            

                
throw ex;

            }

            
finally
            

                
if(sqlConnection.State != ConnectionState.Closed)
                
{
                    sqlConnection.Close();
                }

            }

            
return table;
        }

        
public int executeSQL(string cmdText)
        

            
int iValue=-1;
            SqlConnection sqlConnection
=this.createConnection();
                          sqlConnection.Open();
             SqlTransaction sqlTransaction
=sqlConnection.BeginTransaction();
            
try
            
{
                SqlCommand sqlCommand
=new SqlCommand();
                sqlCommand.Connection
=sqlConnection;
                sqlCommand.CommandText
=cmdText;
                sqlCommand.Transaction
=sqlTransaction;
                
                iValue
=sqlCommand.ExecuteNonQuery();
                sqlTransaction.Commit();
            }

            
catch(System.Data.SqlClient.SqlException ex)
            
{
                sqlTransaction.Rollback();
                
throw ex;

            }

            
catch(System.Exception ex2)
            

                
throw ex2;
            }

            
finally
            

                sqlConnection.Close();
            }

            
return iValue;

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值