我从MySQL获取日期值,我正在尝试格式化它,因此它没有任何空格或显示时间。当我把它作为一个字符串带进来时,我得到了完整的日期和时间,所以我想我会尝试我在另一篇文章中读到的convert.TODateTime格式,但它似乎并没有起作用。
最终目标是将日期显示为只有没有空格或破折号的数字,例如21/02/1982将是21021982。
我得到的错误是'无法在结果中找到指定的列s5'
有人可以指出我正确的方向吗
public MySqlDataReader RunQueryNATFiles27(string query, string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11, string s12, string s13, string s14, string s15, string s16, string s17, string s18, string s19, string s20, string s21, string s22, string s23, string s24, string s25, string s26, string s27, string pathName, string NATFile)
{
MySqlConnection conDataBase = new MySqlConnection(connString);
MySqlCommand cmdDatabase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader;
StreamWriter sw = new StreamWriter(@pathName);
conDataBase.Open();
myReader = cmdDatabase.ExecuteReader();
while (myReader.Read())
{
string NameHolder1 = (myReader[s1].ToString());
string NameHolder2 = (myReader[s2].ToString());
string NameHolder3 = (myReader[s3].ToString());
string NameHolder4 = (myReader[s4].ToString());
var NameHolder5 = Convert.ToDateTime(myReader["s5"]).ToString("dd/MM/yyyy");
// NameHolder5 = String.Format("{0:M/d/yyyy}", NameHolder5);
MessageBox.Show(NameHolder5);
string NameHolder6 = (myReader[s6].ToString());
string NameHolder7 = (myReader[s7].ToString());
string NameHolder8 = (myReader[s8].ToString());
string NameHolder9 = (myReader[s9].ToString());
string NameHolder10 = (myReader[s10].ToString());
string NameHolder11 = (myReader[s11].ToString());
string NameHolder12 = (myReader[s12].ToString());
string NameHolder13 = (myReader[s13].ToString());
string NameHolder14 = (myReader[s14].ToString());
string NameHolder15 = (myReader[s15].ToString());
string NameHolder16 = (myReader[s16].ToString());
string NameHolder17 = (myReader[s17].ToString());
string NameHolder18 = (myReader[s18].ToString());
string NameHolder19 = (myReader[s19].ToString());
string NameHolder20 = (myReader[s20].ToString());
string NameHolder21 = (myReader[s21].ToString());
string NameHolder22 = (myReader[s22].ToString());
string NameHolder23 = (myReader[s23].ToString());
string NameHolder24 = (myReader[s24].ToString());
string NameHolder25 = (myReader[s25].ToString());
string NameHolder26 = (myReader[s26].ToString());
string NameHolder27 = (myReader[s27].ToString());
答案 0 :(得分:0)
如果您想以ddMMYYYY
格式查询和检索日期,您可以使用DATE_FORMAT
函数在MySQL端实际处理此日期,例如
SELECT
Student_Number,
first_name,
DATE_FORMAT(DOB, '%d%m%Y') AS DOB
FROM STUDENTREGISTER;
这会产生01022014
作为输出。 DATE_FORMAT
返回一个字符串,因此您的C#代码应该是DOB
列的字符串。