how to use DATA_FORMAT() in mysql with C# Asp.net?

Asked

Viewed 36 times

0

I need to take the data of the dates and pass them to the format dd/mm/AAAA, because it comes from Mysql AAAA-mm-dd to then move to a databinder that is in a Repeater on the front, someone knows how to use the Data_Format()? or another resource to change the? format in my mysql all those dates are with string, I don’t know if this information is relevant or not.

private void CarregarPacientes()
{
    string query = @"select pac_id, pac_nome, pac_contato, pac_unidade, pac_dataMoldagem, pac_proteseProva, pac_entregaPac from pacientes";
    
    DataTable dt = new DataTable();
    try
    {
        MySqlDataAdapter da = new MySqlDataAdapter(query, Conexao.Connection);
        da.Fill(dt);
        //Popular o Repeater
        rptPacientes.DataSource = dt;
        rptPacientes.DataBind();
    }
    catch (Exception ex)
    {
        lblMsgPaciente.Text = "Falha: " + ex.Message;
    }
}

out of the Databinder

<%# DataBinder.Eval(Container.DataItem, "pac_dataMoldagem")%>
<%# DataBinder.Eval(Container.DataItem, "pac_proteseProva") %>
<%# DataBinder.Eval(Container.DataItem, "pac_entregaPac") %>
  • The answer came true?

1 answer

1

To format information of any format and in the case for Date (day, month and year) exists in this same method Eval an overload where such an expected format can be made {0:dd/MM/yyyy} as shown in the excerpt:

<body>
    <form id="form1" runat="server">
        <div>
          <asp:Repeater ID="RptList" runat="server">
            <ItemTemplate>
              <%# DataBinder.Eval(Container.DataItem, "name") %>
              <%# DataBinder.Eval(Container.DataItem, "created","{0:dd/MM/yyyy}") %>
              <hr />
            </ItemTemplate>
          </asp:Repeater>
        </div>
    </form>
</body>

where created is a field in the type table date. Reference: https://stackoverflow.com/questions/7621373/how-do-i-change-the-date-format-of-a-databinder-eval-in-asp-net/7621831

In the text Custom date and time format string has the table that explains the types of formats allowed for date or date and time:

+----------------------------------------------+--------------------------------------+
| "MM/dd/yyyy"                                 |  07/21/2007                          |
| "dddd, dd MMMM yyyy"                         |  Saturday, 21 July 2007              |
| "dddd, dd MMMM yyyy HH:mm"                   |  Saturday, 21 July 2007 14:58        |
| "dddd, dd MMMM yyyy hh:mm tt"                |  Saturday, 21 July 2007 03:00 PM     |
| "dddd, dd MMMM yyyy H:mm"                    |  Saturday, 21 July 2007 5:01         |
| "dddd, dd MMMM yyyy h:mm tt"                 |  Saturday, 21 July 2007 3:03 PM      |
| "dddd, dd MMMM yyyy HH:mm:ss"                |  Saturday, 21 July 2007 15:04:10     |
| "MM/dd/yyyy HH:mm"                           |  07/21/2007 15:05                    |
| "MM/dd/yyyy hh:mm tt"                        |  07/21/2007 03:06 PM                 |
| "MM/dd/yyyy H:mm"                            |  07/21/2007 15:07                    |
| "MM/dd/yyyy h:mm tt"                         |  07/21/2007 3:07 PM                  |
| "MM/dd/yyyy HH:mm:ss"                        |  07/21/2007 15:09:29                 |
| "MMMM dd"                                    |  July 21                             |
| "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"     |  2007-07-21T15:11:19.1250000+05:30   |
| "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"        |  Sat, 21 Jul 2007 15:12:16 GMT       |
| "yyyy'-'MM'-'dd'T'HH':'mm':'ss"              |  2007-07-21T15:12:57                 |
| "HH:mm"                                      |  15:14                               |
| "hh:mm tt"                                   |  03:14 PM                            |
| "H:mm"                                       |  5:15                                |
| "h:mm tt"                                    |  3:16 PM                             |
| "HH:mm:ss"                                   |  15:16:29                            |
| "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"             |  2007-07-21 15:17:20Z                |
| "dddd, dd MMMM yyyy HH:mm:ss"                |  Saturday, 21 July 2007 15:17:58     |
| "yyyy MMMM"                                  |  2007 July                           |
+----------------------------------------------+--------------------------------------+

Your code also has problems, an ideal code follows:

void LoadRepeater()
{
    string sqlStr = "SELECT * FROM tests ORDER BY id";
    string connStr = "Server=localhost;Database=dbs;Uid=root;Pwd=senha;";
    using (MySqlConnection connect = new MySqlConnection(connStr))
    using (MySqlCommand command = new MySqlCommand(sqlStr, connect))
    {
        connect.Open();
        using (MySqlDataReader reader = command.ExecuteReader())
        {
            DataTable items = new DataTable();
            items.Load(reader);
            RptList.DataSource = items;
            RptList.DataBind();
        }
    }
}

use a class Mysqldataadapter has several negative aspects, one of them is performance the other is that this class does a lot of things that are not needed.

  • Thanks for commenting on my post! &#xA;string connStr = "Server=localhost;Database=dbs;Uid=root;Pwd=senha;";&#xA; has already in another part of my code, unfortunately it has not changed the format in my list

  • How this date is stored @Felipebellini?

  • comes straight from the bank, as varchar(10)

  • @Felipebellini then guy could not be so, if the data type is date in the bank has to be date, and so your question instead of bringing real information, brought a prank. In your SQL it looks like this: https://www.w3schools.com/sql/func_mysql_date.asp type: select pac_id, pac_nome, pac_contato, pac_unidade, date(pac_dataMoldagem), pac_proteseProva, pac_entregaPac from pacientes

  • It is worth remembering @Felipebellini that the date in the bank always puts date What I told you to do is a gambiarra created by you OK! My solution is correct when the type is date, got it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.