C#Sql命令帮助 [英] C# Sql command help
问题描述
我有一个sql UPDATE命令,我有一些麻烦。在我的数据库中,我有3个相似的(具有相同的规格)列,这些列包含一行数字,其中81个字符始终为81个字符长。我有一个正常的INSERT INTO命令,它可以正常工作。但是当我使用UPDATE命令更新这些时。我得到一个问题,在第一个数字后面放一个逗号:
示例(正常:987987987,有问题的代码:9,8798797)如何在没有逗号的情况下执行此操作?
以下是代码:
ki1 / 2/3是包含数字行的字符串。
I have a sql UPDATE command that i have some trouble with. In my database i have 3 similar(have the same specs) columns these columns contain a line of numbers which is 81 char always 81 char long. I have a normal INSERT INTO command which works fine. but when i use the UPDATE command to update these. I get a problem where after the first number it puts a comma:
Example(normal:987987987, with problematic code:9,8798797) how can i do it without the comma?
Here is the code:
ki1/2/3 are strings that contain the line of numbers.
OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=userstat.accdb;Persist Security Info=false;");
con1.Open();
OleDbCommand cmd1 = new OleDbCommand("UPDATE save SET mapfull="+ki1+",mapinitial="+ki2+",mapcurrent="+ki3+";", con1);
cmd1.ExecuteNonQuery();
con1.Close();
推荐答案
因为ki1 / ki2 / ki3是字符串,你 会有 引用它们。我假设你在insert语句中做了这件事而忘了这里。
但是:你不应该构建你的查询字符串无论如何,通过将值连接为文字。
相反,您应该使用SQL参数。请参阅我以前的一个答案,其中我写了一个示例性的良好实践数据库访问(那里的选择语句,但无关紧要)。我还解释了SQL参数的优点:
如何循环sql server表来创建datagridview - sql表字段匹配csv字段 [ ^ ]
编辑:代码格式有点混乱不幸的是,因为这个网站存在轻微的错误。代码中有一个< pre> -Tag,不属于那里,最后一行不再是代码块的一部分。
As ki1/ki2/ki3 are strings you would have to quote them. I assume you did that in your insert-statement and just forgot that here.
But: You shouldn't build your query-strings by concatenating the values as literals anyway.
Instead you should use SQL-Parameters. Please see one of my previous answers where I wrote an exemplary "good practice" database-access (a select-statement there but that doesn't matter). I also explain there the advantages of SQL-Parameters:
how to loop sql server table to create a datagridview - sql table field matches csv field[^]
The code-formatting is a bit messed up there, unfortunately, because of slight bugs with this website. There's a <pre>-Tag in the code that doesn't belong there and the last line isn't part of the code-block any more.
这篇关于C#Sql命令帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!