帮助我按日期在所选国家/地区搜索 [英] help me to search by date in the select statmen'
问题描述
查看更多:代码
我已经在C#中创建了一个程序,当我按日期搜索时会显示错误
我需要在SQL Server中按日期搜索,可以吗?还是可以在SELECT语句中使用convert函数
请帮帮我
该表已覆盖列date_outlaid
erorr显示器
是
语法erorr将varchar值aug20 2011 12:00 Am转换为数据类型为int的列
代码是
See more: Code Hi,
I''ve created a program in C#, when I search by date an error is displayed
I need to search by date in SQL Server, can I do that? Or can I use the convert function in the SELECT statement
Please help me
the table is outlaid the column date_outlaid
the erorr display
is
syntax erorr convertingthe varchar value aug20 2011 12:00 Am to column of data type int
the code is
private void display_outliad_f(string x)
{
//creat adabter to execute the query
SqlDataAdapter ad = new SqlDataAdapter(x, con1);
//create comand buillder
SqlCommandBuilder cBulider = new SqlCommandBuilder(ad);
//create data table to hold the result query
DataTable dTable = new DataTable();
//fill the data table
ad.Fill(dTable);
//to sycron between table and grv
BindingSource Bsource = new BindingSource();
//set bsouce to the datasource
Bsource.DataSource = dTable;
dataGridView1.DataSource = Bsource;
}
人民要问的是
the statmants ask is
private void textBox4_TextChanged(object sender, EventArgs e)
{
display_outliad_f("select * from outlaid where date_outlaid="+date );
}
请帮助我更改此代码
help me please to chang this code
推荐答案
尝试以下操作:
Try this:
private void textBox4_TextChanged(object sender, EventArgs e)
{
display_outliad_f("select * from outlaid where date_outlaid=\""+date.ToToShortDateString() + """ );
}
错误提示无法将varchar转换为int.
date_outlaid
列的数据类型是什么?
The error says the cannot convert varchar to int.
What is the datatype ofdate_outlaid
column
只需添加到DaveKerr所说的内容中:SQL Server总是想要ISO格式的日期:yyyy-MM-dd,因此他提供的解决方案可能会有所帮助,但可能需要进行更改,因为ToShortDateString方法以适合您当前语言环境的格式返回日期.
相反,可以考虑使用
Just to add to what DaveKerr said: SQL server always wants dates in ISO format: yyyy-MM-dd, so the solution he gave may help, but probably will need changes, as the ToShortDateString method returns the date in the format appropriate to your current locale.
Instead, consider using
date.ToString("yyyy-MM-dd")
或(最好)将其用作SQL参数-这将意味着对display_outliad_f
方法进行大量更改,但是从长远来看,这是一个非常好的主意.
所有这些可能仍然无法满足您的要求-假定您将date_outlaid
作为DateTime存储在SQL数据库中.
Or, (preferably) using it as a SQL Parameter instead - this would mean changing your display_outliad_f
method quite a bit, but is a very good idea in the long run.
All of this may still not do what you want - it assumes that you are storing date_outlaid
as a DateTime in your SQL database.
这篇关于帮助我按日期在所选国家/地区搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!