C# 4.0 如何在access中插入空日期

C# 4.0 如何在access中插入空日期,c#-4.0,ms-access-2007,C# 4.0,Ms Access 2007,我想在access数据库中插入空日期,如果用户付款选择为现金,那么支票日期应该为空。对于此im,我使用屏蔽文本框,我也使用调试器,但每次调试器转到其他条件时,它都会给我数据不匹配。这里的例外是im给我插入代码 string bank = txtbankname.Text; bank = ""; string cheque = txtchequeno.Text; cheque = ""; strin

我想在access数据库中插入空日期,如果用户付款选择为现金,那么支票日期应该为空。对于此im,我使用屏蔽文本框,我也使用调试器,但每次调试器转到其他条件时,它都会给我数据不匹配。这里的例外是im给我插入代码

string bank = txtbankname.Text;
            bank = "";
            string cheque = txtchequeno.Text;
            cheque = "";


            string billno = txtbillno.Text;
            billno = "";


            string codecreate = txtcodecreator.Text;
            codecreate = "";

            string connetionString = null;
            connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            cnn.ConnectionString = connetionString;

            string SqlString = "Insert Into Billing([FormNo],[Date],[TruckNo],[Office_Code],[Party_Code],[Party_Code1],[Location],[Supplier],[Item],[Invoice_no],[Invoice_date],[Package],[Weight],[Invest_Amount],[Percentage],[Amount],[Total_Amount],[Payment_Amount],[Payment_Type],[Bank_Name],[Cheque_No],[Cheque_Date],[Bill_No],[Bill_Date],[Code_Create]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            using (cmd = new OleDbCommand(SqlString, cnn))
            {
                cnn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@FormNo", txtformno.Text);
                cmd.Parameters.AddWithValue("@Date", txtdate.Text);
                cmd.Parameters.AddWithValue("@TruckNo", txttruck.Text);
                cmd.Parameters.AddWithValue("@Office_Code", txtofficecode.Text);
                cmd.Parameters.AddWithValue("@Party_Code", txtpartycode.Text);
                cmd.Parameters.AddWithValue("@Party_Code1", txtpartycode1.Text);
                cmd.Parameters.AddWithValue("@Location", txtlocation.Text);
                cmd.Parameters.AddWithValue("@Supplier", txtsupplier.Text);
                cmd.Parameters.AddWithValue("@Item", txtitem.Text);
                cmd.Parameters.AddWithValue("@Invoice_no", txtinvoice.Text);
                cmd.Parameters.AddWithValue("@Invoice_date", DateTime.Parse(txtmaskinvoice.Text));
                cmd.Parameters.AddWithValue("@Package", txtpackage.Text);
                cmd.Parameters.AddWithValue("@Weight", txtwieght.Text);
                cmd.Parameters.AddWithValue("@Invest_Amount", Convert.ToDouble(txtinvestamount.Text));
                cmd.Parameters.AddWithValue("@Percentage", txtpercentage.Text);
                cmd.Parameters.AddWithValue("@Amount", Convert.ToDouble(txtamount.Text));
                cmd.Parameters.AddWithValue("@Total_Amount", Convert.ToDouble(txttotalamount.Text));
                cmd.Parameters.AddWithValue("@Payment_Amount", Convert.ToDouble(txtpaymentamount.Text));
                cmd.Parameters.AddWithValue("@Payment_Type", txtpaymenttype.Text);
                if (txtbankname.Text == "")
                {
                    cmd.Parameters.AddWithValue("@Bank_Name", bank);

                }

                else
                {
                    cmd.Parameters.AddWithValue("@Bank_Name", txtbankname.Text);
                }


                if (txtchequeno.Text == "")
                {
                    cmd.Parameters.AddWithValue("@Cheque_No", cheque);
                }
                else
                {

                    cmd.Parameters.AddWithValue("@Cheque_No", txtchequeno.Text);
                }


                DateTime chequeDate;
                var value = (object)DBNull.Value;
                if (DateTime.TryParseExact(txtmaskchequedate.Text,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out chequeDate))
                {



                    value = chequeDate;
                    cmd.Parameters.AddWithValue("@Cheque_Date", value);  

                }
                else
                {

                    cmd.Parameters.AddWithValue("@Cheque_Date",(txtmaskchequedate.Text));  

                }

                if (txtbillno.Text == "")
                {
                    cmd.Parameters.AddWithValue("@Bill_No", billno);

                }
                else
                {
                    cmd.Parameters.AddWithValue("@Bill_No", txtbillno.Text);


                }

                DateTime BillDate;
                var value1 = (object)DBNull.Value;
                if (DateTime.TryParseExact(txtmaskbilldate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out BillDate))
                {



                    value1 = BillDate;
                    cmd.Parameters.AddWithValue("@Bill_Date", value1);

                }
                else
                {

                    cmd.Parameters.AddWithValue("@Bill_Date", (txtmaskbilldate.Text));

                }



                if (txtcodecreator.Text == "")
                {
                    cmd.Parameters.AddWithValue("@Code_Create", codecreate);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@Code_Create", txtcodecreator.Text.ToString());
                }



                int n = cmd.ExecuteNonQuery();
                cnn.Close();
                if (n > 0)
                {
                    MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

您可以签入textbox控件中是否输入了有效日期。但如果不是,则无论如何都要使用文本框的内容作为参数值。-这当然是不对的

由于
value
被初始化为DBNull,您可以在任何情况下添加参数,而不需要代码的else部分。 更改您的代码:

DateTime chequeDate;
var value = (object)DBNull.Value;
if (DateTime.TryParseExact(txtmaskchequedate.Text,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out chequeDate))
{
    value = chequeDate;
}
cmd.Parameters.AddWithValue("@Cheque_Date", value);  

对于
@Bill\u Date
参数,添加文本框中的文本时存在完全相同的问题,即使该文本框无效。根据我的示例重写该参数的代码。

仍然是我得到的相同异常,我更改代码,如DateTime chequeDate;var value=(object)DBNull.value;如果(DateTime.TryParseExact(txtmaskchequedate.Text,“dd/MM/yyyy”,null,System.Globalization.DateTimeStyles.None,out chequeDate)){value=chequeDate;}cmd.Parameters.AddWithValue(@check_Date),value);我也为此放置了调试器,但调试器go cmd.Parameters.AddWithValue(“@check\u Date”,value);只有当掩码文本框为空时,这一行才在答案中测试代码。这两种情况都适用。我认为您应该使用调试器逐步检查代码。具有…参数的行。AddWithValue。。。应该总是执行。感谢您的回复,我也使用调试器,它总是在cmd.Parameters.AddWithValue(“@check\u Date”,value)中运行;如果输入日期值数据插入成功,则输入日期值或不输入日期值,但如果输入日期值数据插入不成功,则输入例外我在access表中设置了短日期格式,然后尝试运行您的代码,它运行得非常完美我亲爱的朋友,非常感谢您的努力和帮助!!