SqlDataReader.GetString and sqlnullvalueexception

前端 未结 4 1181
庸人自扰
庸人自扰 2020-12-03 13:38

I am new to C#. I was executing some select queries from database tables using System.Data.SqlClient classes. I got sqlnullvalueexception while executing some select query.

4条回答
  •  遥遥无期
    2020-12-03 14:09

    The code you posted is fine. You could also do something like that :

    value = r[n] as string;
    

    If the value in the database is null, r[n] will return DBNull.Value, and the cast to string will return null.

提交回复
热议问题