验证mssql中的字段是否具有值或null [英] Validate field in mssql if has value or null

查看:201
本文介绍了验证mssql中的字段是否具有值或null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

con1.Open();
SqlCommand cmd = new SqlCommand("Select first from TransactionsInfo where name=@name and tid=@tid", con1);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@tid", txtId.Text);
SqlDataReader rdr = cmd.ExecuteReader();

if (!DBNull.Value.Equals(rdr["first"]))
{
    MessageBox.Show("First payment already exists in database .", "Alert !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//save
}




我想验证我在TransactionsInfo表中的字段"first"是否具有值或为null
提前输入:)




I want to validate if my field "first" in TransactionsInfo table has value or null
ty in advance :)

推荐答案

//check for rows 
if (rdr.HasRows)
{            
    // for reading one row
    if (rdr.Read())
    {
        if(dr1["first"] != DBNull.Value)
       {
            MessageBox.Show("First payment already exists in database .", "Alert !", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       }else
       {
          // dr1["first"] has DBNull
       }
    }
}else
{
   // no rows returned by the reader, show message....
}


这篇关于验证mssql中的字段是否具有值或null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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