原创- -数据层

1总合了n种数据访问层……——……不是很成熟
2. 由于鄙人的懒惰缺少了-传递参数(这部分设计完了但是~~懒得写分析原谅哈),这部分主要采用缓存参数的设计.
3.缺少DAL.SqlClient.SqlParametersAll.SqlText枚举的定义他用来封装所有的存储过程的名称
4.将要呈现给大家的是一部分中的部分哈~~原因鄙人懒惰~~造成的~~检讨中

        public ExecState Exec(DAL.SqlClient.SqlParametersAll.SqlText commandText)
        
{

            ExecState ren 
= new ExecState();
            System.Data.SqlClient.SqlConnection sqlCon
=new System.Data.SqlClient.SqlConnection(this.ConnectionString);
            
            System.Data.SqlClient.SqlCommand sqlCommand 
= new System.Data.SqlClient.SqlCommand(
            Enum.GetName(
typeof(DAL.SqlClient.SqlParametersAll.SqlText), commandText), sqlCon);

            sqlCommand.CommandType 
= CommandType.StoredProcedure;
            
            
try
            
{
                sqlCon.Open();
                sqlCommand.Transaction 
= sqlCon.BeginTransaction();

                
//---------------------记录影响的行数
                ren.State.Add("NonQuery", sqlCommand.ExecuteNonQuery());

                
//---寻找输出参数
                this.SelectOuputParameters(ren, sqlCommand.Parameters);



                
//--成功提交
                sqlCommand.Transaction.Commit();
            }

            
catch (System.Data.SqlClient.SqlException exp)
            
{
                
//--失败了回滚
                sqlCommand.Transaction.Rollback();
            }

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

            }


            
return ren;
        }


        
//--寻找输出参数
        
        
/// <summary>
        
/// 寻找输出参数,不用返回数据因为是按引用传递的
        
/// </summary>
        
/// <param name="ren"></param>
        
/// <param name="SelectPr"></param>

        private void SelectOuputParameters(ExecState ren, System.Data.SqlClient.SqlParameterCollection SelectPr)
        
{
            
foreach (System.Data.SqlClient.SqlParameter OnLyPr in SelectPr)
            
{
                
if (OnLyPr.Direction ==ParameterDirection.InputOutput || OnLyPr.Direction == ParameterDirection.Output)
                
{
                    ren.State.Add(OnLyPr.ParameterName, OnLyPr.Value);
                }

            }


        }



    }

    
//------------执行后的状态
    public class ExecState
    
{
        
//-----------------------------------------存储对应的数据存储在那个组中
        public System.Collections.Generic.Dictionary<stringobject> State = new  System.Collections.Generic.Dictionary<string,object>();
   
    }

}


posted @ 2007-04-24 22:48  苹果王子  阅读(754)  评论(0编辑  收藏  举报