本文介绍了关于ado.net设计的非常*基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前抱歉我的无知。任何帮助肯定会赞赏
。我正在用VB.Net写一个相当简单的应用程序和

显然是一个新手。此应用程序将由1,2和/或
或最多3人同时使用,我正在使用Access 2003获取我的数据

来源。我们没有处理大量的数据(5或6

表,总共可能有3,000条记录 - 一张表格中的大部分是

)。这个应用程序使用一个相当简单的形式,

但是在离开某些文本框时,我想填写一些数据。对于

实例,我可能有一个竞赛号码字段 -

当我离开它时,我想填写一个描述。下一个字段

可能是竞争对手的号码,当我离开时,我想用竞争对手的名字填充一个

字段。


足够的背景 - 我创建了一个模块来打开我的连接

以及其中的一个函数来处理我的DataReader。在那个模块中我

有:


Imports System.Data

Imports System.data.oledb

Imports System.Data.sqlclient


Module Main

Public strConn As String =" Provider = Microsoft.Jet.OLEDB.4.0;数据

源= f:\ mydatabase.mdb"

Public cn As New OleDbConnection(strConn)


函数Exec​​uteReader( ByVal sSQLString As String)As

OleDb.OleDbDataReader


Dim dr As OleDbDataReader

Dim cmd As OleDbCommand = New OleDbCommand(sSQLString ,cn)

尝试

如果cn.State = ConnectionState.Closed那么cn.Open()

dr = cmd.ExecuteReader()

cmd.Dispose()

Catch ex As OleDbException

MsgBox(ex.Message,MsgBoxStyle.Exclamation)

结束尝试

返回博士

结束功能


结束模块


那个到目前为止,它几乎就是这样。在我的主要代码中,我有:


Private Sub txtCompNum_Leave(ByVal sender As Object,ByVal e As

System.EventArgs)处理txtCompNum.LostFocus

Dim dr As OleDbDataReader

dr = ExecuteReader(" SELECT * FROM competition where compnum =

"&"''"& txtCompNum.Text&"''")

当dr.Read

txtDance.Text = dr.GetString(2)

结束时

dr.Close()

End Sub


Private Sub txtCompetitor_Leave(ByVal sender As Object,ByVal e As

System.EventArgs)处理txtCompetitor.Leave

Dim dr As OleDbDataReader

dr = ExecuteReader(" SELECT * FROM dancer where cardnum =" &

txtCompetitor.Text)

当dr.Read

txtName.Text = dr.GetString(3)& , &安培; dr.GetString(4)

结束时

dr.Close()

cn.Close()

结束子


这些似乎工作得很好而且速度很快。但是有一些问题:


1.是否有正确的问题。结构创建不同的

组件填充表单所需的组件,如果是这样,到目前为止我的

右行是什么?


2.我应该使用离开吗?或Lost_Focus或Lost_Focus。对于我的文本框?我以前用

使用Lost_Focus使用VB6,但想知道是否有差异

或者如果首选。


3.如果上面的两个数据站只返回一条记录,则是还有一些

其他读者我应该用它来提高性能吗?


任何其他建议一定会受到赞赏。我有点

湿漉漉的耳朵所以我会喜欢一些帮助。


谢谢!

Steve

Sorry in advance for my ignorance. Any help would sure be
appreciated. I''m writing a fairly simple application with VB.Net and
am obviously a bit of a newbie. This application will be used by 1, 2
or at most 3 people concurrently and I''m using Access 2003 for my data
source. We are not dealing with a large amount of data (5 or 6
tables, for a total of maybe 3,000 records - one table having the
majority of that). This application is using a fairly simple form,
but upon leaving certain text boxes, I want to fill in some data. For
instance, I might have one field that is for a competition number -
when I leave it, I want to populate a description. The next field
might be a competitor number and when I leave it, I want to populate a
field with the competitor name.

Enough of the background - I created a module to open my connection
and a function within that to handle my DataReader. In that module I
have:

Imports System.Data
Imports System.data.oledb
Imports System.Data.sqlclient

Module Main
Public strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=f:\mydatabase.mdb"
Public cn As New OleDbConnection(strConn)

Function ExecuteReader(ByVal sSQLString As String) As
OleDb.OleDbDataReader

Dim dr As OleDbDataReader
Dim cmd As OleDbCommand = New OleDbCommand(sSQLString, cn)
Try
If cn.State = ConnectionState.Closed Then cn.Open()
dr = cmd.ExecuteReader()
cmd.Dispose()
Catch ex As OleDbException
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
Return dr
End Function

End Module

And that''s pretty much it so far. In my main code I have:

Private Sub txtCompNum_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtCompNum.LostFocus
Dim dr As OleDbDataReader
dr = ExecuteReader("SELECT * FROM competition where compnum =
" & "''" & txtCompNum.Text & "''")
While dr.Read
txtDance.Text = dr.GetString(2)
End While
dr.Close()
End Sub

Private Sub txtCompetitor_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtCompetitor.Leave
Dim dr As OleDbDataReader
dr = ExecuteReader("SELECT * FROM dancer where cardnum = " &
txtCompetitor.Text)
While dr.Read
txtName.Text = dr.GetString(3) & ", " & dr.GetString(4)
End While
dr.Close()
cn.Close()
End Sub

These seem to work well and fast. But there are a few questions:

1. Is there a "right" way to structure creating the different
components necessary to populate my form and if so, is mine along the
right lines so far?

2. Should I use "Leave" or "Lost_Focus" for my text boxes? I used to
use "Lost_Focus" with VB6, but was wondering if there was a difference
or if one was preferred.

3. If the two datareaders above only return one record, is there some
other reader I should use to improve performance?

Any other suggestions would sure be appreciated. I''m a bit
wet-behind-the-ears so I''d love some help.

Thanks!
Steve

推荐答案





这篇关于关于ado.net设计的非常*基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:09