日期转换为字符串

时间:2018-08-17 11:36:22

标签: c#

enter image description here 在我的项目中,我想输入一个日期然后保存。但这会保存今天日期和时间的确切日期。拜托,我是C#的新手。有谁能够帮助我?它们的数据类型是日期和时间,而不是DateTime,因为我需要分别存储日期和时间。

protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SignUp_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString);
            string query = "INSERT INTO Tickt (Date, Time, Pickup, DropOff,Fare, BusName) Values (@date, @time, @pick, @drop,@far,@busname)";
            con.Open();


            SqlCommand cmd2 = new SqlCommand(query, con);
            cmd2.Parameters.AddWithValue("@date",  SqlDbType.DateTime).Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            cmd2.Parameters.AddWithValue("@time", SqlDbType.DateTime).Value =DateTime.Now.ToString("HH:mm:ss");
            cmd2.Parameters.AddWithValue("@pick", SqlDbType.NVarChar).Value = tbPickUp.Text;
            cmd2.Parameters.AddWithValue("@drop", SqlDbType.NVarChar).Value = tbDropOff.Text;
            cmd2.Parameters.AddWithValue("@far", SqlDbType.Int);
            cmd2.Parameters.AddWithValue("@busname", SqlDbType.NChar).Value = tbBusName.Text;


            cmd2.ExecuteNonQuery();
            con.Close();
            LblMsgs.Text = "Registration is done sucessfully ";
            LblMsgs.ForeColor = Color.Red; 



         }
    }

0 个答案:

没有答案