将DataTable列的Null值分配给c#中的int变量

时间:2022-09-27 23:10:15

I have null value in datatable and looking to put this null value in integer variable.

我在datatable中有null值,并希望将此null值放在整数变量中。

SqlConnection con = new SqlConnection("Initial Catalog=test;Data Source=test;user id=sa;password=****;");
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from table1", con);

da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
    **int? ii = dt.Rows[i];**
    if (ii == null)
    {
        test.Text = ii.ToString() + "Test";
    }
}

2 个解决方案

#1


1  

You have several fundamental errors in your code. Below are some of the errors explained, and in the end of the answer is suggestion how to rewrite your code.

您的代码中有几个基本错误。下面是一些解释的错误,并在答案的最后建议如何重写您的代码。


You are trying to get data from row, and now from a cell. Data is stored in row cells:

您正尝试从行中获取数据,现在从单元格中获取数据。数据存储在行单元格中:

var row = dt.Rows[i]; // row contains several columns, you have to get from specific column
var ii = dt.Rows[i]["iiColumnName"];

You are using method of null object. If ii is null and you try to call it's method, an exception will be thrown and application will crash

您正在使用null对象的方法。如果ii为null并且您尝试调用它的方法,则会抛出异常并且应用程序将崩溃

if (ii == null)
{
    test.Text = ii.ToString() + "Test"; // This will throw NullReferenceException
}

You are checking to see if for value existence by comparing to null. Cell value is never null in DataTable. Value is instead DBNull.Value. Hence, you check if value is set in one of two following ways:

您正在通过比较null来检查是否存在值。 DataTable中的单元格值永远不会为空。值是DBNull.Value。因此,您可以检查是否通过以下两种方式之一设置值:

if (ii is DBNull) { /* do something */ }
if (ii == DBNull.Value) { /* do something */ }

Code which does something you want (hopefully) looks something like this:

做你想要的代码(希望如此)看起来像这样:

SqlConnection con = new SqlConnection("Initial Catalog=test;Data Source=test;user id=sa;password=****;");
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from table1", con);

da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
    DataRow row = dt.Rows[i];
    int? ii;
    if (row["iiColumnName"] is DBNull)
        ii = null;
    else
        ii = (int)row["iiColumnName"];

    if (ii != null)
        test.Text = ii.ToString() + "Test";
    else
        test.Text = "ii is not set";
}

#2


-1  

Casting it to an int?

将它转换为int?

int? ii = dt.Rows[i] as int?;

If the dt.Rows[i] value is not an int, ii will be null if not it will be the integer value stored in dt.Rows[i].

如果dt.Rows [i]值不是int,则ii将为null,否则它将是存储在dt.Rows [i]中的整数值。

#1


1  

You have several fundamental errors in your code. Below are some of the errors explained, and in the end of the answer is suggestion how to rewrite your code.

您的代码中有几个基本错误。下面是一些解释的错误,并在答案的最后建议如何重写您的代码。


You are trying to get data from row, and now from a cell. Data is stored in row cells:

您正尝试从行中获取数据,现在从单元格中获取数据。数据存储在行单元格中:

var row = dt.Rows[i]; // row contains several columns, you have to get from specific column
var ii = dt.Rows[i]["iiColumnName"];

You are using method of null object. If ii is null and you try to call it's method, an exception will be thrown and application will crash

您正在使用null对象的方法。如果ii为null并且您尝试调用它的方法,则会抛出异常并且应用程序将崩溃

if (ii == null)
{
    test.Text = ii.ToString() + "Test"; // This will throw NullReferenceException
}

You are checking to see if for value existence by comparing to null. Cell value is never null in DataTable. Value is instead DBNull.Value. Hence, you check if value is set in one of two following ways:

您正在通过比较null来检查是否存在值。 DataTable中的单元格值永远不会为空。值是DBNull.Value。因此,您可以检查是否通过以下两种方式之一设置值:

if (ii is DBNull) { /* do something */ }
if (ii == DBNull.Value) { /* do something */ }

Code which does something you want (hopefully) looks something like this:

做你想要的代码(希望如此)看起来像这样:

SqlConnection con = new SqlConnection("Initial Catalog=test;Data Source=test;user id=sa;password=****;");
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from table1", con);

da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
    DataRow row = dt.Rows[i];
    int? ii;
    if (row["iiColumnName"] is DBNull)
        ii = null;
    else
        ii = (int)row["iiColumnName"];

    if (ii != null)
        test.Text = ii.ToString() + "Test";
    else
        test.Text = "ii is not set";
}

#2


-1  

Casting it to an int?

将它转换为int?

int? ii = dt.Rows[i] as int?;

If the dt.Rows[i] value is not an int, ii will be null if not it will be the integer value stored in dt.Rows[i].

如果dt.Rows [i]值不是int,则ii将为null,否则它将是存储在dt.Rows [i]中的整数值。