HTTPWebRequest不会INSERT null Value

时间:2013-06-07 16:36:24

标签: c# sql httpwebrequest

我目前正在为一些在线业务开发Webform。 遇到HTTPWebrequest的大问题。 函数UpdateUserData()具有一些典型的DataBase更新信息。姓名,电子邮件,itc。 问题出现在用户未在[Birthday]单元格中输入enything及其DateTime(顺便为Nullable)时值为Null。

以下是我添加参数的方法:

    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”填充它

我希望我足够清楚。很高兴再解释一下。

请提前获得帮助!

1 个答案:

答案 0 :(得分:0)

确保处理WebRequest处理的服务器端代码也将生日日期作为Nullable DateTime。您还需要确保数据库中的Birthday字段可以接受NULL。

如果这没有帮助,请附上服务器端处理代码和数据库架构。