繁体   English   中英

在 Visual Studio 2013 中使用 C# 将文本转换为日期格式并将其保存在 SQL Server 2014 中

[英]Convert text into date format in Visual Studio 2013 using C# and save it in SQL Server 2014

我想在 Visual Studio 2013 中使用 C# 将文本转换为日期格式。 唉! 我在其中遇到了困难。 我还想将这些数据保存在 SQL Server 2014 中。

我的代码是

protected void Button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText= "insert into Optical values('"+date.Text+"','"+name.Text+"','"+guardian.Text+"','"+gender.Text+"','"+age.Text+"','"+address.Text+"','"+occupation.Text+"','"+telephone.Text
                +"','"+sph.Text+"','"+cyl.Text+"','"+axis.Text+"','"+prism.Text+"','"+lsph.Text+"','"+lcyl.Text+"','"+laxis.Text+"','"+lprism.Text+"','"+note.Text+"')";
            cmd.ExecuteNonQuery();

            con.Close();
        }

我只想将日期的文本更改为日期格式。 请任何人指导我,我该怎么做?

DateTime.TryParseExact() 可能是更好的选择,但您可以使用正则表达式来确保输入的文本以正确的格式开始。 您可以在表单上的占位符中显示所需的格式(“格式必须为 yyyy-MM-dd ”)。 要保存在 SQL Server 中,该字符串可能类似于“yyyy-MM-dd HH:mm:ss”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM