private void ShowSchoolView1(string areaid)
{ this.GridMessage.Visible = false;
SqlConnection conn = new SqlConnection("server=172.16.1.248;uid=sa;password='$eszaq!23w';database=JxtFee");
conn.Open();
SqlCommand command = new SqlCommand("Ptj_FeeMobPermon_sel", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@seldate", SqlDbType.VarChar, 20);
command.Parameters.Add("@areaid", SqlDbType.Int);
command.Parameters.Add("@schid", SqlDbType.Int);
command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01");
command.Parameters["@areaid"].Value = areaid;
command.Parameters["@schid"].Value = DBNull.Value;
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
this.ShowSchoolView.DataSource = ds;
this.ShowSchoolView.DataBind(); }
在执行的时候会出现从字符串向 datetime 转换时失败的问题
sda.Fill(ds);
这行出错,这是怎么回事该怎么解决
{ this.GridMessage.Visible = false;
SqlConnection conn = new SqlConnection("server=172.16.1.248;uid=sa;password='$eszaq!23w';database=JxtFee");
conn.Open();
SqlCommand command = new SqlCommand("Ptj_FeeMobPermon_sel", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@seldate", SqlDbType.VarChar, 20);
command.Parameters.Add("@areaid", SqlDbType.Int);
command.Parameters.Add("@schid", SqlDbType.Int);
command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01");
command.Parameters["@areaid"].Value = areaid;
command.Parameters["@schid"].Value = DBNull.Value;
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
this.ShowSchoolView.DataSource = ds;
this.ShowSchoolView.DataBind(); }
在执行的时候会出现从字符串向 datetime 转换时失败的问题
sda.Fill(ds);
这行出错,这是怎么回事该怎么解决
这里错了 把类型改成datetime
command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"); 设置类型是varchar,给的值却是datetime
改为
command.Parameters.Add("@seldate", SqlDbType.VarChar, 20);
command.Parameters["@seldate"].Value = this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01";
或
command.Parameters.Add("@seldate", SqlDbType.DateTime, 20);
command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01");
command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01").ToShortDateString();
改为command.Parameters.Add("@seldate", SqlDbType.DateTime,8);
command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01");
没必要强制转换
command.Parameters["@seldate"].Value = this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01";
请问这个的值this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01" 是多少???请检查是否有空格之类的,务必使其格式满足 "yyyy-MM-dd" 还有你上面的明明定义的是Varchar类型的数据,下面却要转DATATIME,,~~~~!
ALTER PROCEDURE [dbo].[Ptj_FeeMobPermon_Sel]
@seldate varchar(20),
@areaid int,
@schid int=null
AS
BEGIN
if @areaid is not null and @schid is null
begin
--指定地区
select a.schid,b.schname,
case when a.mobtype=1 then '移动' when a.mobtype=2 then '联通' when a.mobtype=3 then '小灵通' end as mobtype,
sum(a.mobnum) as mobnum,sum(a.sucnum) as sucnum,sum(a.failnum) as failnum
from dbo.Tj_FeeMobPermon a,dbo.Fee_School b where a.schid=b.schid
and datediff(mm,a.countmonth,@seldate)=0 and left(a.areaid,4)=@areaid
group by a.schid,b.schname,a.mobtype
end
else if @schid is not null
begin
--指定学校
select a.classid,b.classname,
case when a.mobtype=1 then '移动' when a.mobtype=2 then '联通' when a.mobtype=3 then '小灵通' end as mobtype,
sum(a.mobnum) as mobnum,sum(a.sucnum) as sucnum,sum(a.failnum) as failnum
from dbo.Tj_FeeMobPermon a,dbo.Fee_class b
where datediff(mm,a.countmonth,@seldate)=0 and a.schid=@schid and a.classid=b.classid
group by a.classid,b.classname,a.mobtype
end
END
存储过程里是 @seldate varchar(20),
外面
command.Parameters.Add("@seldate", SqlDbType.***
command.Parameters["@seldate"].Value = ***
里面
@seldate ***
***要统一!~