封装DB操作(七)--测试代码。

类别:.NET开发 点击:0 评论:0 推荐:

public class Test

{

 private void TestGetRec()
  {
   //------取数据-----------
   DataSet ds = DB.getRec("select * from customer");
   this.dataGrid1.DataSource = ds.Tables[0];

  }

  private void TestExeStore()
  {
   //---------执行存储过程范例---------
   SqlParameter[] para = new SqlParameter[] {
             

     new SqlParameter("@x2",SqlDbType.VarChar,255),
             

     new SqlParameter("@x1",SqlDbType.Int,8),
             

     new SqlParameter("@x3",SqlDbType.VarChar,255)
             

    };

   para[0].Direction = ParameterDirection.Output;

   para[1].Direction = ParameterDirection.ReturnValue;

   para[2].Direction = ParameterDirection.Input;
   para[2].Value = "pppppppppppppppppppppppp";

   //返回Command。
   SqlCommand dbc= (SqlCommand)DB.ExecuteByStore("selectdata",para);

   //返回值
   object o = dbc.Parameters["@x1"].Value;
   object oo = dbc.Parameters["@x3"].Value;

  }

  private void TestExecuteWithTran()
  {

   //--------含有事务管理的执行更新语句-----------
   DB.ExecuteWithTran("update customer set custname='浙江' where id=1 ");
  }

  private void TestMulti()
  {
   string SQL_INSERT_HMD = "INSERT INTO SJ_HMD(UNAME,SJHM,SM)

VALUES(:uname,:sjhm,:sm)"; 
      string SQL_DELETE_HMD = "DELETE FROM SJ_HMD WHERE UNAME=:uname";

   string PARAM_UNAME = "UNAME";
   string PARAM_SJHM = "SJHM";
   string PARAM_SM = "SM";
    
  
   //---first parameter-----------
   SqlParameter[] para = new SqlParameter[] {
             

     new SqlParameter(PARAM_UNAME,SqlDbType.VarChar,50),
             

     new SqlParameter(PARAM_SJHM,SqlDbType.VarChar,50),
             

     new SqlParameter(PARAM_SM,SqlDbType.VarChar,255)
             

    };
   para[0].Value ="sss";
   para[1].Value = "dddd";
   para[2].Value = "uuuu";
   //-------------------------------

   

   //-----second parameter-----------
   SqlParameter[] para2 = new SqlParameter[]{ new

SqlParameter(PARAM_UNAME,SqlDbType.VarChar,50)};
   para2[0].Value = "7";

   //-------------------------------
   
   try
   {
    //-----在一个事务上执行多个语句-------
    DB.BeginTran();
    DB.ExeMultiPara(SQL_INSERT_HMD,para);
    DB.ExeMultiPara(SQL_DELETE_HMD,para2);
    DB.CommitTran();
   }
   catch(Exception ex)
   {
    DB.RollBackTran();  //回滚
    throw new Exception(ex.ToString());
   }
  }

  private void TestExecuteSql()
  {
   //------无事务的更新语句----------
   DB.ExecuteSql("update customer set custname='浙江' where id=2");
  }

  private void  TestExeWithPara()
  {

   //---------执行带参数的含事务插入语句---------
   SqlParameter[] para = new SqlParameter[]{ new

SqlParameter("param1",SqlDbType.VarChar)};
   para[0].Value = "testdata";
   DB.ExecuteWithParam("insert into customer values(@param1)",para);
   

  }

  private void  TestExeMulti()
  {
   try
   {
    //-----在一个事务上执行多个语句-------
    DB.BeginTran();
    DB.Execute("inser into customer values('testdata')");
    DB.Execute("update customer set custname='updateData' where

id=1");
    DB.CommitTran();
   }
   catch(Exception ex)
   {
    DB.RollBackTran();
    throw new Exception(ex.ToString());
   }
  }

  private void TestReturnReader()
  {

   //----------返回DataReader---------
   SqlDataReader rdr = (SqlDataReader)DB.getDataByReader("select * from

customer");
   while(rdr.Read())
   {
    
   }  
  }

  private void TestGetDataByPara()
  {
   //----------含有参数的select --------------------
   SqlParameter[] para = new SqlParameter[]{ new

SqlParameter("@id",SqlDbType.Int,8)};
   para[0].Value = 9;
   DataSet ds = DB.getDataWithParam("select * from customer where id =

@id",para);
   this.dataGrid1.DataSource = ds.Tables[0];
  }

  private void  TestGetRecByStore()
  {
   //--------执行无参数的存储过程---------
   DataSet ds = DB.getRecByStore("selectCustomer","customer",null); 

   this.dataGrid1.DataSource = ds.Tables[0];
   
   
           //----------执行有参数的存储过程-------------
   SqlParameter[] para = new SqlParameter[]{ new

SqlParameter("@id",SqlDbType.Int,8)};
   para[0].Value = 9;
   DataSet ds2  = DB.getRecByStore("selectCustomer","customer",para);
   this.dataGrid1.DataSource = ds2.Tables[0];

  }

  private void TestGetDataReader()
  {
   //----------执行无参数的过程,返回DataReader---------
   SqlDataReader rdr =

(SqlDataReader)DB.getReaderByStore("selectCustomer",null);

   while(rdr.Read())
   {
      //processing code 
   } 
 
   //----------执行有参数的过程,返回DataReader---------
   SqlParameter[] para = new SqlParameter[]{ new

SqlParameter("@id",SqlDbType.Int,8)};
   para[0].Value = 9;
   SqlDataReader rd =

(SqlDataReader)DB.getReaderByStore("selectCustomer",para);

   while(rdr.Read())
   {
    //processing code 
   }
 }

}

本文地址:http://com.8s8s.com/it/it41651.htm