如何使用以下代码获得唯一日期?

时间:2015-08-20 02:25:19

标签: c#

需要有独特日期插入的帮助!

无法执行代码。

DateTime now = DateTime.Now;
string a = dateTimePicker1.Value.ToString("");

if (!(string.IsNullOrEmpty(txtSal.Text) && string.IsNullOrEmpty(txt13.Text) && 
    string.IsNullOrEmpty(txtEmp.Text) && string.IsNullOrEmpty(txtPark.Text) && 
    string.IsNullOrEmpty(txtBank.Text) && string.IsNullOrEmpty(txtMisc.Text) && 
    string.IsNullOrEmpty(txtTrans.Text) && string.IsNullOrEmpty(txtLight.Text) && 
    string.IsNullOrEmpty(txtOS.Text) && string.IsNullOrEmpty(txtOE.Text) && 
    string.IsNullOrEmpty(txtPagibig.Text) && string.IsNullOrEmpty(txtSSS.Text) && 
    string.IsNullOrEmpty(txtPHIL.Text) && string.IsNullOrEmpty(txtRent.Text) && 
    string.IsNullOrEmpty(txtTL.Text) && string.IsNullOrEmpty(txtTele.Text) && 
    string.IsNullOrEmpty(txtTravel.Text) && string.IsNullOrEmpty(txtRM.Text) && 
    string.IsNullOrEmpty(txtICRT.Text) && string.IsNullOrEmpty(lbltotal.Text)))
    {
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jaey\Documents\Transaction.accdb");
    OleDbCommand dt;
    con.Open();
    dt = new OleDbCommand("INSERT INTO Expenses ([Date], Salaries, [13th], Employee, Parking, Bank, Misc, Transpo, Light, [Office supply], [Office Equipment], Pagibig, SSS, Philhealth, Rental, Tax, Tel, Travel, Repairs, I, Total)  VALUES   ('" + a + "','" + txtSal.Text + "','" + txt13.Text + "','" + txtEmp.Text + "','" + txtPark.Text + "','" + txtBank.Text + "','" + txtMisc.Text + "','" + txtTrans.Text + "','" + txtLight.Text + "','" + txtOS.Text + "',' " + txtOE.Text + "','" + txtPagibig.Text + "','" + txtSSS.Text + "','" + txtPHIL.Text + "','" + txtRent.Text + "','" + txtTL.Text + "','" + txtTele.Text + "','" + txtTravel.Text + "','" + txtRM.Text + "','" + txtICRT.Text + "', '" + lbltotal.Text + "')", con);
    dt.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Data saved");
}

如何插入唯一日期值? 数据验证是对的吗?

1 个答案:

答案 0 :(得分:0)

我在评论中已经阅读了您的问题。也许下次在你的问题中包含你的问题。

也许这就是你想要的?因为您在检查控件值的语句中遇到问题。

像这样优化你的代码:

DateTime now = DateTime.Now;
    string a = dateTimePicker1.Value.ToString("");


    if (!string.IsNullOrEmpty(txtSal.Text) && !string.IsNullOrEmpty(txt13.Text) && !string.IsNullOrEmpty(txtEmp.Text) && !string.IsNullOrEmpty(txtPark.Text) && !string.IsNullOrEmpty(txtBank.Text) && !string.IsNullOrEmpty(txtMisc.Text) && !string.IsNullOrEmpty(txtTrans.Text) && !string.IsNullOrEmpty(txtLight.Text) && !string.IsNullOrEmpty(txtOS.Text) && !string.IsNullOrEmpty(txtOE.Text) && !string.IsNullOrEmpty(txtPagibig.Text) && !string.IsNullOrEmpty(txtSSS.Text) && !string.IsNullOrEmpty(txtPHIL.Text) && !string.IsNullOrEmpty(txtRent.Text) && !string.IsNullOrEmpty(txtTL.Text) && !string.IsNullOrEmpty(txtTele.Text) && !string.IsNullOrEmpty(txtTravel.Text) && !string.IsNullOrEmpty(txtRM.Text) && !string.IsNullOrEmpty(txtICRT.Text) && !string.IsNullOrEmpty(lbltotal.Text))
    {


        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jaey\Documents\Transaction.accdb");
        OleDbCommand dt;
        con.Open();
        dt = new OleDbCommand("INSERT INTO Expenses ([Date], Salaries, [13th], Employee, Parking, Bank, Misc, Transpo, Light, [Office supply], [Office Equipment], Pagibig, SSS, Philhealth, Rental, Tax, Tel, Travel, Repairs, I, Total)  VALUES   ('" + a + "','" + txtSal.Text + "','" + txt13.Text + "','" + txtEmp.Text + "','" + txtPark.Text + "','" + txtBank.Text + "','" + txtMisc.Text + "','" + txtTrans.Text + "','" + txtLight.Text + "','" + txtOS.Text + "',' " + txtOE.Text + "','" + txtPagibig.Text + "','" + txtSSS.Text + "','" + txtPHIL.Text + "','" + txtRent.Text + "','" + txtTL.Text + "','" + txtTele.Text + "','" + txtTravel.Text + "','" + txtRM.Text + "','" + txtICRT.Text + "', '" + lbltotal.Text + "')", con);
        dt.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Data saved");
    }

这将要求您在插入数据库之前输入所有控件。

也许你有错误,因为在你的MS DB中你有一个你不接受Null值的列。就像你说的那样,只有1个文本框被输入到else语句并插入数据库时​​才会出现错误。