簡體   English   中英

將日期/時間轉換為字符串時出錯

[英]There is an error converting date/time to string

它在我在學校使用的計算機上運行,​​但是當我將其轉移到個人筆記本電腦上時,突然出現了錯誤。

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ConnectionString);
        SqlCommand cmd = new SqlCommand("INSERT INTO Student VALUES (@stu_fname, @stu_lname, @Address, @Phone, @Email, @DateOfBirth, @UserName, @PassWord, @DateAdded)", con);
        con.Open();
        cmd.Parameters.AddWithValue("@stu_fname", TxtFN.Text);
        cmd.Parameters.AddWithValue("@stu_lname", TxtLN.Text);
        cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
        cmd.Parameters.AddWithValue("@Phone", TxtPhone.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@DateOfBirth", TxtDateOfBirth.Text);
        cmd.Parameters.AddWithValue("@UserName", TxtUsername.Text);
        cmd.Parameters.AddWithValue("@PassWord", TxtPassword.Text);
        cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Success boi");
        con.Close();

在此處輸入圖片說明

嘗試!

cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

或將字符串轉換為日期。

 DateTime dateOfbirth = DateTime.ParseExact(TxtDateOfBirth.Text, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

cmd.Parameters.AddWithValue(“ @ DateAdded”,。TxtDateOfBirth.value.ToString(“ yyyy-MM-dd”)); //添加所需的格式。 //將其保留在TxtDateOfBirth.value系統的日期和時間格式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM