如何通过经典的asp将空值传递给sql server表中的列 [英] How to pass null value to a column in sql server table through classic asp

查看:48
本文介绍了如何通过经典的asp将空值传递给sql server表中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......



我的表格中有一列,数据类型为datetime。如果我从我的asp应用程序传递空字符串,该列已将默认值设置为1/1/1900。但是,如果我传递空值意味着它应该存储空值。我知道如何将null值传递给asp.net中的列。在asp.net中,我们可以将其作为DBNull.value传递。但我不知道如何通过我的经典asp页面传递null值。任何人都可以帮忙....



谢谢...

Hi everybody...

I have a column in my table with datatype as datetime. If Im passing empty string from my asp application, the column has been taking the default value as 1/1/1900. But if I pass empty value means it should store null value. I know how to pass null value to a column in asp.net. In asp.net we can pass it as DBNull.value. But Im not knowing how to pass null value through my classic asp page. Can anybody please help....

Thanks...

推荐答案

这是一个很晚回答,但我有这个问题,这些答案都没有。 DBNull在Classic ASP中不存在,并且必须有一种方法将SQL的DateTime字段设置为null(因为如果你从一开始就没有设置它们那么它们已经为空)。 br />


我花了大约半个小时在Google搜索结果页面上搜索这个,最后找到了它!我想在此发布以供将来参考,因为我觉得它非常相关。



首先,您可以使用SQL查询将字段更改为null,在这种情况下你只需使用字符串null。



但是我更喜欢使用ASP的内置ADODB.Recordset对象来进行SQL查询查找记录及其AddNew / Update / Delete方法来修改数据库。我是一个顽固的JS粉丝,所以我当然认为eval是邪恶的,并且用SQL查询做同样的事情对我来说是错误的。所以我需要一种不同的方法将记录设置为null。



我找到了解决方案!它就像在VBScript中使用变量Null一样简单。 Notepad ++并没有突出显示它所以我认为它不会起作用,而且我认为Nothing是VBScript的null版本,其中没有(没什么)不起作用。你也不想投Null,只是传入Null。



这就是我的代码看起来像(注意我写的是学习,所以我没有创建许可系统或任何东西):



This is a pretty late response, but I had this very problem and neither of these answers were right. DBNull doesn''t exist in Classic ASP, and there has to be a way to set SQL''s DateTime fields to null (because if you don''t set them from the beginning then they''re already null).

I spent about half an hour searching for this going through pages of Google results, and finally found it! I wanted to post it here for future reference because I think it''s pretty relevant.

First, you can use an SQL query to change a field to null, and you just use the string "null" in that case.

However I prefer using ASP''s built-in ADODB.Recordset object to make an SQL query to find the records, and its AddNew/Update/Delete method to modify the database. I''m a die hard JS fan, so of course I believe eval is evil, and doing the same thing with SQL queries feels wrong to me. So I needed a different way to set records to null.

And I found the solution! It''s as simple as using the variable "Null" in VBScript. Notepad++ didn''t highlight it so I thought it wouldn''t work, plus I thought "Nothing" was VBScript''s version of null, which (Nothing) didn''t work. You don''t want to cast Null either, just pass in Null.

Here''s what my code looks like (note I''m writing this to learn, so I didn''t create a permission system or anything):

<!--#include virtual="/adovbs.inc"-->
<%
Dim id
id = Request("ID")
if id <> "" and Request.Form("eventName") <> "" then
  Dim fields
  Dim values
  fields = Array("eventName", "eventDate")
  if Request.Form("eventDate") <> "" then
    values = Array(Request.Form("eventName"), CDate(Request.Form("eventDate"))
  else
    values = Array(Request.Form("eventName"), Null)
end if
Dim item
Set item = Server.CreateObject("ADODB.RecordSet")
item.open "select top 1 * from calendar where id = " & id, "DSN=Calendar.dsn", , adLockOptimistic, adCmdText
if not item.EOF then
  item.Update fields, values
end if
%>





这里'它来自哪里(即使提问者决定使用SQL查询更新选项>。>)...

< a href =http://forums.devx.com/showthread.php?45891-Insert-Empty-Date> http://forums.devx.com/showthread.php?45891-Insert-Empty-Date [ ^ ]


亲爱的朋友,



我想我们不能将null值传递给数据类型字段datetime。

从应用程序端或SQL端更好,

第一次检查

应用程序结束

如果年份(column_value)='''1900''那么

str_Val =没有找到日期

其他

str_Val = column_value

结束如果



SQL结束

选择年份时的情况(column_value)='' 1900''然后''没有找到日期''其他column_value结束



希望这样做。



问候,

AP
Dear Friend,

I think so we can not pass null value to the datatype field datetime.
Better from application side or sql end,
1st check
Application End
if year(column_value)=''1900'' then
str_Val="No date found"
else
str_Val=column_value
end if

SQL End
Select case when year(column_value)=''1900'' then ''No date found'' else column_value end

Hope this works.

Regards,
AP


将值存储到数据库时使用DBNull值。因此,只要您根据asp页面的输入决定何时使用该值,就可以使用它。例如,如果在日期字段之前有一个复选框,指示是否存在值,则该复选框可用作代码中的条件,无论是使用DBNull.Value还是字段中的值。



另外我会从列中删除默认值,因为如果要在数据库中使用空值,默认值为1/1/1900会使事情变得更复杂也意味着数据库中没有日期值。
The DBNull value is used when you store the value to the database. So you can use it as long as you decide when to use that value n your code based on the input it the asp page. For example if you have a check box before a date field indicating if there is a value or not, that check box could be used as a criteria in your code whether to use DBNull.Value or the value in the field.

Also I''d remove the default from the column since if you are going to use null values in the database a default of 1/1/1900 would make things more complicated if it also means that there is no date value in the database.


这篇关于如何通过经典的asp将空值传递给sql server表中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆