为什么这个sql命令执行了两次? [英] why is this sql command executed twice?
问题描述
我编写了将数据插入表格的代码,但它运行了两次。如果我删除
行:comd.ExecuteNonQuery(),则它会运行一次;但我认为
行是执行sql命令所必需的......如果我删除了
oConnection.Open(),那么错误:连接没有打开。
看到我的代码:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql,na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings(" myconn")。C onnectionString.ToString()
sql =" INSERT INTO table(...)VALUES(...);"
oConnection =新的SqlConnection(sConnectionString)
comd =新的SqlCommand(sql,oConnection)
comd.Parameters.Add(" @ var1" ;,SqlDbType.NVarChar,10).value =" ok"
oConnection.Open()
comd.ExecuteNonQuery()
谢谢
Bob
Bob写道:
我编写了将数据插入表中的代码,但它运行了两次。如果我删除
行:comd.ExecuteNonQuery(),则它会运行一次;但我认为
行是执行sql命令所必需的......如果我删除了
oConnection.Open(),那么错误:连接没有打开。
看到我的代码:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql,na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings(" myconn")。C onnectionString.ToString()
sql =" INSERT INTO table(...)VALUES(...);"
oConnection =新的SqlConnection(sConnectionString)
comd =新的SqlCommand(sql,oConnection)
comd.Parameters.Add(" @ var1" ;,SqlDbType.NVarChar,10).value =" ok"
oConnection.Open()
comd.ExecuteNonQuery()
谢谢
Bob
该代码只运行一次查询。其余的代码做了什么?
另外,我看到你编辑了部分SQL查询。你还有其他什么吗?b $ b编辑出去了吗?
-
G?跑Andersson
_____
http://www.guffa.com
感谢您的回复。
确实有更多代码:这是整个代码:
------- -------------------------------------------
Dim trans As SqlTransaction = Nothing''new
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql,na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings(" myconn")。 C onnectionString.ToString()
sql =" INSERT INTO table(...)VALUES(...); SELECT
SCOPE_IDENTITY();"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql,oConnection)
comd.Parameters.Add(" @ var1",SqlDbType.NVarChar,10).value =" ok"
试试''新
oConnection.Open()
comd.ExecuteNonQuery()
trans = connection.BeginTransaction''new
comd.Transaction = trans''new
''这里另一个插入sqlcommand但即使我删除了这部分,它仍然是
执行两次...
trans.Commit()''new
Catch sqlEx As SqlException
如果trans IsNot Nothing那么
trans.Rollback()
结束如果
抛出新例外(错误!,sqlEx)
返回
最后
如果连接IsNot Nothing那么
connection.Close()
E nd如果
结束尝试
Response.Redirect(" fin.aspx")
结束子
" G?ran Andersson" < gu *** @ guffa.comschreef in bericht
news:u
**************** @ TK2MSFTNGP05.phx.gbl ...
Bob写道:
>
我编写了将数据插入表中的代码,但它运行了两次。如果我删除了行:comd.ExecuteNonQuery(),那么它会运行一次;但我认为该行是执行sql命令所必需的...如果我
删除 oConnection.Open()",然后错误:连接未打开。
查看我的代码:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql,na As String
Dim iden as Integer
ConfigurationManager.ConnectionStrings(" myconn")。 ConnectionString.ToString()
sql =" INSERT INTO table(...)VALUES(...);"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand( sql,oConnection)
comd.Parameters.Add(" @ var1",SqlDbType.NVarChar,10).value =" ok"
oConnection.Open()
comd.ExecuteNonQuery( )
谢谢
Bob
该代码只运行一次查询。其余的代码做了什么?
另外,我看到你编辑了部分SQL查询。你还有其他什么吗?b $ b编辑出去了吗?
-
G?跑Andersson
_____
http://www.guffa.com
Hi,
i wrote code for inserting data into a table, but it runs twice. If i remove
the line: "comd.ExecuteNonQuery()", then it runs once; but i thought that
line was necessary for executing the sql command ... if i remove "
oConnection.Open()", then error: "connection is not open".
see my code:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql, na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
sql = "INSERT INTO table(...) VALUES (...);"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql, oConnection)
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
oConnection.Open()
comd.ExecuteNonQuery()
Thanks
Bob
Bob wrote:Hi,
i wrote code for inserting data into a table, but it runs twice. If i remove
the line: "comd.ExecuteNonQuery()", then it runs once; but i thought that
line was necessary for executing the sql command ... if i remove "
oConnection.Open()", then error: "connection is not open".
see my code:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql, na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
sql = "INSERT INTO table(...) VALUES (...);"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql, oConnection)
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
oConnection.Open()
comd.ExecuteNonQuery()
Thanks
Bob
That code only runs the query once. What does the rest of the code do?
Also, I see that you edited out part of the SQL query. Anything else you
edited out?
--
G?ran Andersson
_____
http://www.guffa.com
Thanks for replying.
There is indeed more code: here is the whole code:
--------------------------------------------------
Dim trans As SqlTransaction = Nothing ''new
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql, na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
sql = "INSERT INTO table(...) VALUES (...); SELECT
SCOPE_IDENTITY();"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql, oConnection)
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
Try ''new
oConnection.Open()
comd.ExecuteNonQuery()
trans = connection.BeginTransaction ''new
comd.Transaction = trans ''new
''here another insert sqlcommand but even if i remove this part, it still
executes twice ...
trans.Commit() ''new
Catch sqlEx As SqlException
If trans IsNot Nothing Then
trans.Rollback()
End If
Throw New Exception("error!", sqlEx)
Return
Finally
If connection IsNot Nothing Then
connection.Close()
End If
End Try
Response.Redirect("fin.aspx")
End Sub
"G?ran Andersson" <gu***@guffa.comschreef in bericht
news:u
****************@TK2MSFTNGP05.phx.gbl...Bob wrote:>Hi,
i wrote code for inserting data into a table, but it runs twice. If i
remove the line: "comd.ExecuteNonQuery()", then it runs once; but i
thought that line was necessary for executing the sql command ... if i
remove " oConnection.Open()", then error: "connection is not open".
see my code:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql, na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings("myconn"). ConnectionString.ToString()
sql = "INSERT INTO table(...) VALUES (...);"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql, oConnection)
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
oConnection.Open()
comd.ExecuteNonQuery()
Thanks
Bob
That code only runs the query once. What does the rest of the code do?
Also, I see that you edited out part of the SQL query. Anything else you
edited out?
--
G?ran Andersson
_____
http://www.guffa.com
这篇关于为什么这个sql命令执行了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!