当文本框的值为空时,我试图停止在表中输入默认值日期“01/01/1900”。我不需要在表单上验证空文本框就可以了 谢谢
using (SqlConnection con = new SqlConnection(WebConfigurationManager
.ConnectionStrings["Molecular"].ConnectionString))
{
con.Open();
using (SqlCommand sc = new SqlCommand(@"Insert into ClinicalFollowUp (MBID, Diagnosis,
DateLastSeen, DateofDeath ) values(@MBID, Upper(@Diagnosis),
Convert (date, @DateLastSeen , 103), Convert (date, @DODeath, 103);", con))
{
sc.Parameters.AddWithValue("@MBID", txtMBID1.Text);
sc.Parameters.AddWithValue("@Diagnosis", txtDiagnosis.Text);
// Date Textbox
sc.Parameters.AddWithValue("@DateLastSeen", txtDateLastSeen.Text);
// Date Textbox
sc.Parameters.AddWithValue("@DODeath", txtDateDeath.Text);
}
con.close();
}
最佳答案
我个人会这样做。
static public object ToDbNullableDate(this string s)
{
DateTime d;
var ok = DateTime.TryParse(s, out d);
return ok ? d : DbNull.Value;
}
然后在你的代码中:
sc.Parameters.AddWithValue("@DateLastSeen", txtDateLastSeen.Text.ToDbNullableDate());
关于c# - 当文本框值为空时使用 C# 停止在表中插入空值日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42838027/