在类的成员变量(全局变量)中,DateTime的默认值为: 0001-1-1 0:00:00
注:局部变量必须赋值才能使用
继承于System.ValueType的全是值(Struct)类型的: System.ArgIterator System.Boolean System.Byte System.Char System.Collections.DictionaryEntry System.Collections.Specialized.BitVector32 System.Collections.Specialized.BitVector32.Section System.Configuration.Assemblies.AssemblyHash System.Data.OracleClient.OracleBinary System.Data.OracleClient.OracleBoolean System.Data.OracleClient.OracleDateTime System.Data.OracleClient.OracleMonthSpan System.Data.OracleClient.OracleNumber System.Data.OracleClient.OracleString System.Data.OracleClient.OracleTimeSpan System.Data.SqlTypes.SqlBinary System.Data.SqlTypes.SqlBoolean System.Data.SqlTypes.SqlByte System.Data.SqlTypes.SqlDateTime System.Data.SqlTypes.SqlDecimal System.Data.SqlTypes.SqlDouble System.Data.SqlTypes.SqlGuid System.Data.SqlTypes.SqlInt16 System.Data.SqlTypes.SqlInt32 System.Data.SqlTypes.SqlInt64 System.Data.SqlTypes.SqlMoney System.Data.SqlTypes.SqlSingle System.Data.SqlTypes.SqlString System.DateTime System.Decimal System.Diagnostics.CounterSample System.Diagnostics.SymbolStore.SymbolToken System.Double System.Drawing.CharacterRange System.Drawing.Color System.Drawing.Point System.Drawing.PointF System.Drawing.Rectangle System.Drawing.RectangleF System.Drawing.Size System.Drawing.SizeF System.EnterpriseServices.BOID System.EnterpriseServices.XACTTRANSINFO System.Enum System.Guid System.Int16 System.Int32 System.Int64 System.IntPtr System.IO.WaitForChangedResult System.Reflection.Emit.EventToken System.Reflection.Emit.FieldToken System.Reflection.Emit.Label System.Reflection.Emit.MethodToken System.Reflection.Emit.OpCode System.Reflection.Emit.ParameterToken System.Reflection.Emit.PropertyToken System.Reflection.Emit.SignatureToken System.Reflection.Emit.StringToken System.Reflection.Emit.TypeToken System.Reflection.InterfaceMapping System.Reflection.ParameterModifier System.Runtime.InteropServices.ArrayWithOffset System.Runtime.InteropServices.BIND_OPTS System.Runtime.InteropServices.BINDPTR System.Runtime.InteropServices.CONNECTDATA System.Runtime.InteropServices.DISPPARAMS System.Runtime.InteropServices.ELEMDESC System.Runtime.InteropServices.ELEMDESC.DESCUNION System.Runtime.InteropServices.EXCEPINFO System.Runtime.InteropServices.FILETIME System.Runtime.InteropServices.FUNCDESC System.Runtime.InteropServices.GCHandle System.Runtime.InteropServices.HandleRef System.Runtime.InteropServices.IDLDESC System.Runtime.InteropServices.PARAMDESC System.Runtime.InteropServices.STATSTG System.Runtime.InteropServices.TYPEATTR System.Runtime.InteropServices.TYPEDESC System.Runtime.InteropServices.TYPELIBATTR System.Runtime.InteropServices.VARDESC System.Runtime.InteropServices.VARDESC.DESCUNION System.Runtime.Serialization.SerializationEntry System.Runtime.Serialization.StreamingContext System.RuntimeArgumentHandle System.RuntimeFieldHandle System.RuntimeMethodHandle System.RuntimeTypeHandle System.SByte System.Security.Cryptography.DSAParameters System.Security.Cryptography.RSAParameters System.Single System.Threading.LockCookie System.Threading.NativeOverlapped System.TimeSpan System.TypedReference System.UInt16 System.UInt32 System.UInt64 System.UIntPtr System.Void System.Web.UI.WebControls.FontUnit System.Web.UI.WebControls.Unit System.Windows.Forms.BindingMemberInfo System.Windows.Forms.DataGridCell System.Windows.Forms.LinkArea System.Windows.Forms.Message
在我的一个程序里遇到这样一个问题? 在数据库中的某个字段类型为 datetime 页面上对应该字段的为一个text文本输入框,意思是输入时间。 string strId =txtId.Text.Trim(); string strName=txtName.Text.Trim(); string strPwd=txtPwd.Text.Trim(); string strExpiry=txtTime.Text.Trim(); //时间 System.Data.SqlClient.SqlParameter []parmCustomers = new SqlParameter[3]; parmCustomers[0] = new SqlParameter( "@C_Id", strId ); parmCustomers[1] = new SqlParameter( "@Name", strName ); parmCustomers[2] = new SqlParameter( "@Pwd", strPwd ); parmCustomers[3] = new SqlParameter("@Date",strExpiry);//如果现文本里没有输入时间
SqlServerDatabase obj = new SqlServerDatabase(); if ( obj.RunProc( "proc_AddUser", parmCustomers ) == 1 ) // 添加成功 { Response.Write("<script type='text/javascript'>alert('Add Success!')</script>"); } 上段程序当然可以添加成功, 问题是当txtTime.Text什么都没输入的时候,数据库中的这个字段仍然会存储 1900-01-01 00:00:00.000 于是我就在parmCustomers[3] = new SqlParameter("@Date", " " )写入空字符串 或是 null ,可问题插入后数据库里还是显示1900-01-01
以下是解决办法: 于是加了判断: //注 数据库里时间字段要设置永许为空
string strExpiry=this.txtTime.Text.Trim();
System.Data.SqlClient.SqlParameter []parmCustomers = new SqlParameter[3]; parmCustomers[0] = new SqlParameter( "@C_Id", strId ); parmCustomers[1] = new SqlParameter( "@Name", strName ); parmCustomers[2] = new SqlParameter( "@Pwd", strPwd ); if(strExpiry.ToString()=="") { parmCustomers[3] = new SqlParameter("@Date",DBNull.Value);//如果文本框的时间为空的话就吧 strExpiry 改为 DBNull.Value 就OK了 } else { parmCustomers[3] = new SqlParameter("@Date",strExpiry);//有值时 } SqlServerDatabase obj = new SqlServerDatabase(); if ( obj.RunProc( "proc_AddUser", parmCustomers ) == 1 ) // 添加成功 { Response.Write("<script type='text/javascript'>alert('Add Success!')</script>"); } 如果是Sql语句直接插入的话 insert into AddUser (name,pwd)values('test','123') date字段 就不要写入到插入的Sql语句里 这样数据库里的值就为空了。。。
-------------------------------------无敌分割线----------------------------------------- DateTime是结构体,他不能直接被赋值成 null, 我建议是赋值成以下的方法 DateTime s = default( DateTime ); 或是 DateTime s = DateTime.MinValue;
或是
DateTime s=DateTime.MaxValue; //DateTime最大值为:9999-12-31 23:59:59
比较的时候直接if(s == DateTime.MinValue;)// 到了显示的时候,判断一下,如果是s == DateTime.MinValue 就可以在数据库传参的时候赋值为DBNull.value
|
请发表评论