在我的数据库中,我有一行日期类型。我想在我的 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/