为什么这段代码不会产生错误? [英] Why does this code not generate an error?

查看:59
本文介绍了为什么这段代码不会产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Group,


为什么以下代码不会产生错误?我有一张表有x

的记录数。每条记录都从DB读入一个哈希表。

然后我添加哈希表附加键。从这里开始,我在哈希表中的每个键上运行

并更改相关值。现在,当我使用哈希表中的键和值更新数据库时,所有工作都很好。但是,我还在哈希表中添加了额外的项目。所以,到目前为止,我担心的是,当代码试图对表中不存在

的记录进行更新时,它应该生成错误,因为它可以

找不到它。然而,它只是忽略它并继续,好像所有的一切都很好。我没有在代码中添加任何错误处理,所以理论上

它应该抱怨。


这是代码。


Dim FileTable作为新的HashTable

ScanDA =新的SqlDataAdapter

ScanConStr =" server = localhost; uid = SA; password = 123;数据库= myD B"

ScanConn =新的SqlConnection(ScanConStr)

ScanQuery =" SELECT FilePath,Comment from myTable"

ScanCMD = New SqlCommand (ScanQuery,ScanConn)


ScanDA.SelectCommand = ScanCMD

ScanConn.Open()

每个HashLine作为DictionaryEntry在FileTable中


UpdateMD5Cmd =" UPDATE myTable SET" &安培; Chr(34)& " newMD5" &安培; Chr(34)&

" =''updated''WHERE" &安培; Chr(34)& "文件路径" &安培; Chr(34)& " =" &

HashLine.Key& "''"


ScanCMD.CommandText = UpdateMD5Cmd

ScanCMD.ExecuteNonQuery()


下一步


ScanConn.Close()


因此,它将所有文件名和注释加载到哈希表中

( filename是键,注释是值)。然后它处理

评论。之后,它将密钥上传回FilePath

列,并将值上传回评论列。

因此,它应该抛出一个错误,因为它无法找到新的文件我

添加到哈希表中。


我希望这很清楚!谢谢。


-


Daniel

MCSE,MCP + I,Windows 2000 / NT中的MCP


--------------------------------------

从我的邮箱地址中删除第二个马德里联系我。

Hi Group,

Why does the below code not generate an error? I have a table with x
number of records. Each record is read from the DB into a hashtable.
Then I add to the hashtable additional keys. From here, i run through
each key in the hashtable and change the associated value. Now, when I
update the DB with the keys and values from the hashtable, all works
well. However, I also added to the hashtable extra items. So, as far
as I am concerned, when the code tries to do an update for a record that
does not exist in the table it should generate an error because it could
not find it. However, it just ignores it and continues as if everything
is great. I have not added any error handling to the code so in theory
it should complain.

Here is the code.

Dim FileTable as new HashTable

ScanDA = New SqlDataAdapter
ScanConStr = "server=localhost;uid=SA;password=123;database=myD B"
ScanConn = New SqlConnection(ScanConStr)
ScanQuery = "SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery, ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()
For Each HashLine As DictionaryEntry In FileTable

UpdateMD5Cmd = "UPDATE myTable SET " & Chr(34) & "newMD5" & Chr(34) &
"=''updated'' WHERE " & Chr(34) & "FilePath" & Chr(34) & "=''" &
HashLine.Key & "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

Next

ScanConn.Close()

So, it loads all the file names and comments into the hashtable
(filename is the key and the comment is the value). Then it processes
the comments. After that, it uploads the keys back into the FilePath
column and the values back into the comments column.
So, it should throw an error because it cannot find the new files i
added to the hashtable.

I hope this is clear! Thanks.

--

Daniel
MCSE, MCP+I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.

推荐答案



"丹尼尔" <哒**** @ madridmadridsoleado.com>在消息中写道

新闻:uD ************** @ TK2MSFTNGP09.phx.gbl ...

"Daniel" <da****@madridmadridsoleado.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
Hi Group,

为什么以下代码不会生成错误?我有一张表有x
的记录数。每条记录都从DB读入哈希表。然后
我添加哈希表附加键。从这里开始,我遍历哈希表中的每个键
并更改相关值。现在,当我使用散列表中的键和值更新
数据库时,一切正常。但是,我还在哈希表中添加了额外的项目。因此,就我而言,当代码尝试对表中不存在的记录进行更新时,它应该生成错误,因为它无法找到它。
然而,它只是忽略了它,并继续好像一切都很好。我没有在代码中添加任何错误处理,所以理论上它应该抱怨。

这是代码。

Dim FileTable as new HashTable

ScanDA =新的SqlDataAdapter
ScanConStr =" server = localhost; uid = SA; password = 123; database = myD B"
ScanConn = New SqlConnection(ScanConStr)< br> ScanQuery =" SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery,ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()

每个HashLine As DictionaryEntry在FileTable中

UpdateMD5Cmd =" UPDATE myTable SET" &安培; Chr(34)& " newMD5" &安培; Chr(34)&
" ='''更新''WHERE" &安培; Chr(34)& "文件路径" &安培; Chr(34)& " =" &安培; HashLine.Key
& "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

下一页

ScanConn.Close()

因此,它将所有文件名和注释加载到哈希表中(文件名
是关键,注释是值)。然后它处理注释。
之后,它将键上传回FilePath列,并将
值上传回注释列。
因此,它应该抛出一个错误,因为它无法找到我添加的新文件是哈希表。

我希望这很清楚!谢谢。

-

Daniel
MCSE,MCP + I,Windows 2000 / NT中的MCP

----- ---------------------------------
从我的邮箱地址中删除第二个马德里,与我联系。
Hi Group,

Why does the below code not generate an error? I have a table with x
number of records. Each record is read from the DB into a hashtable. Then
I add to the hashtable additional keys. From here, i run through each key
in the hashtable and change the associated value. Now, when I update the
DB with the keys and values from the hashtable, all works well. However,
I also added to the hashtable extra items. So, as far as I am concerned,
when the code tries to do an update for a record that does not exist in
the table it should generate an error because it could not find it.
However, it just ignores it and continues as if everything is great. I
have not added any error handling to the code so in theory it should
complain.

Here is the code.

Dim FileTable as new HashTable

ScanDA = New SqlDataAdapter
ScanConStr = "server=localhost;uid=SA;password=123;database=myD B"
ScanConn = New SqlConnection(ScanConStr)
ScanQuery = "SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery, ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()
For Each HashLine As DictionaryEntry In FileTable

UpdateMD5Cmd = "UPDATE myTable SET " & Chr(34) & "newMD5" & Chr(34) &
"=''updated'' WHERE " & Chr(34) & "FilePath" & Chr(34) & "=''" & HashLine.Key
& "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

Next

ScanConn.Close()

So, it loads all the file names and comments into the hashtable (filename
is the key and the comment is the value). Then it processes the comments.
After that, it uploads the keys back into the FilePath column and the
values back into the comments column.
So, it should throw an error because it cannot find the new files i added
to the hashtable.

I hope this is clear! Thanks.

--

Daniel
MCSE, MCP+I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.



你可以更新0条记录,所以如果它找不到记录,它就不会更新任何内容。


也许我不理解,但似乎就是这种情况。


You are allowed to update 0 records, so if it cannot find the record, it
will simply not update anything.

Perhaps I am not understanding, but it seems that this is the case.




1)你可能尝试使用string.format创建命令字符串 -

使事物更具可读性


例如

private const CMD_STRING as string =" UPDATE myTable SET newMD5 =

''updated''WHERE FilePath =''{0}''"


UpdateMD5Cmd = string。格式化(CMD_STRING,cstr(HashLine.Key))

2)Debug.writeline()命令字符串和他们试图通过

手来运行它们以查看它们是否更新什么 - 有可能是t嘿是

找不到要更新的记录(比如一个案例比较问题)因此

没有更新任何东西但没有给出任何错误。

艾伦。


1) You might try using string.format for creating the command string -
makes things a touch more readable

e.g.
private const CMD_STRING as string = "UPDATE myTable SET newMD5 =
''updated'' WHERE FilePath = ''{0}''"

UpdateMD5Cmd= string.Format(CMD_STRING, cstr(HashLine.Key))
2) Debug.writeline() the command strings and them try to run them by
hand to see if they update anything - it is possible that they are
finding no records to update (say a case comparison problem) and thus
are not updating anything but not giving any errors.
Alan.


Rick Mogstad写道:
Rick Mogstad wrote:
" Daniel" <哒**** @ madridmadridsoleado.com>在消息中写道
新闻:uD ************** @ TK2MSFTNGP09.phx.gbl ...
"Daniel" <da****@madridmadridsoleado.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
Hi Group,

为什么以下代码不会生成错误?我有一张表有x
的记录数。每条记录都从DB读入哈希表。然后
我添加哈希表附加键。从这里开始,我遍历哈希表中的每个键
并更改相关值。现在,当我使用散列表中的键和值更新
数据库时,一切正常。但是,我还在哈希表中添加了额外的项目。因此,就我而言,当代码尝试对表中不存在的记录进行更新时,它应该生成错误,因为它无法找到它。
然而,它只是忽略了它,并继续好像一切都很好。我没有在代码中添加任何错误处理,所以理论上它应该抱怨。

这是代码。

Dim FileTable as new HashTable

ScanDA =新的SqlDataAdapter
ScanConStr =" server = localhost; uid = SA; password = 123; database = myD B"
ScanConn = New SqlConnection(ScanConStr)< br> ScanQuery =" SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery,ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()

每个HashLine As DictionaryEntry在FileTable中

UpdateMD5Cmd =" UPDATE myTable SET" &安培; Chr(34)& " newMD5" &安培; Chr(34)&
" ='''更新''WHERE" &安培; Chr(34)& "文件路径" &安培; Chr(34)& " =" &安培; HashLine.Key
& "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

下一页

ScanConn.Close()

因此,它将所有文件名和注释加载到哈希表中(文件名
是关键,注释是值)。然后它处理注释。
之后,它将键上传回FilePath列,并将
值上传回注释列。
因此,它应该抛出一个错误,因为它无法找到我添加的新文件是哈希表。

我希望这很清楚!谢谢。

-

Daniel
MCSE,MCP + I,Windows 2000 / NT中的MCP

----- ---------------------------------
从我的邮箱地址中删除第二个马德里,与我联系。
Hi Group,

Why does the below code not generate an error? I have a table with x
number of records. Each record is read from the DB into a hashtable. Then
I add to the hashtable additional keys. From here, i run through each key
in the hashtable and change the associated value. Now, when I update the
DB with the keys and values from the hashtable, all works well. However,
I also added to the hashtable extra items. So, as far as I am concerned,
when the code tries to do an update for a record that does not exist in
the table it should generate an error because it could not find it.
However, it just ignores it and continues as if everything is great. I
have not added any error handling to the code so in theory it should
complain.

Here is the code.

Dim FileTable as new HashTable

ScanDA = New SqlDataAdapter
ScanConStr = "server=localhost;uid=SA;password=123;database=myD B"
ScanConn = New SqlConnection(ScanConStr)
ScanQuery = "SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery, ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()
For Each HashLine As DictionaryEntry In FileTable

UpdateMD5Cmd = "UPDATE myTable SET " & Chr(34) & "newMD5" & Chr(34) &
"=''updated'' WHERE " & Chr(34) & "FilePath" & Chr(34) & "=''" & HashLine.Key
& "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

Next

ScanConn.Close()

So, it loads all the file names and comments into the hashtable (filename
is the key and the comment is the value). Then it processes the comments.
After that, it uploads the keys back into the FilePath column and the
values back into the comments column.
So, it should throw an error because it cannot find the new files i added
to the hashtable.

I hope this is clear! Thanks.

--

Daniel
MCSE, MCP+I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.



你可以更新0条记录,所以如果它找不到记录,它就不会更新任何东西。

也许我不是理解,但似乎就是这种情况。



You are allowed to update 0 records, so if it cannot find the record, it
will simply not update anything.

Perhaps I am not understanding, but it seems that this is the case.



嗯,那么,如果没有什么可以让VB抛出错误

更新?


-

Daniel

Windows中的MCSE,MCP + I,MCP 2000 / NT


----------------------------------- ---

从我的邮箱地址中删除第二个马德里联系我。


Hmmm, so then, how can I get VB to throw an error if there is nothing to
update?

--

Daniel
MCSE, MCP+I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.


这篇关于为什么这段代码不会产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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