为什么当我将此消息调用此函数时? [英] Why when I call this function this message up ?

查看:103
本文介绍了为什么当我将此消息调用此函数时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Function MaxFieldID(ByVal Tabel As String, ByVal Field As String) As Integer

       Try

           'con.Close()
           If con.State = ConnectionState.Closed Then con.Open()
           cmd = New SqlCommand("SELECT MAX(CAST(" + Field + " AS INT))+1 FROM " + Tabel, con)

           If cmd.ExecuteScalar Is DBNull.Value Then
               Return 1
           Else
               Return cmd.ExecuteScalar

           End If

       Catch ex As Exception
           MsgBox(ex.Message)

       Finally
           '  If con.State = ConnectionState.Open Then con.Close()
       End Try

   End Function





我尝试了什么:





What I have tried:

gmAccCmd.Parameters.Add("@idCard", SqlDbType.BigInt).Value = Zcls.MaxFieldID("empCards", "idCard")th







消息警告:

执行标量请求命令,以便在分配给命令的con

处于挂起的本地事务中时进行事务处理。

的事务属性该命令尚未初始化。




the message warning :
execute scalar requites command to have transaction when the con
assigned to the command is in pending local transaction.
the transaction property of the command has not been initialized.

推荐答案

查看错误消息:

Look at the error message:
Execute scalar requites command to have transaction when the con 
assigned to the command is in pending local transaction.
the transaction property of the command has not been initialized.



这意味着您正在使用的SqlConnection对象正在处理打开事务的命令,还没有完成它。



基本上,不要那样做SQL:创建你的 SqlConnection SqlCommand 中的对象每次需要时使用块作为新对象,不要回收它们作为你的现有代码:


What it means is "the SqlConnection object you are using is processing a command which opened a transaction and has not completed it".

Basically, don't "do SQL" like that: create your SqlConnection and SqlCommand objects inside a Using block as new objects each time you need them, do not "recycle" them as your existing code does:

Using con As New SqlConnection(strConnect)
	con.Open()
	Using cmd As New SqlCommand("SELECT iD, description FROM myTable", con)
		Using reader As SqlDataReader = cmd.ExecuteReader()
			While reader.Read()
				Dim id__1 As Integer = CInt(reader("iD"))
				Dim desc As String = DirectCast(reader("description"), String)
				Console.WriteLine("ID: {0}" & vbLf & "    {1}", iD, desc)
			End While
		End Using
	End Using
End Using





你自己做一个好处:找到一个更好的方法来做表 - 你不应该连接字符串来形成SQL命令,这是非常危险的。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。始终使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:



And do yourself a favour: find a better way to do tables - you should NEVER concatenate strings to form SQL commands, it is exceedingly dangerous. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且会出现问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



你不能使用带有表名的参数,所以找到一种更好的方法来处理它们 - 只依赖于字符串要正确是非常危险的!

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

You can't use parameters with table names, so find a better way to handle them - just relying on strings to be correct is very dangerous!


这篇关于为什么当我将此消息调用此函数时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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