asp.net .cs数据库添加语句

本文介绍了一个使用 C# 进行 SQL 数据库操作的类,包括连接数据库、执行查询、获取数据集及事务处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public  class  DB

{

   SqlConnection  con  =  null;

  SqlCommand  cmd  =  new  SqlCommend;

  private   bool   flag ;

   public  DB  ()

   {      //此处添加构造函数逻辑

     con  =  new  SqlConnection ( " Data Source  = .  ;  Initial  Catalog  =  vote ; User  ID = sa ;  pwd = yang  ;  "  )  ;

  } 

    public SqlConnection  getCon ()   //打开链接

   {

          if  ( con.State  ==   ConnectionState.Closed)

          {

                con.Open();

       }

     return   con ;

   }

   public void   clear  ()  //关闭连接

   {

     if  ( con.State  ==  ConnectionState.Open  )

      {

           con.Close ();

     }

    }

public  string  executeGetReturn  (  string  strSql  )

{   //执行查询语句返回结果的第一行第一列
    cmd.Connection  =  getCon  ();

   cmd.CommandText  =  strSql ;

   string  result  =  cmd.ExecteScalar().ToString();

   clear();

   return   result;

  }

public DataSet   getDs ( string  strSql ,  string  tableName )

 {  // 得到一个数据集

    SqlDataAdapt  sda  =  new  SqlDataAdapt  (  strSql ,  getCon  () ) ;

   DataSet ds  =  new  DataSet  () ;

   sda.Fill (  ds  ,  "  tableName " ) ;

  clear () ;

  return  ds;

 }

  public  bool  executeTransation  (  string []  strSql )

   {

        con  =  getCon  () ;

       cmd.Connection  =  con ;

      SqlTranscation  myTranscation  =  con.BeginTransaction () ;

      try

     {

           for ( int  i=0 ; i<strSql.Length ; i++)

           {

                cmd.Transaction  =  myTransaction ;

                cmd.CommandText  =  strSql [ i ];

                cmd.ExecuteNonQuery  () ;

            }

             myTransaction.Commit  () ;

              flag  =  true ;

     }

      catch  ( Exception  )

      {

               myTransaction.Rollback () ;

               flag  =  flase  ;

      }

      return    flag ;

      }

}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值