如何有一个独特的日期与以下代码
本文关键字:代码 日期 有一个 | 更新日期: 2023-09-27 18:08:53
需要帮助,有唯一的日期插入!
执行代码有问题。
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");
}
如何插入唯一的日期值?数据验证是否正确?
我在评论中看到了你的问题。也许下次在你的问题中包括你的问题。
也许这就是你想要的?因为你在检查控件值的语句中遇到了问题。
像这样改进你的代码:
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数据库你有一个列,你不接受空值。就像你说的,错误发生时,只有一个文本框输入到else语句,并插入到数据库。