vb.net写的odbc连接dsn数据源和ole链接oracle的小例子
最近由于工作需要开始接触vb2010,也叫vb.net。相比vb6.0有面向对象编程的优势。同时接触一门新语言,要更快的实际应用起来,链接数据库是必不可少的。之前用vba写过一个售书工具,正好可以拿来改造成vb.net程序。同时考虑到面向对象编程,尽力使用MVC模式编程。其中链接数据库的部分被写在一个模块中,可以切换选择用ole直连oracle,或者用odbc连接dsn数据源。具体如下:
1.dao层新建一个模块
Option Explicit Off
Imports Microsoft.Data.Odbc
Module dao
Public conndsn As OdbcConnection
Public connole As OleDb.OleDbConnection
Public connectionString1 As String
Public connectionString2 As String = "Provider=MSDAORA;Data Source=xx;User ID=xx;Password=xx;"
Sub OdbcConnection()
connectionString1 = "DSN=sht1;UID=sheet;Pwd=sheet;"
Try
conndsn = New OdbcConnection(connectionString1)
conndsn.Open()
MsgBox("数据库连接成功!", MsgBoxStyle.OkOnly, "连接状态")
Catch ex As Exception
MsgBox("数据库连接发生错误!", MsgBoxStyle.OkOnly, "系统错误")
End
End Try
End Sub
Sub OleConnection()
Try
connole = New System.Data.OleDb.OleDbConnection(connectionString2)
connole.Open()
MsgBox("采用ole数据库连接成功!", MsgBoxStyle.OkOnly, "连接状态")
Catch ex As Exception
MsgBox("数据库连接发生错误!", MsgBoxStyle.OkOnly, "系统错误")
End
End Try
End Sub
Sub CloseConndsn()
conndsn.Close()
conndsn.Dispose()
End Sub
Sub CloseConole()
connole.Close()
connole.Dispose()
End Sub
End Module
2.数据库设计,字典表如下:
| TableName | TableId | FieldName | TableId | Type(Oracle) | Primary key |
| 书目表 | bookTab | 书目号 | bookCode | varchar2(20) | Y |
| 书名 | bookName | varchar2(50) | |||
| 定价 | price | number(5,2) | |||
| 折扣 | discount | number(3,2) | |||
| 分类 | classification | varchar2(50) | |||
| 分类号 | classificationCode | varchar2(50) | |||
| 库存数 | inventoryNum | INTEGER |
| TableName | TableId | FieldName | TableId | Type(Oracle) | Primary key |
| 顾客表 | customerTab | 顾客号 | customerCode | varchar2(50) | Y |
| 姓名 | name | varchar2(50) | |||
| 工号 | jobNum | varchar2(50) |
| TableName | TableId | FieldName | TableId | Type(Oracle) | Primary key |
| 购书经历表 | purchaseExperienceTab | 顾客号 | customerCode | varchar2(50) | Y |
| 购书日期 | purchaseDate | DATE | Y | ||
| 书单 | bookList | varchar2(3000) | |||
| 金额 | moneyAmount | number(5,2) |
建表语句:
| DB Create | ||||
| create table bookTab ( | ||||
| bookCode | varchar2(20) | not null, | ||
| bookName | varchar2(50), | |||
| price | number(5,2), | |||
| discount | number(3,2), | |||
| classification | varchar2(50), | |||
| classificationCode | varchar2(50), | |||
| inventoryNum | INTEGER | |||
| ) | ||||
| ; | ||||
| alter table bookTab add(constraint pk_bookCode primary key(bookCode)); | ||||
| create table customerTab ( | |||||
| customerCode | varchar2(50) | not null, | |||
| name | varchar2(50), | ||||
| jobNum | varchar2(50) | ||||
| ) | |||||
| ; | |||||
| alter table customerTab add(constraint pk_customerCode primary key(customerCode)); | |||||
| create table purchaseExperienceTab ( | ||||||
| customerCode | varchar2(50) | not null, | ||||
| purchaseDate | DATE | not null, | ||||
| bookList | varchar2(3000), | |||||
| moneyAmount | number(5,2) | |||||
| ) | ||||||
| ; | ||||||
| alter table purchaseExperienceTab add(constraint pk_CodeDate primary key(customerCode,purchaseDate)); | ||||||
插入数据:
insert into bookTab (bookCode,bookName,price,discount,classification,classificationCode,inventoryNum)values('9787532489510','老象恩仇记',12.8,0.75,'童话系列','1111',100)
insert into bookTab (bookCode,bookName,price,discount,classification,classificationCode,inventoryNum)values('9787532489527','神奇的警犬-沈石溪激情动物小说',13.85,0.8,'童话系列','1111',200)
insert into bookTab (bookCode,bookName,price,discount,classification,classificationCode,inventoryNum)values('9787533266066','小男生杜歌飞',133.85,0.9,'男生系列','2222',50)
insert into bookTab (bookCode,bookName,price,discount,classification,classificationCode,inventoryNum)values('9787533266067','Public変数/定数の宣',133.85,0.9,'男生系列','2222',50)
insert into customerTab (customerCode,name,jobNum)values('1','汪晓阳','wm139a0')
insert into customerTab (customerCode,name,jobNum)values('02','汪雨','wm110')
3.新建一个bookClass实体类:
Public Class bookClass
Private bookCode As String
Private bookName As String
Private price As Single
Private discount As Single
Private classification As String
Private classificationCode As String
Private inventoryNum As Integer
Sub New()
Me.bookCode = bookCode
Me.bookName = bookName
Me.price = price
Me.discount = discount
Me.classification = classification
Me.classificationCode = classificationCode
Me.inventoryNum = inventoryNum
End Sub Function getBook(ByVal bookCode As String) As bookClass
Return selectBook(bookCode)
End Function
Function getBookCode() As String
Return Me.bookCode
End Function
Function getBookName() As String
Return Me.bookName
End Function
Function getPrice() As Single
Return Me.price
End Function
Function getDiscount() As Single
Return Me.discount
End Function
Function getClassification() As String
Return Me.classification
End Function
Function getClassificationCode() As String
Return Me.classificationCode
End Function
Function getInventoryNum() As Integer
Return Me.inventoryNum
End Function Sub setBookCode(ByVal bookCode As String)
Me.bookCode = bookCode
End Sub
Sub setBookName(ByVal bookName As String)
Me.bookName = bookName
End Sub
Sub setPrice(ByVal price As Single)
Me.price = price
End Sub
Sub setDiscount(ByVal discount As Single)
Me.discount = discount
End Sub
Sub setClassification(ByVal classification As String)
Me.classification = classification
End Sub
Sub setClassificationCode(ByVal classificationCode As String)
Me.classificationCode = classificationCode
End Sub
Sub setInventoryNum(ByVal inventoryNum As Integer)
Me.inventoryNum = inventoryNum
End Sub
End Class
新建customerClass实体类:
Public Class customerClass
Private customerCode As String
Private name As String
Private jobNum As String
Sub New()
Me.customerCode = customerCode
Me.name = name
Me.jobNum = jobNum
End Sub
Function getCustomer(ByVal customerCode As String) As customerClass
Return selectCustomer(customerCode)
End Function
Function getCustomerCode() As String
Return Me.customerCode
End Function
Function getName() As String
Return Me.name
End Function
Function getJobNum() As String
Return Me.jobNum
End Function
Sub setCustomerCode(ByVal customerCode As String)
Me.customerCode = customerCode
End Sub
Sub setName(ByVal name As String)
Me.name = name
End Sub
Sub setJobNum(ByVal jobNum As String)
Me.jobNum = jobNum
End Sub End Class
4.表现层上用vb控件画出窗体很方便快速:
其页面代码如下:
Public Class sellBook
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Dim book As bookClass
book = New bookClass
book = book.getBook(TextBox1.Text)
DataGridView1.Rows.Add()
DataGridView1.Item("num", DataGridView1.Rows.Count - ).Value = DataGridView1.Rows.Count -
DataGridView1.Item("bookCode", DataGridView1.Rows.Count - ).Value = TextBox1.Text.Trim
DataGridView1.Item("bookName", DataGridView1.Rows.Count - ).Value = book.getBookName()
DataGridView1.Rows(DataGridView1.Rows.Count - ).Cells().Value = book.getPrice()
DataGridView1.Rows(DataGridView1.Rows.Count - ).Cells().Value = book.getDiscount()
Label2.Text = book.getPrice() * book.getDiscount() + Label2.Text
End If
End Sub
Private Sub sellBook_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call CloseConole()
End Sub
Private Sub sellBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call OleConnection()
Label2.Text =
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
Dim customer As New customerClass
customer = customer.getCustomer(TextBox2.Text.Trim)
Label11.Text = customer.getName
Label12.Text = customer.getJobNum
End If
End Sub
End Class
今天就先写到这里,这个页面已经能跑起来了,页面使用了常用的DataGridview控件,输入顾客编号或者书目条码按回车自动检索,合计金额也是自动根据购书单算出的。程序待继续完善。。。
vb.net写的odbc连接dsn数据源和ole链接oracle的小例子的更多相关文章
- informix 通过ADO或ODBC连接提取数据时出现中文乱码的解决方法
最近在做一个项目,是对INFORMIX数据库的数据进行大数据分析,INFORMIX数据库数据有上亿条,没有linux的Root权限和informix数据的生产权限,只能读取.客户要求结果显示在内网wi ...
- EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...
- VB连接MYSQL数据的方法
原文链接:http://hanbaohong.iteye.com/blog/704800 第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载m ...
- 采用ODAC,ODBC连接Oracle【转】
采用ODAC,ODBC连接Oracle 在没有装oracle的环境中,偶尔会需要访问数据库(Word文档查看数据,开发项目运行等等),简单介绍我用过的方案. 一:ODAC 1.介绍.ODAC,全称:o ...
- MFC通过ODBC连接Mysql程序
分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 MFC通过ODBC连接 ...
- DOMINO的JDBC和ODBC连接方法
利用ODBC实现Domino和关系数据库的互操作 Lotus Domino是当今办公自动化系统的主流开发平台之一,Domino自带一个非关系型数据库–文档型数据库,而目前大部分企业的信息都储存在 ...
- PHP 数据库 ODBC创建 ODBC 连接
PHP 数据库 ODBC ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数 ...
- Apache Doris 通过ODBC连接SQL Server
社区有小伙伴有使用Doris ODBC外表连接SQL Server数据库,使用中遇到不知道驱动怎么安装,苦于我这边也没有SQL Server的环境,正好社区有用户使用了这个数据库,也安装ODBC驱动测 ...
- ODBC连接发生错误:未发现数据源名称并且未指定默认驱动程序
程序在使用ODBC方式连接数据库时发生错误: ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序. 什么原因造成的呢? 本人使用&l ...
随机推荐
- 《深入Java虚拟机学习笔记》- 第12章 整数运算
Java虚拟机提供几种进行整数算术运算的操作码,他们执行基于int和long类型的运算.当byte.short和char类型值参与算术运算时,首先会将它们转换为int类型.这些操作码都不会抛出异常,溢 ...
- linu、C语言、计算机基础教程
Linux操作系统入门教程:http://see.xidian.edu.cn/cpp/linux/ 鸟哥的linux私房菜:http://vbird.dic.ksu.edu.tw/ 计算机操作系统教程 ...
- init进程学习
linux的init进程 一个在线编辑markdown文档的编辑器,是内核启动的第一个进程,init进程有很多重要的任务,它的pit 为1,在linux shell中使用pstree命令可以看到它为其 ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- 【转载】svn代码回滚命令
[说明]转载自 http://www.cnblogs.com/jndream/archive/2012/03/20/2407955.html 取消对代码的修改分为两种情况: 第一种情况:改动没有被 ...
- 使用Genymotion作Android开发模拟器:安装Genymotion、部署Genymotion Vitrue Device、安装Genymotion eclipse插件
偶然听说Genymotion Android模拟器非常强大,到网上了解一番后,决定从AVD又慢又卡中解脱出来,折腾了半天终于部署好了,体验了一下,果然启动快,运行流畅,现在总结一下经验教训,供大家参考 ...
- android模拟器访问localhost或127.0.0.1报错
在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的Web服务,但是如果我们在Android模拟器中也采用同样的地址来访问,Android模拟器将无法正 ...
- Java快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- 转载jquery $(document).ready() 与window.onload的区别
jquery $(document).ready() 与window.onload的区别 投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 时间:2009-12-28我要评论 Jquery中$ ...
- JAVA常用设计模式整理
设计模式:一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开一把 ...