紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3600|回复: 5

[VB/VB.NET] 分享几段vb.net操作数据库的代码

  [复制链接]
冰糖 发表于 2012-4-8 15:27:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
早段时间学vb.net操作数据库的时候照书上的例子一个字母一个字母的敲出来的,还发现了书上的几处错误

注释齐全,欢迎收藏
  1. '数据库连接通用模块,代码来自紫外工控网http://bbs.ziwai.net
  2. Imports System.Data.SqlClient
  3. Imports System.Data.OleDb
  4. Imports System.Data.Odbc
  5. Namespace DBConfig


  6.     Public Class DBConnection

  7.         '声明一个受保护的变量存储链接数据库的信息
  8.         Protected connstr As String
  9.         '声明用于数据库连接的保护成员
  10. #If DB_OLEDB Then
  11.         Protected conn As OleDbConnection

  12. #ElseIf DB_ODBC Then
  13.         Protected conn As OdbcConnection

  14. #Else
  15.         Protected conn As SqlConnection

  16. #End If
  17.         Protected Sub Open()
  18.             '判断连接字符串是否为空
  19.             If connstr Is Nothing Or connstr = "" Then
  20.                 MessageBox.Show("请指定连接字符串!")
  21.                 Return

  22.             End If
  23.             '实例化connection类
  24. #If DB_OLEDB Then
  25.                 conn = New OleDbConnection(connstr)
  26. #ElseIf DB_ODBC Then
  27.                 conn = New OdbcConnection(connstr)
  28. #Else
  29.             conn = New SqlConnection(connstr)
  30. #End If

  31.             '打开数据库
  32.             conn.Open()
  33.         End Sub

  34.         Protected Sub Close()
  35.             '关闭连接
  36.             conn.Close()

  37.         End Sub


  38.     End Class



  39. End Namespace
复制代码
 楼主| 冰糖 发表于 2012-4-8 15:27:59 | 显示全部楼层
  1. '数据库语句通用模块,代码来自紫外工控网http://bbs.ziwai.net
  2. Imports System.Data.Odbc
  3. Imports System.Data.OleDb
  4. Imports System.Data.SqlClient
  5. Namespace DBConfig
  6.     Public Class DBCommand

  7.         Inherits DBConnection
  8.         '在构造函数中指定连接信息字符串
  9.         Public Sub New(ByVal str As String)
  10.             connstr = str
  11.         End Sub
  12.         Public Function insert(ByVal strSQL As String) As Integer
  13.             '连接数据库
  14.             Open()
  15.             '创建SQlcommand实例
  16. #If DB_OLEDB Then
  17.             Dim cmd As OleDbCommand = New OleDbCommand(strSQL, conn)
  18. #ElseIf DB_ODBC Then
  19.             dim cmd as odbccommand=new odbccommand(strsql,conn)
  20. #Else
  21.             Dim cmd As SqlCommand = New SqlCommand(strSQL, conn)
  22. #End If
  23.             'count表示受影响的行数,初始化为0
  24.             Dim count As Integer = 0
  25.             '执行sql命令
  26.             count = cmd.ExecuteNonQuery()
  27.             '关闭数据库
  28.             Close()
  29.             Return count

  30.         End Function

  31.         Public Function Delete(ByVal table As String, ByVal row As String, ByVal value As String) As Integer
  32.             '连接数据库
  33.             Open()
  34.             '创建sql指令
  35.             Dim strSQL As String = "delect from " + table + " where " + row + "=" + value
  36. #If DB_OLEDB Then
  37.             dim cmd as oledbcommand=new oledbcommand(strsql,conn)
  38. #ElseIf DB_ODBC Then
  39.             dim cmd as odbccommand =new odbccommand(strsql,conn)
  40. #Else
  41.             Dim cmd As SqlCommand = New SqlCommand(strSQL, conn)

  42. #End If
  43.             'count表示受影响的行数,初始化为0
  44.             Dim count As Integer = 0
  45.             '执行sql命令
  46.             count = cmd.ExecuteNonQuery()
  47.             '关闭数据库
  48.             Close()
  49.             Return count



  50.         End Function

  51.         Public Function Update(ByVal table As String, ByVal strContent As String, ByVal row As String, ByVal value As String) As Integer
  52.             '连接数据库
  53.             Open()
  54.             '创建sql指令
  55.             Dim strSQL As String = "Update " + table + " set " + strContent + " where " + row + "=" + value
  56. #If db_oledb Then
  57.             dim cmd as oledbcommand=new oledbcommand(strsql,conn)
  58. #ElseIf db_odbc Then
  59.             dim cmd as odbccommand =new odbccommand(strsql,conn)
  60. #Else
  61.             Dim cmd As SqlCommand = New SqlCommand(strSQL, conn)

  62. #End If
  63.             'count表示受影响的行数,初始化为0
  64.             Dim count As Integer = 0
  65.             '执行sql命令
  66.             count = cmd.ExecuteNonQuery()
  67.             '关闭数据库
  68.             Close()
  69.             Return count
  70.         End Function

  71.     End Class
  72. End Namespace
复制代码
 楼主| 冰糖 发表于 2012-4-8 15:28:22 | 显示全部楼层
  1. '数据库读取通用模块,代码来自紫外工控网http://bbs.ziwai.net
  2. Imports System.Data.SqlClient
  3. Imports System.Data.OleDb
  4. Imports System.Data.Odbc
  5. Namespace DBConfig



  6.     Public Class DBDataReader
  7. #Const DB_OLEDB = 1

  8.         Inherits DBConnection
  9.         Public Sub New(ByVal str As String)
  10.             connstr = str
  11.         End Sub
  12. #If DB_OLEDB Then
  13.         Public Function createdatareader(ByVal strsql As String) As OleDbDataReader
  14.             Open()
  15.             Dim cmd As OleDbCommand = New OleDbCommand(strsql, conn)
  16.             Dim dr As OleDbDataReader = cmd.ExecuteReader()
  17.             Return dr

  18.         End Function
  19. #ElseIf DB_ODBC Then
  20.         Public Function createdatareader(ByVal strsql As String) As OdbcDataReader
  21.             Open()
  22.             Dim cmd As OdbcCommand = New OdbcCommand(strsql, conn)
  23.             Dim dr As OdbcDataAdapter = cmd.ExecuteReader()
  24.             Return dr

  25.         End Function
  26. #Else
  27.         Public Function createdatareader(ByVal strsql As String) As SqlDataReader
  28.             Open()
  29.             Dim cmd As SqlCommand = New SqlCommand(strsql, conn)
  30.             Dim dr As SqlDataReader = cmd.ExecuteReader()
  31.             Return dr

  32.         End Function



  33. #End If


  34.     End Class
  35. End Namespace
复制代码
 楼主| 冰糖 发表于 2012-4-8 15:29:00 | 显示全部楼层
  1. '数据库连接通用模块,代码来自紫外工控网http://bbs.ziwai.net
  2. Imports System.Data
  3. Imports System.Data.Odbc
  4. Imports System.Data.OleDb
  5. Imports System.Data.SqlClient
  6. Namespace DBConfig
  7.     Public Class DBDataTable
  8.         Inherits DBConnection
  9.         '在构造函数中连接指定字符串
  10.         Public Sub New(ByVal str As String)
  11.             connstr = str
  12.         End Sub
  13.         Public Function CreateDatatable(ByVal strsql As String) As DataTable
  14.             Open()
  15.             '使用连接字符串和sqlconnection创建sqldataadapter的实例
  16. #If db_oledb Then
  17.                 Dim da As OleDbDataAdapter = New OleDbDataAdapter(strsql, conn)
  18. #ElseIf db_odbc Then
  19.                 Dim da As OdbcDataAdapter = New OdbcDataAdapter(strsql, conn)
  20. #Else
  21.                 Dim da As SqlDataAdapter = New SqlDataAdapter(strsql, conn)
  22. #End If
  23.             '创建dataset对象
  24.             Dim ds As New Data.DataSet()
  25.             '填充dataset
  26.             da.Fill(ds)
  27.             Close()

  28.             Return ds.Tables(0)

  29.         End Function


  30.     End Class
  31. End Namespace
复制代码
chinazhu666666 发表于 2012-4-8 15:37:26 | 显示全部楼层
{:soso_e128:}怎么给帖子加分啊。找半天没找到哦。冰糖哥{:soso_e144:}
 楼主| 冰糖 发表于 2012-4-8 15:45:17 | 显示全部楼层
右下角有个评分哎
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-4-25 15:51 , Processed in 0.640633 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表