它在我在学校使用的计算机上运行,但是当我将它转移到我的个人笔记本电脑时,它突然出现错误。
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);
关于c# - 将日期/时间转换为字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50421954/