C#/MySQL - 从数据库中获取日期

标签 c# mysql date

在我的数据库中,我有一行日期类型。我想在我的 C# 程序中显示这些日期,但我在编写查询时遇到了问题。目前我尝试使用:

public string getAge(string Name) 
    {
        connection con = new connection(); //The object for the class with the connection string 
        con.conopen(); //opens the connection
        string Age = "";
        MySqlCommand cmd_getAge = new MySqlCommand("Select 'Age' from profile where Name = '" + Name + "';", con.con); 
        MySqlDataReader Reader = cmd_getAge.ExecuteReader();
        if (Reader.HasRows)
        {
            try
            {
                while (Reader.Read())
                {
                    Age = Reader.GetString(0);
                }
            }
            finally
            {
                Reader.Close();
                con.conclose();
            }
        }
        return Age;
    }

VS 返回我的结果只是“年龄”,也没有错误。

如果相关,我在 Windows 7 上使用 Visual Studio Ultimate 2013。

最佳答案

问题是您选择的是特定值“年龄”。如果您删除 Age 两边的引号,将改为选择该列的值。

public string getAge(string Name) 
    {
        connection con = new connection(); //The object for the class with the connection string 
        con.conopen(); //opens the connection
        string Age = "";
        MySqlCommand cmd_getAge = new MySqlCommand("Select Age from profile where Name = '" + Name + "';", con.con); 
        MySqlDataReader Reader = cmd_getAge.ExecuteReader();
        if (Reader.HasRows)
        {
            try
            {
                while (Reader.Read())
                {
                    Age = Reader.GetString(0);
                }
            }
            finally
            {
                Reader.Close();
                con.conclose();
            }
        }
        return Age;
    }

关于C#/MySQL - 从数据库中获取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25450037/

相关文章:

php - jQuery datepicker,选择日期后添加一定天数

c# - 如何为发布和 Debug模式 C# 设置不同的全局变量值

c# - 用c#异步处理Bitmap对象

MySQL变量改变,不生效

javascript - 是否有获取区域设置字符串格式的 UTC 日期的函数?

java - 检查 Date 对象是否在过去 24 小时内发生

c# - 避免使用托管语言的分支

c# - FileStreamResult 无法识别 Asp.Net Core 2.2 中的多个 http 范围

php - 带有设置变量的 mysqli 准备语句

java - servlet 发生 mySql 列未找到错误