简体   繁体   English

在 C# 中将 SQL 查询参数设置为 Int32

[英]Setting SQL query parameter to Int32 in C#

I'm having problems with some code I'm trying to write.我在尝试编写一些代码时遇到了问题。 I'm doing something for suppliers orders, so I have a table which is named "encomendas_fornecedores" with a autoincrement field before the key that is the code of sale which consists in a EF before the number(which is a text field).我正在为供应商订单做一些事情,所以我有一个名为“encomendas_fornecedores”的表,在键之前有一个自动增量字段,该字段是销售代码,它包含在数字(这是一个文本字段)之前的 EF 中。

Here is the code:这是代码:

connection.Open();

        OleDbCommand comando1 = new OleDbCommand();
        OleDbCommand comando2 = new OleDbCommand();
        OleDbCommand comando3 = new OleDbCommand();
        comando1.Connection = connection;
        comando2.Connection = connection;
        comando3.Connection = connection;
        comando1.CommandText = "INSERT INTO encomendas_fornecedores (cod_encomenda_forn, cod_metodo, cod_forn, total_pagar_forn) VALUES('FO', '" + txtcodmetodo.Text + "', '" + txtcodforn.Text + "', '" + lbltotalapagar.Text + "'); ";// insert into table the values with a FO to cod
        comando1.ExecuteNonQuery();
        comando2.CommandText = "Select MAX(num_encomenda) From encomendas_fornecedores;";// selecting maximum num encomenda so I can isolate it and add to a text before(btw I do this in php/sql no problems
       int  numero = Convert.ToInt32(comando2.ExecuteScalar());//max num_encomenda
        string codencomendaforn= "EF"+Convert.ToString(numero);// sales code completed
        comando3.CommandText = "UPDATE encomendas_fornecedores SET cod_encomenda_forn = '"+codencomendaforn+"' WHERE num_encomenda = '"+ numero +"';";//query that is giving me the problems, it says something like "type of data incorrect in data expression"
        comando3.ExecuteScalar();//giving me error this line

        connection.Close();

But now here's the catch the cod_encomenda_forn is text and the num_encomenda auto increment as it is in the sql, and I tried to show the query in a textbox to see if its anything is wrong but nothing seems wrong.但是现在这里的问题是 cod_encomenda_forn 是文本,而 num_encomenda 自动递增,就像它在 sql 中一样,我尝试在文本框中显示查询以查看它是否有任何问题,但似乎没有任何问题。

"UPDATE encomendas_fornecedores SET cod_encomenda_forn = '"+codencomendaforn+"' WHERE num_encomenda = **'**"+ **numero** +"**'**;";//query that is giving me the problems,it says something like "type of data incorrect in data expression"

You are passing a string numero to a where statement that seems like it is expecting a number.您正在将一个字符串numero传递给一个似乎需要一个numero的 where 语句。 As long as it is numeric it should work, but definitely not gauranteed to work.只要它是数字,它就应该可以工作,但绝对不能保证工作。 Second you are passing another codencomendaforn string to encomenda what is encomenda 's data type?其次,您将另一个codencomendaforn字符串传递给encomenda什么是encomenda的数据类型?

It appears that you are not handling potential datatype differences between your c# code and your SQL query.您似乎没有处理 c# 代码和 SQL 查询之间潜在的数据类型差异。 In addition single quoting '' around a value in a SQL statement tells the database engines that it is a string even if that is '1234'.此外,在 SQL 语句中的值周围用单引号 '' 告诉数据库引擎它是一个字符串,即使它是 '1234'。 While SQL will automatically convert some values it doesn't always.虽然 SQL 会自动转换某些值,但并非总是如此。 In addition c# .net library also looks for some conversion etc. before sending the SQL statement.此外,c# .net 库在发送 SQL 语句之前还会查找一些转换等。 To fix appropriately use parameters that are data typed to the database type in the SQL table.要适当地修复使用数据类型为 SQL 表中的数据库类型的参数。 To fix it simply in the statement figure out your data types and fix the '' single quotes appropriately.要在语句中简单地修复它,找出您的数据类型并适当地修复 '' 单引号。

PS the people trying to help you in the comments were being nice and telling you the professional way of keeping your job in the future when you graduate after fixing this issue. PS,那些试图在评论中帮助你的人很好,并告诉你在解决这个问题后毕业时保持工作的专业方式。

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

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