C# 如何从数据库中检索日期值到asp.net中的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18454164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:15:10  来源:igfitidea点击:

How to retrieve date value from database to textbox in asp.net

c#asp.netsql

提问by jefferyleo

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MAGConnString"].ConnectionString);
    SqlCommand com = new SqlCommand("SELECT * from MAGMember",conn);
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    da.Fill(ds, "MAGMember");
    txtFirstName.Text = ds.Tables["MAGMember"].Rows[0]["Firstname"].ToString();
    txtLastName.Text = ds.Tables["MAGMember"].Rows[0]["Lastname"].ToString();
    txtGender.Text = ds.Tables["MAGMember"].Rows[0]["Gender"].ToString();
    txtDOB.Text = ds.Tables["MAGMember"].Rows[0]["DOB"].ToString();
    txtPassword.Text = ds.Tables["MAGMember"].Rows[0]["Password"].ToString();
    txtEmail.Text = ds.Tables["MAGMember"].Rows[0]["Email"].ToString();
}

This is code to retrieve the data from database to show in textbox, others value from database i can retrieve it just except the date i can't retrieve since i declare the dateofbirth as date in sqlserver.

这是从数据库中检索数据以显示在文本框中的代码,数据库中的其他值我可以检索它,除了我无法检索的日期,因为我在 sqlserver 中将 dateofbirth 声明为日期。

采纳答案by Simon Whitehead

There's a few issues with this. To begin with, you're not closing your connections and potentially leaking resources.

这有几个问题。首先,您不会关闭连接并可能泄漏资源。

Your date issue is because you probably won't be able to directly convert your SQL date to a .NET DateTime.

您的日期问题是因为您可能无法直接将 SQL 日期转换为 .NET DateTime

Take a look at DateTime.ParseExact, on MSDN here: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx. This will help you learn how to parse dates.

看看DateTime.ParseExact,在 MSDN 上:http: //msdn.microsoft.com/en-us/library/w2sa9yss.aspx。这将帮助您学习如何解析日期。

Then take a look at the Standard Date and Time format strings on MSDN here: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

然后在这里查看 MSDN 上的标准日期和时间格式字符串:http: //msdn.microsoft.com/en-us/library/az4se3k1.aspx

This will give you hints as to how exactly you will parse your date. Without some example data and an exact error there really isn't much more we can do to help you.

这将为您提供有关如何准确解析日期的提示。如果没有一些示例数据和确切的错误,我们真的无能为力来帮助您。

回答by Pir Fahim Shah

If you want to retrieve a single value from data base and show in text box using Razor in Asp.net, then this answer will guide you in detail. Lets for example we have an employee table(ID, Name,Age) and we want to retreive the name of the Employee whose ID=1.

如果您想从数据库中检索单个值并在 Asp.net 中使用 Razor 显示在文本框中,那么此答案将详细指导您。例如,我们有一个员工表(ID、姓名、年龄),我们想要检索 ID=1 的员工的姓名。

  var db = Database.Open("EmployeDataBase");
  var dbCommand = "SELECT * FROM Employee WHERE ID = '1'"; 
  var row = db.QuerySingle(dbCommand);
  var name="null";
        if(row != null) 
        {
             name = row.Name;
        }

Now using the Razor and set the value of the text box, and the text box will show the name

现在使用 Razor 并设置文本框的值,文本框将显示名称

  <input type="textbox" value=@name  name"/>