简体   繁体   English

从数据库中检索整数值

[英]Retrieve integer value from database

I would like to know how to get an Integer value from a database and put it into a TextBox .我想知道如何从数据库中获取Integer值并将其放入TextBox

I know how to get a VARCHAR value from a database, which looks like this:我知道如何从数据库中获取VARCHAR值,如下所示:

public static string email;
public static string Email
{
    get { return email; }
    set { email = value; }
}

Then, at the database code:然后,在数据库代码处:

if (dr.Read())
{
    Email = dr["email"].ToString();
}

But how do I retrieve an int ?但是如何检索int

You can use this:你可以使用这个:

Convert.ToInt32(dr["age"]); 

or maybe或者可能

dr.GetInt32(i); // i: The index of the field to find.

This link may be useful. 链接可能有用。 :) :)

You can use:您可以使用:

var result = dr.Field<int?>("intCol");
txtField.Text = result;

You should not use static for your model class.你不应该为你的模型类使用static

public class YourModel
{
    public string Email { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM