当未选择日期时,WPF Datepicker将1/1/1900添加到数据库
我有一个包含多个日期选择器的WPF应用程序。并非所有的Datepickers都会选择一个日期。如果未选择日期,则会将1/1/1900的值插入SQL数据库。我在C#中尝试了一些逻辑,但它在任何地方都没有得到
。如果没有选择日期,我不希望SQL数据库更新该值。
I have a WPF app that contains several datepickers. Not all the Datepickers will always have a date selected. When a date is not selected a value of 1/1/1900 is being inserted into the SQL database. I have tried some if logic within my C# but it's not getting me anywhere. When a date is not selected, I don't want the SQL database to get updated for that value.
请帮助!
using (SqlConnection sqlcon2 = new SqlConnection(sqlDb))
{
sqlcon2.Open();
string insertStatement = "INSERT INTO tblTracking " +
"(Status,Package,Version,Package_Name,ActionPlan_ProgressNotes,BUILD_Install,CERT_Install,MOCK_Install,TRAIN_Install,PROD_Install,Owner_Requester,Priority,Date_Added,Meaningful_Use,FLASH,CWxPI,Notes,Dependency_Package) " +
"Values (@Status, @Package, @Version, @Package_Name,@ActionPlan_ProgressNotes, @BUILD_Install, @CERT_Install, @MOCK_Install, @TRAIN_Install, @PROD_Install, @Owner_Requester, @Priority, @Date_Added, @Meaningful_Use, @FLASH, @CWxPI, @Notes, @Dependency_Package)";
SqlCommand insertCommand = new SqlCommand(insertStatement, sqlcon2);
insertCommand.Parameters.AddWithValue("@Status", cmbStatus1.Text);
insertCommand.Parameters.AddWithValue("@Package", tbPackage.Text);
insertCommand.Parameters.AddWithValue("@Version", tbVersion.Text);
insertCommand.Parameters.AddWithValue("@Package_Name", tbName.Text);
insertCommand.Parameters.AddWithValue("@ActionPlan_ProgressNotes", tbPlan.Text);
if(dpBUILD.Text !="")
{
insertCommand.Parameters.AddWithValue("@BUILD_Install", dpBUILD.Text);
}
else
insertCommand.Parameters.AddWithValue("@BUILD_Install", "");
if (dpCERT.Text !="")
{
insertCommand.Parameters.AddWithValue("@CERT_Install", dpCERT.Text);
}
else
insertCommand.Parameters.AddWithValue("@CERT_Install", "");
if (dpMOCK.Text !="")
{
insertCommand.Parameters.AddWithValue("@MOCK_Install", dpMOCK.Text);
}
else
insertCommand.Parameters.AddWithValue("@MOCK_Install", "");
if (dpTRAIN.Text !="")
{
insertCommand.Parameters.AddWithValue("@TRAIN_Install", dpTRAIN.Text);
}
else
insertCommand.Parameters.AddWithValue("@TRAIN_Install", "");
if (dpPROD.Text !="")
{
insertCommand.Parameters.AddWithValue("@PROD_Install", dpPROD.Text);
}
else
insertCommand.Parameters.AddWithValue("@PROD_Install", "");
insertCommand.Parameters.AddWithValue("@Owner_Requester", tbOwner.Text);
insertCommand.Parameters.AddWithValue("@Priority", tbPriority.Text);
insertCommand.Parameters.AddWithValue("@Date_Added", dpDateAdded.Text);
insertCommand.Parameters.AddWithValue("@Meaningful_Use", tbMeaningfulUse.Text);
insertCommand.Parameters.AddWithValue("@FLASH", tbFlash.Text);
insertCommand.Parameters.AddWithValue("@CWxPI", tbCWxPI.Text);
insertCommand.Parameters.AddWithValue("@Notes", tbNotes.Text);
insertCommand.Parameters.AddWithValue("@Dependency_Package", tbDependency.Text);
insertCommand.ExecuteNonQuery();
sqlcon2.Close();
}
您好    Michael Knope,b
>>并非所有的Datepickers都会有一个日期选择。如果未选择日期,则会将1/1/1900的值插入SQL数据库。我已经在C#中尝试了一些逻辑,但它不是
让我到任何地方。如果未选择日期,我不希望SQL数据库更新该值。
从您的描述中,您不希望在未选择日期时将值插入SQL数据库?
您可以检查datepicker的值并避免执行SQL语句。
Hi Michael Knope,
>>Not all the Datepickers will always have a date selected. When a date is not selected a value of 1/1/1900 is being inserted into the SQL database. I have tried some if logic within my C# but it's not getting me anywhere. When a date is not selected, I don't want the SQL database to get updated for that value.
From your description, you don't want to insert a value into SQL database when a date is not selected?
You can check the datepicker's value and avoid executing SQL statements.
var selectvalue = DatePicker.SelectedDate;
if(selectvalue==null)
{
MessageBox.Show("not select");
}
else
{
MessageBox.Show(dateppi.SelectedDate.ToString());
}