如何向数据库中插入null?

发布于 2024-09-27 06:01:26 字数 282 浏览 4 评论 0原文

您好,我正在尝试根据 gridview datakeys 值在数据库列中插入 null (如果是“”,则将 null 插入数据库) 但是,我在数据库列内出现一个空格“”。

string sbcId = gvTest.DataKeys[gr.RowIndex]["myColumn"].ToString();
 insgnp.Parameters.Add(new OleDbParameter("EMPID", (sbcId==""?DBNull.Value.ToString():sbcId)));

Hi I am trying to insert null in a database column depending on a gridview datakeys value (if being "" insert null into database)
However, I am getting a space ' ' inside the database column.

string sbcId = gvTest.DataKeys[gr.RowIndex]["myColumn"].ToString();
 insgnp.Parameters.Add(new OleDbParameter("EMPID", (sbcId==""?DBNull.Value.ToString():sbcId)));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

看透却不说透 2024-10-04 06:01:26

您必须重写您的代码:

if(string.IsNullOrEmpty(sbcId))
    Parameters.Add(new OleDbParameter("EMPID", DBNull.Value)); 
else
    Parameters.Add(new OleDbParameter("EMPID", sbcId)); 

您拥有的三元 if 语句的问题是它的返回类型必须始终相同,这就是您不能使用它的原因(字符串和 DbNull.Value 不兼容)

You have to rewrite your code:

if(string.IsNullOrEmpty(sbcId))
    Parameters.Add(new OleDbParameter("EMPID", DBNull.Value)); 
else
    Parameters.Add(new OleDbParameter("EMPID", sbcId)); 

The problem with the ternary if statement that you have is that its returntype must always be the same, which is why you cannot use it (string and DbNull.Value are not compatible)

污味仙女 2024-10-04 06:01:26

使用DBNull.Value,而不是DBNull.Value.ToString。 (对于 .NET 中的其他参数集合类型,仅使用 null 也可以,但我不能 100% 确定 OleDbParameter)。

至于三级运算符。不要转换为字符串,而是将字符串转换为对象:

sbcId=="" ? DBNull.Value : (object)sbcId

Use DBNull.Value, not DBNull.Value.ToString. (With other parameter collection types in .NET just using null would also work, but I'm not 100% sure about OleDbParameter).

As for the tertiary operator. Don't cast to string, but cast the string to object:

sbcId=="" ? DBNull.Value : (object)sbcId
只为守护你 2024-10-04 06:01:26

您需要使用 DBNull.Value 而不是 DBNull.Value.ToString()

DBNull.Value.ToString() is ''

该程序给出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("'{0}'", DBNull.Value.ToString());
        }
    }
}

< img src="https://i.sstatic.net/goUIs.jpg" alt="替代文本">

You need to use DBNull.Value not DBNull.Value.ToString()

DBNull.Value.ToString() is ''

This program gives

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("'{0}'", DBNull.Value.ToString());
        }
    }
}

alt text

情深如许 2024-10-04 06:01:26

尝试下面通过删除 DBNull.Value 之后的 ToString()

string sbcId = gvTest.DataKeys[gr.RowIndex]["myColumn"].ToString();
 insgnp.Parameters.Add(new OleDbParameter("EMPID", (sbcId==""?DBNull.Value:sbcId)));

Try below by removing ToString() after DBNull.Value

string sbcId = gvTest.DataKeys[gr.RowIndex]["myColumn"].ToString();
 insgnp.Parameters.Add(new OleDbParameter("EMPID", (sbcId==""?DBNull.Value:sbcId)));
紫瑟鸿黎 2024-10-04 06:01:26

SQLString.Null 应该可以很好地完成这项工作:)

SQLString.Null should do the job just fine :)

自找没趣 2024-10-04 06:01:26

我更喜欢使用扩展方法来处理而不是多个 if 语句。这允许您考虑 null 或空字符串值。还允许它可重复使用。

    public static object GetDBNullOrValue<T>(this T val)
    {
        bool isDbNull = true;
        Type t = typeof(T);

        if (Nullable.GetUnderlyingType(t) != null)
            isDbNull = EqualityComparer<T>.Default.Equals(default(T), val);

        else if (t.IsValueType)
            isDbNull = false;

        else if (t == typeof(string) && val != null)
        {
            string temp = val as String;
            isDbNull = (temp == String.Empty);
        }

        else
            isDbNull = (val == null);

        return isDbNull ? DBNull.Value : (object)val;
    }

然后你可以使用

     Parameters.Add(new OleDbParameter("EMPID", sbcId.GetDBNullOrValue())); 

I prefer to use an extension method to handle instead of multiple if statements. This allows you to account for null or empty string values. Also allows it to be reusable.

    public static object GetDBNullOrValue<T>(this T val)
    {
        bool isDbNull = true;
        Type t = typeof(T);

        if (Nullable.GetUnderlyingType(t) != null)
            isDbNull = EqualityComparer<T>.Default.Equals(default(T), val);

        else if (t.IsValueType)
            isDbNull = false;

        else if (t == typeof(string) && val != null)
        {
            string temp = val as String;
            isDbNull = (temp == String.Empty);
        }

        else
            isDbNull = (val == null);

        return isDbNull ? DBNull.Value : (object)val;
    }

Then you could use

     Parameters.Add(new OleDbParameter("EMPID", sbcId.GetDBNullOrValue())); 
隐诗 2024-10-04 06:01:26

如果你在access数据库中插入非字符串值,你可以这样编写插入查询

Insert into tableName (column1,column2) values (NULL,NULL);

但是你想在中插入空值访问中的短文本或长文本字段您可以这样编写插入查询

Insert into tableName (column1, column2)values('','');

If you Insert a non string value in the access database you may write the insert query in this way

Insert into tableName (column1,column2) values (NULL,NULL);

But you want to insert null value in Short Text or Long Text field in access you may write the insert query in this way

Insert into tableName (column1, column2) values('','');

打小就很酷 2024-10-04 06:01:26

像这样的字符串

string value = "";

现在添加以下行

 if (value == ""){

  value = "NULL";
     }

,然后插入数据库并检查是否有空值,它们是否符合逻辑

your string like that

string value = "";

now add following line

 if (value == ""){

  value = "NULL";
     }

now insert in db and check you have null value their be logical

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文