我的Windows应用程序中的数据类型问题 [英] Datatype problem in my Windows Application

查看:47
本文介绍了我的Windows应用程序中的数据类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人

我开发并创建了一个基于Windows的应用程序setup.exe。在客户端机器上安装软件后,我打开订单表格并输入一些数据,然后点击提交按钮将数据插入数据库。它抛出异常如下: -

错误: -  System.Data.SqlClient.SqlException:将char数据类型转换为datetime数据类型导致out范围的日期时间值。 
声明已被终止。





< img src =C / Users / Mashkoor / Desktop / untitled.jpg />;

虽然我在SQL Server 2005数据库中使用了'订单日期'列的datetime数据类型。在Windows应用程序中,我使用了datetimepicker控件来'订单日期'列。客户端计算机中的日期格式类似于 2013年9月11日







先生,这是我写的用于插入数据的代码片段:



 尝试 
{
string s = 插入Sales_Details值(' + comboBox1.Text + ' ,' + comboBox2.Text + ',' + textBox1.Text + ',' + textBox2.Text + ',' + textBox3.Text + ',' + dateTimePicker1.Value + ',' + dateTimePicker2.Value + ',' + textBox5.Text + ',' + textBox6.Text + ' ,' + textBox7.Text + ',' + textBox8.Text + ');
SqlConnection con = new SqlConnection(Class1.cs);
con.Open();
SqlCommand cmd = new SqlCommand(s,con);
cmd.ExecuteNonQuery();
MessageBox.Show( 信息成功保存);
textBox3.Text = ;
comboBox2.Text = ;
textBox1.Text = ;
textBox4.Text = ;
textBox2.Text = ;
dateTimePicker1.Text = ;
dateTimePicker2.Text = ;
textBox5.Text = ;
textBox6.Text = ;
textBox7.Text = ;
textBox8.Text = ;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show( 错误: + ex);
}

解决方案

因此,如果您使用的是DateTimePicker,为什么要将其转换为字符串以将其传递给SQL?



简单:因为您的代码对SQL注入攻击是开放的,因为您将字符串连接起来形成SQL命令,而不是将它们作为参数化查询传递并发送合理的,未转换的形式的价值。

所以当你写的时候

  string  cmd =   INSERT INTO MyTable(dateColumn)VALUES(' + myDateTimePicker.Value +  '); 

提供给SQL的字符串取决于系统日期和时间格式运行C#代码的PC中的设置:它可以是

  INSERT   INTO  MyTable(dateColumn) VALUES '  2013年9月11日'

或者

  INSERT   INTO  MyTable(dateColumn) ) VALUES '  11 09 2013'



  INSERT   INTO  MyTable(dateColumn) VALUES '  2013/9/11'

由于SQL通常需要:

  INSERT   INTO  MyTable(dateColumn) VALUES '   2013-09-11'

很有可能它会在某些PC上投诉,而不是在其他PC上投诉。

U.参数化查询,并将DateTime值作为DateTimes,整数作为整数传递等。


使用 DateTime 类型从客户机获取日期。

更改客户端机器中的日期格式





在区域和地区的双重碰撞中转到控制面板语言选项选择时间格式为英语(英国)



i认为这会解决你的问题


Dear All
I developed and created setup.exe a Windows Based Application. After installing the software in client machine I open the Order Form and input some data to it and clicked submit button to insert data into database. It throws an exception as like:-

Error:- System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime datatype resulted in an out-of-range datetime value.
The statement has been terminated.



<img src="C/Users/Mashkoor/Desktop/untitled.jpg" />;
While I used datetime datatype for 'Date of Order' column in SQL Server 2005 database. In windows application I used a datetimepicker control for 'Date of Order'column. The date format in the Client Machine is like 11 September 2013


[edit]
Sir, this is the code snippet which I wrote to insert data:

try
{
    string s = "insert into Sales_Details values('" + comboBox1.Text + "','" + comboBox2.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','"+dateTimePicker1.Value+"','"+dateTimePicker2.Value+"','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";
    SqlConnection con = new SqlConnection(Class1.cs);
    con.Open();
    SqlCommand cmd = new SqlCommand(s, con);
    cmd.ExecuteNonQuery();
    MessageBox.Show("Information Saved Successfully");
    textBox3.Text = "";
    comboBox2.Text = "";
    textBox1.Text = "";
    textBox4.Text = "";
    textBox2.Text = "";
    dateTimePicker1.Text = "";
    dateTimePicker2.Text = "";
    textBox5.Text = "";
    textBox6.Text = "";
    textBox7.Text = "";
    textBox8.Text = "";
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Error:"+ex);
}

解决方案

So, if you are using a DateTimePicker, why are you converting it to a string to pass it to SQL?

Simple: because your code is wide open to SQL injection attack because you are concatenating strings to form an SQL command instead of passing them as parametrized queries and sending the values in a sensible, unconverted form.
So when you write

string cmd = "INSERT INTO MyTable (dateColumn) VALUES ('" + myDateTimePicker.Value + "')";

The string presented to SQL depends on the system date and time format settings in the PC running the C# code: it could be

INSERT INTO MyTable (dateColumn) VALUES ('11 September 2013')

Or

INSERT INTO MyTable (dateColumn) VALUES ('11 09 2013')

Or

INSERT INTO MyTable (dateColumn) VALUES ('2013/9/11')

Since SQL normally expects:

INSERT INTO MyTable (dateColumn) VALUES ('2013-09-11')

there is a good chance it will complain on some PCs and not on others.
Use a parametrized query, and pass DateTime values as DateTimes, integers as integers, etc.


Use DateTime type to get the date from client machine.


change the date format in client machine


go to control panel in the double clink on regional and language option select the time format to English(united kingdom)

i think this will slove u r issue


这篇关于我的Windows应用程序中的数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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