简体   繁体   English

为什么我的SQL查询不做任何事情?

[英]Why Doesn't My SQL Query do anything?

Hi there I got this query string I have to run so I can update a distinct row in a sql server table: 嗨,我得到了必须运行的查询字符串,以便可以更新sql server表中的不同行:

    servidorSql.ConnectionString = conexao
    Dim insert As String = "UPDATE [Equipamentos] SET [ID_Cliente]=@ID_Cliente,      [Identificacao] = @Identificacao, [Fabricante] = @Fabricante, [Modelo] = @Modelo, [Versao_Software] = @Versao_Software, [Localizacao] = @Localizacao WHERE [ID_Equipamento]=@ID_Equipamento"

    Dim sqlquery As SqlCommand = New SqlCommand(insert, servidorSql)

    Try
        servidorSql.Open()
        sqlquery.Parameters.AddWithValue("@ID_Cliente", ID_Cliente)
        sqlquery.Parameters.AddWithValue("@ID_Equipamento", ID_Equipamento)
        sqlquery.Parameters.AddWithValue("@Identificacao", Identificacao)
        sqlquery.Parameters.AddWithValue("@Fabricante", Fabricante)
        sqlquery.Parameters.AddWithValue("@modelo", modelo)
        sqlquery.Parameters.AddWithValue("@versao_Software", versao_Software)
        sqlquery.Parameters.AddWithValue("@localizacao", localizacao)
        sqlquery.ExecuteNonQuery()
        servidorSql.Close()

    Catch e As SqlClient.SqlException
        MsgBox(e.ToString())
    End Try

The problem is that it doens't do anything, it doesn't update the table and it doesn't give me any exception. 问题是它什么也不做,不会更新表,也不会给我任何异常。 "ID_Equipamento" is key and identity of the table and I've tried to run the query with the field being update or not, and it's the same. “ ID_Equipamento”是表的键和标识,我尝试使用字段是否为更新来运行查询,它是相同的。 Any suggestion? 有什么建议吗?

Try adding a commit. 尝试添加提交。

Even if the database autocommits, it's almost always better to explicitly take care of it. 即使数据库自动提交,显式处理它总是总是更好的。

Try using SQL Server Profiler- that will give you much more of a clue as to what is happening. 尝试使用SQL Server Profiler-可以为您提供更多有关正在发生的事情的线索。 Chances are you are passing in an ID that doesn't exist. 您可能会传入一个不存在的ID。

Capture an SQL Profiler trace to confirm that the parameter that the ID your passing in is definitely the ID that you think it is. 捕获SQL事件探查器跟踪以确认您传入的ID的参数绝对是您认为的ID。 If you also select the RowCounts column you can see the number of rows updated. 如果您还选择RowCounts列,则可以看到更新的行数。

The sqlquery.ExecuteNonQuery() instruction returns the number of rows affected, so you can check it also, something like: sqlquery.ExecuteNonQuery()指令返回受影响的行数,因此您也可以检查它,例如:

int rowsAffected = sqlquery.ExecuteNonQuery();
Debug.WriteLine("Rows affected: " + rowsAffected);

You can also check the ID_Equipamento variable. 您还可以检查ID_Equipamento变量。

Debug.WriteLine(ID_Equipamento);

Maybe this variable doesn't hold the value of the primary key for your table 也许此变量不包含表的主键的值

You're not inserting anything. 您没有插入任何内容。 And I doubt you're even modifying things. 我怀疑您是否正在修改内容。 You're executing an UPDATE statement. 您正在执行UPDATE语句。 Check if you're updating the record with different values than the ones stored in your database. 检查是否使用与数据库中存储的值不同的值更新记录。 Of course, @ID_Equipamento might be the ID of a non-existing record, in which case you're not updating anything. 当然,@ ID_Equipamento可能是不存在的记录的ID,在这种情况下,您不会更新任何内容。 That won't generate an error but just tells you it modified 0 records. 那不会产生错误,只是告诉您它修改了0条记录。

The problem it's in the query it self! 问题出在查询本身! Couldn't figure ou very well why, but doens't matter. 不太清楚为什么,但是没关系。

The problem is solved.... Thanks anyway for your help. 问题已解决。...无论如何,谢谢您的帮助。 Aprecciated Aprecciated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM