转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Visual Basic数据库开发疑难问题解         ★★★★

Visual Basic数据库开发疑难问题解

作者:闵涛 文章来源:闵涛的学习笔记 点击数:878 更新时间:2009/4/23 15:02:10
  [前言:]我始终认为Visual Basic是数据库开发最便捷的工具,我在论坛中看到不少朋友提出的关于VB数据库开发方面的问题,有许多问题是反复提出,也有些我认为是比较典型的问题,我将这些问题收录在一起同时提出答案,便于网友们查阅,如果你还有社呢们提可以给我写信。

  问:如何显示格式为03-3-13的日期?

  解决的方法:

1 
Cmd.CommandText = "select * from 支出 where 日期=03-3-13" 中 03-3-13=-13。
日期实际上是Double型数字。0 是 1899-12-30,-13 是 1899-12-17。你当然没有这样日期的记录,所以只有大于才行。
2
Cmd.CommandText = "select * from 支出 where 日期=#03-3-13#"
凡是没有明示,文字型日期是按美国习惯解释的,#03-3-13# 是 0013-03-03。

或者使用长日期格式:
Cmd.CommandText = "select * from 支出 where 日期=#2003-3-13#"
用格式化函数:
Cmd.CommandText = "select * from 支出 where 日期=#" & format(mydate,"yyyy-mm-dd") & "#"

  问: 如何判断DNS是否存在?怎样才能列举出所有的DNS?

  解决方法:

  1、通过利用ODBC API中的SQLDataSource函数可以取得ODBC API中数据源的列表。 判断DNS是否存在:
 
  2、使用API函数Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax As Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal cbDescriptionMax As Integer, pcbDescription As Integer) As Integer Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (ByRef env As Long) As Long 列举出所有DNS。
  问:处理文本文件是导入数据库还是直接读写文件呢?

  解决方法:

Set main = bumony.OpenRecordset("main")
Open App.Path & "\sources\" & Text1.Text & "\′úàíòμ??" & Text1.Text & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, str1
With main
.AddNew
!code = Mid(str1, 1, 5)
!date = Text1.Text
If Mid(str1, 1, 5) = "21310" Or Mid(str1, 1, 5) = "21311" Or Mid(str1, 1, 5) = "21410" Or Mid(str1, 1, 5) = "21411" Then
!Money = Trim(Mid(str1, 7, 10))
Else
!Money = Trim(Mid(str1, 7, 10)) & "0000"
End If
!whao = "1102"
!ywhao = "1102"
.Update
End With
Loop
Close #1
main.Close
  问:调用SQL存储后有参数返回,应该怎么赋值?

  解决方法:


Dim ADOCmd As New ADODB.Command
Dim ADOPrm As New ADODB.Parameter
Dim ADORs As ADODB.Recordset

'....
Set ADOCmd.ActiveConnection = ADOCon
With ADOCmd
.CommandType = adCmdStoredProc
.CommandText = "ADOTestRPE"
End With

sParmName = "Output"
Set ADOPrm = ADOCmd.CreateParameter(sParmName, adInteger, adParamOutput)
ADOCmd.Parameters.Append ADOPrm
ADOCmd.Parameters(sParmName).Value = 999

Set ADORs = ADOCmd.Execute
'.....

Debug.Print "Output: " & ADOCmd.Parameters("Output").Value

  问: SQL Server 2000中如何存取图片信息?
  
  解决方法:


新建一个工程,添加 ado 控件,2个 Command ,1个 Picture,1个 Image

Dim Chunk() As Byte
Dim lngLengh As Long
Dim intChunks As Integer
Dim intFragment As Integer
Const ChunkSize = 1000
Const lngDataFile = 1

Private Sub cmdBrowse_Click()

On Error Resume Next
With cmdlFilePath
.Filter = "JPG Files|*.JPG|Bitmaps|*.BMP"
.ShowOpen
txtFilePath.Text = .filename
End With
End Sub

Private Sub Savepic()

Open "c:\colordraw0094_m.jpg" For Binary Access Read As lngDataFile
lngLengh = LOF(lngDataFile)
If lngLengh = 0 Then Close lngDatafile: Exit Sub
intChunks = lngLengh \ ChunkSize
intFragment = lngLengh Mod ChunkSize

'OpenData 打开数据库
Dim i As Integer
Dim rs As New ADODB.Recordset
Dim strQ As String

If rs.State = adStateOpen Then rs.Close

strQ = "Select * From [mydata]"
rs.Open strQ, conn, adOpenStatic, adLockOptimistic

On Error Resume Next

rs.AddNew

ReDim Chunk(intFragment)
Get lngDataFile, , Chunk()
rs.Fields("rs_photo1").AppendChunk Chunk()
ReDim Chunk(ChunkSize)

For i = 1 To intChunks
Get lngDataFile, , Chunk()
rs.Fields("rs_photo1").AppendChunk Chunk()
Next i

rs.Update
rs.Close
Close lngDataFile
Call ShowPic

End Sub

Public Sub ShowPic()

'OpenData 打开数据库
Dim i As Integer
Dim rs As New ADODB.Recordset
Dim strQ, filename As String
If rs.State = adStateOpen Then rs.Close

strQ = "Select * From [mydata]"
rs.Open strQ, conn, adOpenStatic, adLockOptimistic
If rs.EOF <> True Then
rs.MoveLast
Else
Exit Sub
End If
On Error Resume Next
Open "pictemp" For Binary Access Write As lngDataFile
lngLengh = rs.Fields("rs_photo1").ActualSize
intChunks = lngLengh \ ChunkSize
intFragment = lngLengh Mod ChunkSize
ReDim Chunk(intFragment)
Chunk() = rs.Fields("rs_photo1").GetChunk(intFragment)
Put lngDataFile, , Chunk()
For i = 1 To intChunks
ReDim Buffer(ChunkSize)
Chunk() = rs.Fields("rs_photo1").GetChunk(ChunkSize)
Put lngDataFile, , Chunk()
Next i
Close lngDataFile
filename = "pictemp"
Picture1.Picture = LoadPicture(filename)
Image1.Stretch = True
Image1.Picture = Picture1.Picture
Kill filename

End Sub

Private Sub Command1_Click()

Savepic

End Sub

Private Sub Command2_Click()

ShowPic

End Sub

上面写的是acess的代码!楼主可以改一下连接数据库的设置代码用于sql server!


[系统软件]Visual Studio 2005 Express Beta Products 下载链…  [系统软件]Visual FoxPro9.0中扩展报表系统功能
[系统软件]Visual FoxPro:我是旁观者  [系统软件]Visual Studio 2005 Express Editions Beta 2 下载…
[系统软件]Boost库在XP+Visual C++.net中的安装  [系统软件]Visual Studio 2005 Express Edition 正式版下载地…
[常用软件]Visual Foxpro通用报表打印程序  [常用软件]Visual FoxPro 6.0与大型数据库的无数据源连接
[常用软件]Visual Foxpro 的一个BUG  [VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (2)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台