HTTPWebRequest won't INSERT null Value
本文关键字:INSERT null Value won HTTPWebRequest | 更新日期: 2023-09-27 18:17:37
我目前正在为一些在线业务做一个Webform。有很大的问题与HTTPWebrequest。函数UpdateUserData()有一些用于更新数据库的典型信息。姓名,电子邮件,itc。当用户没有在[Birthday]单元格中输入任何内容并且它的DateTime(顺便说一下,它是空的)值为空时,问题就出现了。
下面是我添加参数的方法: private void AddParam(string Key, object Value)
{
if (Value == null)
Value = string.Empty;
ParamStringBuilder.Append(string.Format("{0}={1}&", Key, HttpUtility.UrlEncode(Value.ToString())));
}
我是这样发送WebRequest的:
string _URL = URIBuilder(Action);
HttpWebRequest httpWebRequest = HttpWebRequest.Create(_URL) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Headers.Add("X-Forwarded-For", HttpContext.Current.Request.UserHostAddress);// this is optional to log the original client ip address
// add culture info of calling client
if (SendBrowserCulture)
{
if (HttpContext.Current.Request.Headers["Accept-Language"] != null) // this is optional to access the api with the original culture settings
httpWebRequest.Headers.Add("Accept-Language", HttpContext.Current.Request.Headers["Accept-Language"].ToString());
}
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(ParamStringBuilder);
}
// reset paramstringbuilder
ParamStringBuilder.Clear();
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
APIResponse _Response = new APIResponse(httpWebResponse.GetResponseStream());
return _Response;
我怀疑当SQL得到URLencoded命令"Birthday=null"时,它将其读取为
INSERT 'null' INTO Table.Birthday
当然这对它来说没有意义,所以它用'0001-01-01'填充
我希望我说得够清楚了。我很乐意再解释一些。提前感谢您的帮助!
确保处理WebRequest的服务器端代码也将生日日期作为可空日期。您还需要确保数据库中的Birthday字段可以接受NULL。
如果这没有帮助,请附上您的服务器端处理代码和您的数据库模式。