我正在尝试使用表单中的数据将数据输入到我的 SQL Server 2008 中。我正在使用 DateTimePicker 从表单中选择日期并将此数据输入到数据库中,但是当我尝试运行表单时出现以下错误:
将 varchar 数据类型转换为 datetime 数据类型导致值超出范围。
该语句已终止
这是下面我的表单的代码:
private void btnAddBooking_Click(object sender, EventArgs e)
{
DateTime requested = dtpRequested.Value;
DateTime pickup = dtpPickup.Value;
DateTime estDropoff = dtpEstimatedDrop.Value;
SAF.AddNewBooking(requested, pickup, estDropoff, selectedStatus.StatusId, selectedRate.RateId);
dtpRequested.CustomFormat = " ";
dtpPickup.CustomFormat = " ";
dtpEstimatedDrop.CustomFormat = " ";
cbStatus.SelectedIndex = 0;
cbRateOfPayment.SelectedIndex = 0;
MessageBox.Show("Booking Created");
tabControl2.SelectTab(2);
}
这是我存储方法的外观中的代码:
public void AddNewBooking(DateTime requestedD, DateTime pickupD, DateTime estDropOff, int statusId, int rateId)
{
dao.AddNewBooking(requestedD, pickupD, estDropOff, statusId, rateId);
}//end CreateBooking
这是INSERT
我的 DAO 中的语句代码:
sql = "INSERT INTO [Transaction] (TransactionId, RequestedDateOfPickup, ActualDateOfPickup, EstimatedDropOffDate, StatusId, RateId)";
sql += String.Format("VALUES('{0}', '{1}', '{2}', '{3}', {4}, {5})", transactionId, requestedD, pickupD, estDropOff, statusId, rateId);
cn = new SqlConnection(Properties.Settings.Default.UKRENTALSConnectionString);
cmd = new SqlCommand(sql, cn);
cn.Open();
cmd.ExecuteNonQuery();