1

.aspx:

<asp:TextBox ID="txtend" runat="server"></asp:TextBox></td>

此 aspx 代码将在其文本字段中存储“日期”。

我想在 c# 中访问此文本字段,其中代码显示为如何将字符串格式转换为日期类型并被访问..?

SqlDataAdapter adapter = new SqlDataAdapter(
    "select * from Membership_det where updateDate is between "+txtstart.Text+" and "+txtend.Text+" ", 
    con);
4

6 回答 6

1

您可以使用DateTime.ParseExact对其进行转换。像这样的东西: -

DateTime.ParseExact(txtstart.Text, "yyyyMMdd", null);

附带说明:-

正如Uwe Keim在评论中指出的那样,您的代码容易出现SQL 注入

如果您使用Parameterized SQL-queries会很好。

于 2013-09-07T06:40:11.907 回答
1
    DateTime startDate;
    DateTime endDate;

    if (DateTime.TryParse(txtstart.Text, out startDate) && DateTime.TryParse(txtend.Text, out endDate))
    {
        //string n1 = DropDownList2.SelectedItem.Text;

        if (DropDownList1.SelectedItem.Text == "Membership")
        {
            SqlConnection con = new
      SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select p.Name,m.FID,m.MembNo,m.MembType,m.Validity,m.Remarks from Membership_det m INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start and @End ", con);
            adapter.SelectCommand.Parameters.Add("@Start", SqlDbType.Date).Value = startDate;
            adapter.SelectCommand.Parameters.Add("@End", SqlDbType.Date).Value = endDate;
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            con.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        log.Debug("Info: Admin viewed the membership details");
    }
于 2013-10-22T05:49:51.900 回答
0

用这个

string date = "01/08/2008";
    DateTime dt = Convert.ToDateTime(date); 

||

string date = "Sun, 17 Mar 2013 12:40:23 +0000";
var dt = DateTime.ParseExact(date, "ddd, dd MMM yyyy hh:mm:ss zzz", CultureInfo.InvariantCulture);
于 2013-09-07T06:48:08.487 回答
0

用这个 :

DateTime startDate = DateTime.ParseExact(txtstart.Text, "yyyyMMdd", null);
DateTime endDate = DateTime.ParseExact(txtend.Text, "yyyyMMdd", null);

SqlDataAdapter adapter = new SqlDataAdapter(
    "select * from Membership_det where updateDate is between "+ startDate.ToString() + " and "+ endDate.ToString() +" ", 
    con);
于 2013-09-07T06:42:39.630 回答
0

你的文本框日期时间就像“25-09-2013”​​意味着把格式类型“dd-MM-yyyy”这样,你得到字符串格式的类型。

例如:1。

var da="2013-09-25";
DateTime.ParseExact(da, "yyyy-MM-dd", 
System.Globalization.CultureInfo.CurrentCulture);

例如:2。

var da="25-09-2013";      
DateTime.ParseExact(da, "dd-MM-yyyy",
System.Globalization.CultureInfo.CurrentCulture);
于 2013-09-07T06:59:14.480 回答
0
SqlDataAdapter adapter = new SqlDataAdapter(
    "select * from Membership_det where updateDate is between '"+ Convert.ToDateTime( txtstart.Text).ToString("dd/MMM/yyyy")+"' and '"+ Convert.ToDateTime( txtend.Text).Tostring("dd/MMM/yyyy")+"' ", 
    con);
于 2013-09-07T15:19:52.670 回答