c# - 将日期/时间转换为字符串时出错

标签 c# mysql database

它在我在学校使用的计算机上运行,​​但是当我将它转移到我的个人笔记本电脑时,它突然出现错误。

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();

enter image description here

最佳答案

尝试一下!

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/

相关文章:

c# - 在 XML 中更新和添加新标签

c# - 如何替换 AutoMapper 9.0 中的 `AutoMapper.Mapper.Initialize` 静态调用?

php - 左连接上的重复查询

mysql - Laravel 中的条件 `orderBy` 或 `DB::Raw` inside `orderBy`

mysql - MyIsam 针对多个 %term% 进行全文搜索

c# - 使用 Azure 表存储在多个分区上获取多个实体

c# - 绕过 silverlight tcp 端口限制

c# - 比较 Entity Framework 模型

php - 数据库设计一对一关系

sql-server - 将数据从一个 MS SQL Server 复制/克隆到另一个