SqlServerClient

原创 2007年10月11日 14:46:00
using System;
using System.Data;
using System.Data.SqlClient;

namespace UnitLibrary.DAL
...{
    
/**//// <summary>
    
/// 功能:用于执行Sql Server 的相关操作
    
/// </summary>

    public class SqlServerClient
    
...{
        
字段#region 字段
        
/**//// <summary>
        
/// 数据库连接字符串
        
/// </summary>

        private string connString;
        
#endregion


        
公有方法#region 公有方法
        
/**//// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="connString">Sql Server数据库连接字符串</param>

        public SqlServerClient(string connString)
        
...{
            
this.connString = connString;
        }


        
/**//// <summary>
        
/// 执行Sql 语句,无返回植
        
/// </summary>
        
/// <param name="sql">sql 语句</param>

        public void ExecSqlWithOutResult(string sql)
        
...{
            SqlConnection conn 
= new SqlConnection(this.connString);
            SqlCommand cmd 
= new SqlCommand(sql, conn);
            cmd.CommandTimeout 
= 0;
            
try
            
...{
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }

            
catch (Exception ex)
            
...{
                
throw ex;
            }

            
finally
            
...{
                
if (cmd.Connection.State == ConnectionState.Open)
                    cmd.Connection.Close();
            }

        }


        
/**//// <summary>
        
/// 执行n条Sql 语句,无返回植(事务)
        
/// </summary>
        
/// <param name="sqls">sql 语句</param>

        public void ExecSqlWithOutResult(string[] sqls)
        
...{
            SqlCommand[] cmds 
= new SqlCommand[sqls.Length];
            SqlConnection conn 
= new SqlConnection(this.connString);
            conn.Open();
            SqlTransaction tran 
= conn.BeginTransaction();

            
try
            
...{
                
for (int i = 0; i < sqls.Length; i++)
                
...{
                    cmds[i] 
= new SqlCommand(sqls[i], conn);
                    cmds[i].Transaction 
= tran;
                    cmds[i].ExecuteNonQuery();
                }

                tran.Commit();
            }

            
catch (Exception ex)
            
...{
                tran.Rollback();
                
throw ex;
            }

            
finally
            
...{
                
if (conn.State == ConnectionState.Open)
                    conn.Close();
            }

        }


        
/**//// <summary>
        
/// 执行Sql 语句,返回一个结果
        
/// </summary>
        
/// <param name="sql">sql 语句</param>
        
/// <returns>sql 语句执行结果</returns>

        public object ExecSqlWithResult(string sql)
        
...{
            
object obj;
            SqlConnection conn 
= new SqlConnection(this.connString);
            SqlCommand cmd 
= new SqlCommand(sql, conn);
            cmd.CommandTimeout 
= 0;
            
try
            
...{
                cmd.Connection.Open();
                obj 
= cmd.ExecuteScalar();
            }

            
catch (Exception ex)
            
...{
                
throw ex;
            }

            
finally
            
...{
                
if (cmd.Connection.State == ConnectionState.Open)
                    cmd.Connection.Close();
            }


            
return obj;
        }


        
/**//// <summary>
        
/// 执行Sql 语句,返回DataTable
        
/// </summary>
        
/// <param name="sql">sql 语句</param>
        
/// <returns>sql 语句的执行结果</returns>

        public DataTable GetDataTable(string sql)
        
...{
            DataTable dt 
= new DataTable();
            SqlDataAdapter adp 
= new SqlDataAdapter(sql, this.connString);
            adp.SelectCommand.CommandTimeout 
= 0;
            adp.Fill(dt);
            CommonDAL.KillStringInDataTable(dt, 
new string[] ...{"'"});
            
return dt;
        }

        
#endregion

    }

}
 
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SqlServerClient
举报原因:
原因补充:

(最多只允许输入30个字)