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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作