关于ado.net设计的非常*基本问题 [英] *very* basic question about design of ado.net

查看:76
本文介绍了关于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

推荐答案

>这些似乎运作良好和快速。但是有一些问题:
> These seem to work well and fast. But there are a few questions:

1.是否有正确的问题。结构创建填充表单所需的不同组件的方法,如果是这样,到目前为止,我的
右边是否正确?
A.)对我来说没问题,但不同的人对如何设计结构有不同的想法。

2.我应该使用离开吗?或Lost_Focus或Lost_Focus。对于我的文本框?我曾经使用过Lost_Focus和Lost_Focus。使用VB6,但想知道是否有差异
或者是否首选。
A.)使用(LostFocus)


3.如果以上两个数据站只返回一条记录,是否有一些我应该用来提高性能的读者?
A.)相信我,这对一条记录没有任何影响。

当然

不是你能注意到的。


任何其他建议肯定会受到赞赏。我有点湿透了,所以我会喜欢一些帮助。

谢谢!
Steve

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? A.) Looks ok to me, but different people have different ideas about how to
design a structure.

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. A.) Use ( LostFocus )


3. If the two datareaders above only return one record, is there some
other reader I should use to improve performance? A.) Trust me, this is not going to make any difference with one record.
Certainly
not one you could notice.


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可以。顺便说一句,因为你正在使用访问权限,你不需要

这是SQLclient的第三个导入语句。


对于任何可能知道这一点的人来说也是一般性的问题。我看到Steve

调用了他的命令对象的dispose方法。这是否有必要删除资源或者垃圾收集最终是否自己获取它(在我的情况下我使用的是数据库类而不是模块)。与VB6相比,我对垃圾收集感到非常困惑。还有表格

我们需要调用处理方法还是足够接近?


-Ivan


***通过开发人员发送的指南 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

That looks pretty good, I am also a newbie and know how frustrating
ADO.NET can be. By the way since you are using access you do not need
that third import statement for SQLclient.

Also general question for anyone who might know this. I saw that Steve
called the dispose method of his command object. Is this necessary to
remove the resources or does garbage collection eventually grab it on
its own (in my case I am using a database class not a module). I am
very confused about garbage collection compared to VB6. Also with forms
do we need to call the dispose method or is just close sufficient?

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这取决于,当然,调用dispose方法而不是等待系统意识到需要标记/删除表单对象是个好主意。


OHM

Ivan Weiss < 4 ***** @ optonline.net>写在留言新闻:uj ************** @ TK2MSFTNGP09.phx.gbl ...

看起来不错,我也是新手,知道如何令人沮丧

ADO.NET可以。顺便说一句,因为你正在使用访问权限,你不需要

这是SQLclient的第三个导入语句。


对于任何可能知道这一点的人来说也是一般性的问题。我看到Steve

调用了他的命令对象的dispose方法。这是否有必要删除资源或者垃圾收集最终是否自己获取它(在我的情况下我使用的是数据库类而不是模块)。与VB6相比,我对垃圾收集感到非常困惑。还有表格

我们需要调用处理方法还是足够接近?


-Ivan


***通过开发人员发送的指南 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

It depends, as a matter of course, it is a good idea to call the dispose method rather than waiting for the system to realise that a form object needs to be marked/deleted.

OHM
"Ivan Weiss" <iv*****@optonline.net> wrote in message news:uj**************@TK2MSFTNGP09.phx.gbl...
That looks pretty good, I am also a newbie and know how frustrating
ADO.NET can be. By the way since you are using access you do not need
that third import statement for SQLclient.

Also general question for anyone who might know this. I saw that Steve
called the dispose method of his command object. Is this necessary to
remove the resources or does garbage collection eventually grab it on
its own (in my case I am using a database class not a module). I am
very confused about garbage collection compared to VB6. Also with forms
do we need to call the dispose method or is just close sufficient?

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆