Datetimepicker和smalldatetime

时间:2013-02-22 20:03:02

标签: c# sql sql-server tsql

我在C#中有这段代码:

label3.Text = dateTimePicker1.Value.ToString(); 
DateTime vencim = dateTimePicker1.Value; 
DataTable llenar3 = minaf.cargrid3(vencim);
BindingSource forwo = new BindingSource();
forwo.DataSource = llenar3;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = llenar3;
dataGridView1.Refresh();

cargrid3是这样的:

DataTable rcargaP = new DataTable();
SqlConnection abre1 = Tconex.GetConnection();
SqlDataAdapter da3 = new SqlDataAdapter();
SqlCommand llena5 = new SqlCommand("vencedata", abre1);
SqlParameter para1 = new SqlParameter("@venci", SqlDbType.SmallDateTime);
para1.Value = vence; llena5.Parameters.Add(para1);
llena5.CommandType = CommandType.StoredProcedure;
da3.SelectCommand = llena5;
da3.Fill(rcargaP);
return rcargaP;

,程序是这样的:

alter proc vencedata (@venci datetime)as
select   conta,vencimento,pagamento,historico,original,formpagto,planoconta,clifor 
from financeiro where vencimento >= @venci 
order by vencimento desc

datetimepicker工作,如果我在SQL Studio中编写,则proc可以工作:

vencedata '01/31/2013'

程序vencedata仅适用于此格式

vencedata '03/28/2013' (mm/dd/yyyy)

但我尝试的时候

vencedata '28/03/2013'(dd/mm/yyyy)

我也试过这个:

alter proc vencedata (@inicio smalldatetime/*,@final smalldatetime*/)as
select idconta,vencimento,pagamento,historico,original,formpagto,planoconta,clifor 
from financeiro where vencimento >= convert (varchar, @inicio,101) 

不行。

2 个答案:

答案 0 :(得分:0)

您应该以正确的格式转换字符串中的日期,然后将其作为参数传递。

试试这个。

para1.Value = String.Format("{0:d/M/yyyy HH:mm:ss}", vence);

我希望这对你有所帮助。

此致

答案 1 :(得分:0)

假设vence是一个包含dateTimePicker1选定日期的字符串,您可以试试这个:

para1.Value = vence.ToString("MM/DD/AAAA");