为什么我如下写参数总出错System.FormatException: 输入字符串的格式不正确。
到底什么格式的正确呀?
//这是输入参数。
mydb.MakeInParam("@购买日期",SqlDbType.DateTime,8, "2004-1-1")
/////
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) 
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/////
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) 
{
SqlParameter param;
if(Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param; }

解决方案 »

  1.   

    try
    mydb.MakeInParam("@购买日期",SqlDbType.DateTime,8, Convert.ToDateTime("2004-1-1"));
    ormydb.MakeInParam("@购买日期",SqlDbType.DateTime,8, DateTime.Parse("2004-1-1"));
      

  2.   

    因为你的"2004-1-1"是字符串而不是一个DateTime类型的值,改成这样:mydb.MakeInParam("@购买日期",SqlDbType.DateTime,8, new DateTime(2004,1,1))
      

  3.   

    把"2004-1-4"转换为日期型的,Convert.ToDateTime("2004-1-1")
      

  4.   

    楼上几个说的都不是关键上,我关键是 时间为 ""时,插不进去数据库
    数据库是允许null 的。 ""的格式不支持。我改怎么转换呢
      

  5.   

    对,日期格式不能插入"",你可以判断一下,if(datevalue=="") datavalue=null;
    即可。
      

  6.   

    if(Value==null||Value.ToString=""){
       Value = DBNull.Value;
    }