如何在oledb数据适配器中将日期转换为字符串格式 [英] How to convert date into string format in oledb data adapter

查看:323
本文介绍了如何在oledb数据适配器中将日期转换为字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨开发者,



i从本地.csv文件中恢复数据并将其显示到gridview中

i将日期转换为datetime格式请建议hpw将数据检索为字符串格式,这样我就可以在csv数据中实现搜索功能

谢谢





hi devs,

i am retriving the data from local .csv file and displaying that into gridview
i am getting date into datetime format please suggest hpw to retrieve data into string format so it will useful for me to implement searching functionality in csv data
thanks


string filename = path;
         FileInfo file = new FileInfo(filename);
         string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + file.DirectoryName + "\";Extended                           Properties='text;HDR=Yes;FMT=Delimited(,)';";
         OleDbConnection con = new OleDbConnection(ConnectionString);
         if (txtSearch.Text == string.Empty)
         {

             OleDbCommand cmd = new OleDbCommand(string.Format("SELECT * FROM [" + file.Name + "]"), con);
             cmd.Parameters.AddWithValue("@p1", txtSearch.Text);
             try
             {
                 con.Open();
                 OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
                 DataTable dt = new DataTable("CSVTable");
                 oda.Fill(dt);
                 GridView1.DataSource = dt;
                 GridView1.DataBind();
             }
             catch (Exception)
             {
                 GridView1.DataSource = null;
                 GridView1.DataBind();
             }

推荐答案

转换它的范围/目的是什么,因为你不能在dataadaptar中这样做,因此,在数据表中,您可以搜索日期列并更改数据类型。



例如





What is the scope/purpose of converting it because, you can't do it in the dataadaptar, therefore inside the datatable you can search for the date column and change the datatype.

Like for example


foreach (DataColumn cl in dt.Columns)
               {
                 if (cl.ColumnName.ToUpper().Contains("DATE"))
                        cl.DataType = typeof(DateTime);
                }







或者你可以用data.ToString()/ Convert转换数据.ToString(数据)。



希望我理解你并帮助你解决你所面临的问题。




Or you can convert the data with data.ToString() / Convert.ToString(data).

Hope i have understood you and would help you solve the problem you are facing.


这篇关于如何在oledb数据适配器中将日期转换为字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆