本文介绍了如何使用带有c#的asp.net在datetime字段中存储sql server 2008中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Iam使用asp.net和c#,Ms Sql Server 2008 当iam传递日期值然后工作很好,如果我不通过它给我的错误的价值你可以帮助我,我必须做出更改 以下是我的表,存储过程,代码如下 表测试 ========== packcode varchar(50) phone varchar( 10) startdate datetime endtime datetime 程序测试 == ================ CREATE PROCEDURE testing( @ packcode varchar ( 50 ), @ phone varchar ( 10 ), @ startdate datetime , @ enddate datetime ) as begin IF EXISTS (选择 * 来自测试其中 packcode = @ packcode) update 测试设置 phone = @ phone,startdate = @ startdate,enddate = @ enddate 其中 packcode = @ packcode 其他 插入 进入测试(packcode ,phone,startdate,enddate) values ( @ packcode , @phone , @ startdate , @ enddate ) end code ==== using System.Configuration; 使用 System.Data; 使用 System.Data.SqlClient; 使用 System.Data.SqlTypes; public class 状态:System.Web.Services.WebService { string StrConnection = ConfigurationManager.ConnectionStrings [ Application]。ConnectionString; [WebMethod] public string HelloWorld(){ 返回Hello World; } [WebMethod] public string packStatus(string packCode,string phoneNo,DateTime startdate,DateTime enddate) { // sqldatenul = DBNull.Value; string strResult =状态失败..; SqlConnection MyCon =新的SqlConnection(StrConnection); SqlCommand MyCommand = new SqlCommand(测试,MyCon; MyCommand.CommandType = CommandType.StoredProcedure; MyCommand.Parameters.AddWithValue(@ packcode,Convert.ToString(packCode)); MyCommand.Parameters.AddWithValue(@ phone,Convert.ToString(phoneNo)); if(startdate == null) { MyCommand.Parameters.Add(@ startdate,SqlDbType.DateTime).Value = SqlDateTime.Null; } else { MyCommand.Parameters .AddWithValue(@ startdate,(startdate).ToUniversalTime()); } if(enddate == null) {$ / $ MyCommand.Parameters.Add(@ enddate,SqlDbType.DateTime).Value = SqlDateTime.Null; } else { MyCommand.Parameters.AddWithValue(@ enddate,(enddate).ToUniversalTime()); } 试试 { MyCon.Open(); if(MyCommand.ExecuteNonQuery()!= 0) { strResult =状态已更改; } strResult =状态已更改; } catch (例外情况) { strResult = ex.ToString(); } MyCon.Close(); 返回strResult; } } 当iam没有将值传递给这个网络服务时它给了我错误 DateTimeStyles样式) at System.Convert.ToDateTime(String value,IFormatProvider provider) at System.String.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ChangeType(对象值,类型conversionType,IFormatProvider提供者) 在System.Web.Services.Protocols.ScalarFormatter.FromString(字符串值,类型类型) ---内部结束异常堆栈跟踪--- 在System.Web.Services.Protocols.ScalarFormatter.FromString(字符串值,类型类型) 在System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) 在System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest请求) 在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()解决方案 首先在你的SP本身你分配参数被接受为null ex: @ startdate datetime = null, 然后在你的网络服务中,只要你在那时添加那个参数就可以检查条件。 if (startDate!= ) MyCommand.Parameters.AddWithValue( @ startdate,startdate); 然后在页面上检查条件 if (startDate!= null ) ---你的插入查询,包含startdate 值 .. else ---使用startdate 的插入查询是 null ... hi,Iam Using asp.net with c#, Ms Sql Server 2008 when iam passing the date value then its working fine if iam not passing the value its giving me error can you help me where i have to make changes below is my table, stored procedure, code as followsTable Test==========packcode varchar(50)phone varchar(10)startdate datetimeendtime datetimeProcedure testing==================CREATE PROCEDURE testing(@packcode varchar(50),@phone varchar(10),@startdate datetime,@enddate datetime)as begin IF EXISTS(select * from Test where packcode=@packcode) update Test set phone=@phone,startdate=@startdate,enddate=@enddate where packcode=@packcode else insert into Test(packcode,phone,startdate,enddate)values(@packcode ,@phone ,@startdate ,@enddate ) endcode ====using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;public class status : System.Web.Services.WebService { string StrConnection = ConfigurationManager.ConnectionStrings["Application"].ConnectionString;[WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string packStatus(string packCode, string phoneNo, DateTime startdate, DateTime enddate) { //sqldatenul = DBNull.Value; string strResult = " Status Failed .."; SqlConnection MyCon = new SqlConnection(StrConnection); SqlCommand MyCommand = new SqlCommand("testing", MyCon); MyCommand.CommandType = CommandType.StoredProcedure; MyCommand.Parameters.AddWithValue("@packcode", Convert.ToString(packCode)); MyCommand.Parameters.AddWithValue("@phone", Convert.ToString(phoneNo)); if (startdate == null) { MyCommand.Parameters.Add("@startdate", SqlDbType.DateTime).Value = SqlDateTime.Null; } else { MyCommand.Parameters.AddWithValue("@startdate", (startdate).ToUniversalTime()); } if (enddate == null) { MyCommand.Parameters.Add("@enddate", SqlDbType.DateTime).Value = SqlDateTime.Null; } else { MyCommand.Parameters.AddWithValue("@enddate", (enddate).ToUniversalTime()); } try { MyCon.Open(); if (MyCommand.ExecuteNonQuery() != 0) { strResult = "Status Changed"; } strResult = "Status Changed"; } catch (Exception ex) { strResult = ex.ToString(); } MyCon.Close(); return strResult; } }when iam not passing value to this webservice it is giving me errorDateTimeStyles styles) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.String.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type) --- End of inner exception stack trace --- at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type) at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 解决方案 first of all in your SP itself you assign the parameters is accepted to nullex:@startdate datetime=null,and then in your web service it self whenever you add that parameter at that time you can check the condition.if(startDate!="") MyCommand.Parameters.AddWithValue("@startdate", startdate);and then in page wise you check the condition if(startDate!=null) --- your insert query with the startdate value..else --- your inserting query with startdate is null... 这篇关于如何使用带有c#的asp.net在datetime字段中存储sql server 2008中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 02:43