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个回答

convert.toDateTime(time.Text)

tb6修改为tb6.Text

phone_m2_note
black-han 怎么输出?
3 年多之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复phone_m2_note: 输出下tb6.Text,是什么
3 年多之前 回复
phone_m2_note
black-han 我改了也是出现这个问题
3 年多之前 回复
phone_m2_note
black-han 不行啊!
3 年多之前 回复

to_date('" + tb6.Text + " 00:00:00','yyyy-mm-dd hh24:mi:ss')"

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!