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 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错
  • ¥15 服务器上的网站安装php5.6版本
  • ¥15 ModuleNotFoundError: No module named 'torch.utils._import_utils' 是缺少什么
  • ¥15 请大咖一起探索iptv 直播源的hls通过反向代理解密
  • ¥100 寻找技术员 云闪付tn转h5输入卡号付款的链接 重酬!
  • ¥100 科大讯飞语音唤醒词,unbuntu环境,报错