C#检查方法的结果是否为空

时间:2017-07-04 21:27:32

标签: c#

>>> test_df03.select('label').distinct().orderBy('label').show()
+-----+
|label|
+-----+
|    1|
|    2|
|    3|
+-----+

我如何检查Database.Insert方法是否返回null

1 个答案:

答案 0 :(得分:2)

不是最好的,但下面的代码将帮助您检查空值

bool found;

using (SqlCommand cmd = new SqlCommand("SELECT * FROM userdata WHERE name = @name", connection))
{
    connection.Open();

    cmd.Parameters.AddWithValue("@name", name);

    SqlDataReader reader = cmd.ExecuteReader();


    if(reader.read())
        found = true
    else
        found = false;

    connection.Close();
}