数据格式问题,插入查询失败来自C#应用程序 [英] Problem with data formats , insert query fails from C# application

查看:118
本文介绍了数据格式问题,插入查询失败来自C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#windows应用程序的保存按钮中使用以下代码。



cmd =新的SqlCommand(插入sman_targets(sman_cd,from_dt,to_dt,target_amt) )values('+ comboBox1.Text +','+ DT1.Text +','+ DT2.Text +','+ textBox2.Text +'),con);



cmd.ExecuteNonQuery();



DT1,DT2是c#windows窗体中(短格式)的日期时间选择器控件。





我在sql server数据库的表列中使用了datetime数据时间。

显示以下错误。似乎有一些问题是

的数据格式。



类型'System.Data.SqlClient.SqlException'的未处理异常发生在System.Data.dll



附加信息:将char数据类型转换为日期时间数据类型会导致日期时间值超出范围。



plz给出解决方案。



感谢和问候



madhu



我的尝试:



我试过通过更改数据时间选择器控件的格式属性来更改插入语句等的双引号/单引号



但是出现了同样的错误。

I am using the following code in save button of c# windows application.

cmd = new SqlCommand("insert into sman_targets(sman_cd,from_dt,to_dt,target_amt) values('"+comboBox1.Text +"','"+DT1.Text+"','"+DT2.Text+"','"+textBox2.Text+"')",con);

cmd.ExecuteNonQuery();

DT1,DT2 are datetime picker controls in (short format) in c# windows form.


I have used "datetime" datatime for the table columns in sql server database.

on button click it shows the following errors. It seems some problem is there
with data formats.

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

plz give the solution.

thanks and regards

madhu

What I have tried:

I tried by changing the double quotes / singles quotes of insert statement etc
also by changing the format property of datatime picker control.
But the same error is appearing.

推荐答案

从不这样做开始!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Start by not doing that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。您是否定期进行备份,不是吗?



修复整个应用程序,验证用户输入(并将其转换为适当的.NET数据类型)和您的问题会在同一时间消失。而不是您的数据库。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your whole app, validate user inputs (and convert them to appropriate .NET data types) and your problem will disappear at the same time. Rather than your database.


cmd = new SqlCommand("insert into sman_targets(sman_cd,from_dt,to_dt,target_amt) values('"+comboBox1.Text +"','"+DT1.Text+"','"+DT2.Text+"','"+textBox2.Text+"')",con);



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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

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

PHP:SQL注入 - 手册 [ ^ ]

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

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]


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[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


在您完成并阅读如何正确创建SQL语句之后;你应该得到类似于以下protocode的东西。您可能需要/想要使用变量来修剪空格并验证DateTime值。



After you go through and read how to properly create an SQL statement; you should end up with something similar to the following protocode. You may need to need/want to work with the variables going into to trim spaces and validate the DateTime value.

cmd = new SqlCommand("INSERT sman_targets(sman_cd,from_dt,to_dt,target_amt) VALUES (@comboBox1, @DT1, @DT2, @textBox2)", con);

cmd.Parameters.AddWithValue("@comboBox1", comboBox1.Text);
cmd.Parameters.AddWithValue("@DT1", (DateTime)DT1.Text);
cmd.Parameters.AddWithValue("@DT2", (DateTime)DT2.Text);
cmd.Parameters.AddWithValue("@textBox2", textBox2.Text);

cmd.ExecuteNonQuery();


这篇关于数据格式问题,插入查询失败来自C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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