black-han 2016-06-06 03:22 采纳率: 100%
浏览 1491
已采纳

C#中将字符串格式转换成日期格式,该怎么做?求解!

这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class upd_article : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null || Session["password"] == null)
{
Response.Redirect("login.aspx");
}
if (!IsPostBack)
{
datashows();
}
}
protected void datashows()
{
conn.Open();
String strsql = "select news_bigclass.bigclassid,bigclassname,newid,title,newsfrom,newstime,writer,hit from news_article inner join news_bigclass on news_article.bigclassid=news_bigclass.bigclassid";
SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "wenzhan");
GridView1.DataSource = ds.Tables["wenzhan"].DefaultView;
GridView1.DataBind();
conn.Close();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    datashows();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    datashows();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    conn.Open();
    string uid = GridView1.DataKeys[e.RowIndex].Value.ToString();
    GridViewRow row = GridView1.Rows[e.RowIndex];
    TextBox tb1, tb2, tb3,tb4,tb5,tb6;
    tb1 = (TextBox)(row.Cells[0].Controls[0]);
    tb2 = (TextBox)(row.Cells[1].Controls[0]);
    tb3 = (TextBox)(row.Cells[2].Controls[0]);
    tb4 = (TextBox)(row.Cells[3].Controls[0]);
    tb5 = (TextBox)(row.Cells[4].Controls[0]);
    tb6 = (TextBox)(row.Cells[5].Controls[0]);
    string strsql = "update news_article inner join news_bigclass on news_article.bigclassid=news_bigclass.bigclassid set title='" + tb1.Text + "',bigclassname=" + Convert.ToInt32(tb2.Text) + ",writer='" + tb3.Text + "',newsfrom='" + tb4.Text + "',hit='" + tb5.Text + "',newstime='" +Convert .ToDateTime (tb6 )+ "' where news_article.newid=" + uid;
    SqlCommand comm = new SqlCommand(strsql, conn);
    comm.ExecuteNonQuery();
    conn.Close();

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    conn.Open();
    string uid = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string strSql = "delete from news_article where newid=" + uid;
    SqlCommand comm = new SqlCommand(strSql, conn);
    comm.ExecuteNonQuery();
    conn.Close();
    datashows();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    this.GridView1 .PageIndex = e.NewPageIndex;
    GridView1.DataBind();

    TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
    tb.Text = (GridView1.PageIndex + 1).ToString();

    //GridView1.PageIndex = e.NewPageIndex;
    //datashows();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "go")
    {
        try
        {
            TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
            int num = Int32.Parse(tb.Text);
            GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
            GridView1_PageIndexChanging(null, ea);
        }
        catch { }
    }
}

}
我在数据库中设置的日期格式是datetime类型
下面是图片
图片说明

  • 写回答

3条回答

  • 毛豆乄 2016-06-07 11:25
    关注

    convert.toDateTime(time.Text)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?