当前位置:编程学习 > C#/ASP.NET >>

数据适配器的简单问题

我用数据适配器 DataAdapter.fill(dataset), 数据库中某列为datetime型,精确到毫秒,但是用fill方法加载到datatable里面后,精确到了秒,怎么样才能精确到毫秒呢?? --------------------编程问答-------------------- 再插入时延迟 --------------------编程问答-------------------- 没有明白什么意思。。 --------------------编程问答-------------------- 数据已经在datatable里面了,是精确到毫秒的,但是fill方法加载到datatable里面后,毫秒被省略了,精确到了秒,有什么方法可以不省略毫秒 --------------------编程问答-------------------- 数据已经在数据库的表里面了,是精确到毫秒的,但是fill方法加载到datatable里面后,毫秒被省略了,精确到了秒,有什么方法可以不省略毫秒 --------------------编程问答--------------------
引用 3 楼 yeqiancheng 的回复:
数据已经在datatable里面了,是精确到毫秒的,但是fill方法加载到datatable里面后,毫秒被省略了,精确到了秒,有什么方法可以不省略毫秒
 public DataTable GetDate(string where)
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "";
            SqlCommand cmd = new SqlCommand();
            DataTable dt = new DataTable();
            SqlDataReader dr = null;
            cmd.CommandText = where;
            cmd.Connection = cn;
            using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(dr);
            }
            if (dt.Rows.Count != 0)
            {
                return dt;
            }
            else
            {
                return null;
            }
        }


用这种看看 --------------------编程问答-------------------- 还是精确到秒 --------------------编程问答-------------------- 先new一个DataTable,定义表结构,其中时间这一列设置类型为String。然后用数据阅读器读出数据,循环插入记录到DataTable中,注意时间这列的值要转换为String,用ToString()方法试试。 --------------------编程问答-------------------- 有什么爽快一点的方法,凭什么数据库里面是精确到毫秒,fill出来就精确到秒了 --------------------编程问答-------------------- 你直接看数据库的话它是只显示到秒
不过你通过select出来还是可以看到毫秒的,你可以绑定后在把格式转换下,显示毫秒就可以显示出来了

create table ceshi([id] int,[time] datetime)
select getdate()--2011-02-17 15:03:06.733--
insert into ceshi([id],[time]) values(2,convert(datetime,'2011-02-17 15:03:06.733'))
select * from ceshi
/*
查询出的结果
1 2011-02-17 15:03:39.123
2 2011-02-17 15:03:06.733
*/
<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate><table></HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%#Eval("ID") %></td>
                <td><%#Eval("Time","{0:yyyy-MM-dd HH:mm:ss:fff}") %></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>


    public void Bind()
    {
        using(SqlConnection conn = new SqlConnection(@"..."))
        {
            conn.Open();
            string str = "select * from ceshi";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(str, conn);
            da.Fill(dt);
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }
    }
--------------------编程问答-------------------- ToString("yyyy-MM-dd HH:mm:ss.fff") 
convert(verchar(100),getdate(),121)就把毫秒存入了记录 --------------------编程问答-------------------- 数据库里面是毫秒,读出来是秒,
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,