How to use the varchar data type in c#?

后端 未结 3 571
时光取名叫无心
时光取名叫无心 2021-01-22 23:24

I have this piece of code and I want to make it varchar not string. What is the appropriate syntax to do that in c#

 string emri = row[\"Nome\"].ToString();
         


        
3条回答
  •  悲&欢浪女
    2021-01-22 23:41

    In .NET there is no type named "varchar", the closest thing you got is string.

    I'm assuming there is a reason you're asking, and I'm guessing you're having some problems with the code you've posted in the question, so let me try to guess what that is, and tell you how to solve it.

    First, since varchar doesn't exist, the code as you've posted it looks fine, on the surface.

    However, there is a problem, you don't show what row is, but if it is one of the usual culprits, you're going to run into problems with null marks from the database.

    A null mark in the database in the corresponding column will not be returned as null to your .NET code, instead you're going to get back a DBNull.Value value.

    Here's what I would write instead:

    string emri;
    if (row["Nome"] == DBNull.Value)
        emri = null; // or String.Empty if you don't like null values
    else
        emri = Convert.ToString(row["Nome"]);
    

提交回复
热议问题