Home > Database > Mysql Tutorial > body text

四大天王之变形金刚(SqlHelper)

WBOY
Release: 2016-06-07 15:58:34
Original
1599 people have browsed it

/pre 1、基础介绍p/ppspan style=color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28pxspan style=font-size:18px SqlHelper是一个基于NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简

 

Copy after login
1、基础介绍

SqlHelper是一个基于·NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。

简单的讲,就是将“四大天王”中具有共性的东西,封装起来,从而让我们的代码变得简单,易于扩展。

2、实现方法(Vb.Net版)

(1)配制文件(U层)

在建立的应用窗体项目下,有一个App.Config文件,打开,然后在后边添加如下代码

  
  <appSettings>
    <clear/>
    <add key="ConnStr" value="server=localhost;Database=Login;User=sa;Password=123456;"/>   
  </appSettings>
Copy after login

添加完以后,如下图



    
        
    
  
  <appSettings>
    <clear/>
    <add key="ConnStr" value="server=localhost;Database=Login;User=sa;Password=123456;"/>   
  </appSettings>
  
Copy after login

其他方法另外参照 VB.Net—配置文件

(2)实现SqlHelper类(D层)

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration          &#39;添加必要引用

Public Class SqlHelper

    &#39;定义变量
    &#39;获得数据库链接字符串
    Private ReadOnly strConnection As String = ("ConnStr")
    &#39;设置链接
    Dim conn As SqlConnection = New SqlConnection(strConnection)
    &#39;定义Command命令
    Dim cmd As New SqlCommand

    &#39; <summary>    
    &#39; 执行增删改三个操作,(有参)返回值为Boolean类型,确认是否执行成功    
    &#39; </summary>    
    &#39; <param name="cmdText">需要执行语句,一般是Sql语句,也有存储过程</param>    
    &#39; <param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    &#39; <param name="paras">参数数组,无法确认有多少参数</param>    
    &#39; <returns></returns>    
    &#39; <remarks></remarks>
    Public Function ExecAddDelUpdate(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As Integer

        &#39;将传入的值,分别为cmd的属性赋值
        cmd.Parameters.AddRange(paras)        &#39;将参数传入
        cmd.CommandType = cmdType             &#39;设置一个值,解释cmdText
        cmd.Connection = conn                 &#39;设置连接,全局变量
        cmd.CommandText = cmdText             &#39;设置SQL语句

        Try
            conn.Open()                       &#39;打开链接
            Return cmd.ExecuteNonQuery()      &#39;执行增删改操作
            cmd.Parameters.Clear()            &#39;清除参数
        Catch ex As Exception
            Return 0                          &#39;如果出错就返回0
        Finally
            Call CloseConn(conn)
            Call CloseCmd(cmd)
        End Try
    End Function

    &#39; <summary>    
    &#39; 执行增删改三个操作,(无参)    
    &#39; </summary>    
    &#39; <param name="cmdText">需要执行语句,一般是Sql语句,也有存储过程</param>    
    &#39; <param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    &#39; <returns>Interger,受影响的行数</returns>    
    &#39; <remarks>2013年2月2日8:19:59</remarks>   

    Public Function ExecAddDelUpdateNo(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer

        &#39;为要执行的命令cmd赋值
        cmd.CommandText = cmdText               &#39;传入查询语句
        cmd.CommandType = cmdType               &#39;设置Sql语句如何解释
        cmd.Connection = conn                   &#39;设置连接

        &#39;执行操作
        Try
            conn.Open()
            Return cmd.ExecuteNonQuery
        Catch ex As Exception
            Return 0
        Finally
            Call CloseConn(conn)
            Call CloseCmd(cmd)
        End Try

    End Function
    &#39; <summary>    
    &#39; 执行查询的操作,(有参),参数不限    
    &#39; </summary>    
    &#39; <param name="cmdText">需要执行语句,一般是Sql语句,也有存储过程</param>    
    &#39; <param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    &#39; <param name="paras">传入的参数</param>    
    &#39; <returns></returns>    
    &#39; <remarks></remarks> 

    Public Function ExecSeletct(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable

        Dim sqlAdpter As SqlDataAdapter            &#39;是 DataSet和 SQL Server之间的桥接器,用于检索和保存数据
        Dim dt As New DataTable
        Dim ds As New DataSet

        &#39;还是给cmd赋值
        cmd.CommandText = cmdText                  &#39;设置查询语句
        cmd.CommandType = cmdType                  &#39;设置Cmd对象的类型
        cmd.Connection = conn                      &#39;数据库连接语句
        cmd.Parameters.AddRange(paras)             &#39;传入参数
        sqlAdpter = New SqlDataAdapter(cmd)        &#39;实例化适配器

        Try
            sqlAdpter.Fill(ds)                     &#39;用adapter将dataSet填充
            dt = ds.Tables(0)                      &#39;dataTable是dataSet的第一个表
            cmd.Parameters.Clear()                 &#39;清除参数
        Catch ex As Exception
            MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告!")
        Finally
            Call CloseCmd(cmd)                     &#39;最后一定要释放cmd
        End Try

        Return dt      &#39;返回查到的数据

    End Function

    &#39; <summary>    
    &#39; 执行查询的操作,(无参)    
    &#39; </summary>    
    &#39; <param name="cmdText">需要执行语句,一般是Sql语句,也有存储过程</param>    
    &#39; <param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    &#39; <returns>dataTable,查询到的表格</returns>    
    &#39; <remarks></remarks>   

    Public Function ExecSelectNo(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable

        Dim sqlAdapter As SqlDataAdapter
        Dim ds As New DataSet

        &#39;给cmd赋值
        cmd.CommandText = cmdText
        cmd.CommandType = cmdType
        cmd.Connection = conn

        sqlAdapter = New SqlDataAdapter(cmd)         &#39;实例化适配器

        Try
            sqlAdapter.Fill(ds)                      &#39;用adapter将dataSet填充
            Return ds.Tables(0)                      &#39;返回dataSet的第一个表
        Catch ex As Exception
            Return Nothing
        Finally
            Call CloseCmd(cmd)                       &#39;关闭cmd
        End Try

    End Function

    &#39; <summary>    
    &#39; 关闭连接    
    &#39; </summary>    
    &#39; <param name="conn">需要关闭的连接</param>    
    &#39; <remarks></remarks>   

    Public Sub CloseConn(ByVal conn As SqlConnection)

        If (conn.State <> ConnectionState.Closed) Then  &#39;如果没有关闭
            conn.Close()                                &#39;关闭连接
            conn = Nothing                              &#39;不指向原对象
        End If

    End Sub

    &#39; <summary>    
    &#39; 关闭命令    
    &#39; </summary>    
    &#39; <param name="cmd">需要关闭的命令</param>    
    &#39; <remarks></remarks>    

    Public Sub CloseCmd(ByVal cmd As SqlCommand)

        If Not IsNothing(cmd) Then                      &#39;如果cmd命令还存在
            cmd.Dispose()                               &#39;释放资源
            cmd = Nothing                               &#39;不指向原对象
        End If

    End Sub

End Class
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template