代码正在执行而没有任何错误,但数据库没有更新 [英] Code is executing without any error, but database is not updating

查看:176
本文介绍了代码正在执行而没有任何错误,但数据库没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码正在执行而没有任何错误,但数据库没有更新。



我尝试过:



同样的查询我在sql中运行并且它的工作成功。我尝试直接分配datagrid值,并尝试将值存储在变量中,然后将其放入查询中。

这是我的代码。



Code is executing without any error, but database is not updating.

What I have tried:

same query i run in sql and its work successfully. I tried by assigning the datagrid value directly and also tried by storing the value in a variable and then put it in query.
Here is my code.

'generating transaction ID
 Dim dateAndTime As Date = Now
 Dim tra_id As Long = Format(dateAndTime, "ddHHmm")

 'inserting into database
 db_connect.Open()
 Dim indate As Date = System.DateTime.Today
 Dim R_index As Integer
 For R_index = 0 To dataGrid_incoming.Rows.Count - 2


     Dim tempLot As String = dataGrid_incoming.Rows(R_index).Cells(1).Value.ToString
     Dim temptot As String = dataGrid_incoming.Rows(R_index).Cells(2).Value.ToString
     Dim tempqty As Integer = dataGrid_incoming.Rows(R_index).Cells(3).Value
     Dim tempdet As String = dataGrid_incoming.Rows(R_index).Cells(4).Value.ToString

     Dim cmd As SqlClient.SqlCommand = db_connect.CreateCommand()
     cmd.CommandText = "Insert into item (tra_id, lot_no, totul ,qty , tra_status ) Values(" & tra_id & ", '" & tempLot & "', '" & temptot & "'," & tempqty & ", 'In_coming')"
     ' cmd.CommandText = "Insert into item (tra_id, lot_no, totul ,qty , tra_status ) Values(" & tra_id & ", '" & dataGrid_incoming.Rows(R_index).Cells(1).Value.ToString & "', '" & dataGrid_incoming.Rows(R_index).Cells(2).Value.ToString & "'," & dataGrid_incoming.Rows(R_index).Cells(3).Value & ", 'In_coming')"

     Dim cmd2 As SqlClient.SqlCommand = db_connect.CreateCommand()
     cmd2.CommandText = "Insert into item_in (cus_id , tra_id , in_date ,in_detail) Values(" & cs_id & ", " & tra_id & ", " & indate & ", '" & tempdet & "')"
     'cmd2.CommandText = "Insert into item_in (cus_id , tra_id , in_date ,in_detail) Values(" & cs_id & ", " & tra_id & ", " & indate & ", '" & dataGrid_incoming.Rows(R_index).Cells(4).Value.ToString & "')"

 Next

 db_connect.Close()
 dataGrid_incoming.Rows.Clear()

推荐答案

cmd2.CommandText = "Insert into item_in (cus_id , tra_id , in_date ,in_detail) Values(" & cs_id & ", " & tra_id & ", " & indate & ", '" & tempdet & "')"



不是解决方案你的问题,但你有另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


如果你使用参数化查询,那么sql可能会工作 - 参见 indate ?这是一个约会,但在最后的sql语句中没有单引号。



但是,只有在实际运行它时,sql才会起作用。您缺少对 ExecuteNonQuery Method <的调用/ a> [ ^ ] - 每个 cmd cmd2
If you use a parameterized query then the sql will probably work - see indate? It's a date but there are no single-quotes around it in the final sql statement.

However, the sql will only work if you actually run it. You are missing the calls to ExecuteNonQuery Method[^] - one each for cmd and cmd2


这篇关于代码正在执行而没有任何错误,但数据库没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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