[英]Preventing Overlapping Appointments C# MySQL
不确定我如何才能做到这一点,所以当单击按钮以保存/预订约会时,它将检查现有约会并且不允许约会重叠。这是目前的代码。
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-ELTMMCE;Initial Catalog=Stylogistx;Integrated Security=True");
DateTime dt = DateTime.ParseExact(dateTbx.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
if (dt < DateTime.Today)
{
MessageBox.Show("Appointment Date is in the past. Please select current or future date.");
}
else
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO APPOINTMENT(APP_DATE, APP_STARTTIME, APP_ENDTIME, CLIENT_ID, EMP_ID, SERVICE_ID, APP_STATUS) values ('" + dateTbx.Text + "','" + apptStartTimePicker.Value.ToString("hh:mm tt") + "','" + apptEndTimePicker.Value.ToString("hh:mm tt") + "','" + clientCmboBx.SelectedValue.ToString() + "','" + stylistCmboBx.SelectedValue.ToString() + "','" + serviceCmboBx.SelectedValue.ToString() + "','" + "BOOKED" + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Appointment Booked");
DialogResult dialogResult = MessageBox.Show("Track Inventory?", "Would you like to track any inventory for this appointment?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
this.Hide();
Inventory_Tracking invt = new Inventory_Tracking();
invt.Show();
}
else if (dialogResult == DialogResult.No)
{
this.Hide();
Appointment_Management am = new Appointment_Management();
am.Show();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
con.Close();
this.Hide();
我尝试创建一个查询,选择 APP_STATUS = 'BOOKED' 的约会,这将引入 APP_STARTTIME 和 APP_ENDTIME。
我不确定检查现有开始和结束时间的方法,如果尝试在另一个约会已预订的时间段内进行约会,则推送无效消息。
我还是 C#/Java 的新手,我卡住了,不知道该怎么做。
I have written some code for you. Try to do the following. -
1- Create a class model
2- Serializer class model to XML
3- Do implementation in your DAL and BAL layers
4- Create a procedure
Some code samples:
public class AppointmentDetails
{
public int appDate { get; set; }
public string appStartTime { get; set; }
public string appEndTime { get; set; }
public int clientId { get; set; }
public int empId { get; set; }
public int serviceId { get; set; }
public string appStatus { get; set; }
}
public void BookAppointmentDetails(AppointmentDetails appointmentDetails)
{
string xmlAppointmentDetails = string.Empty;
//Serializer model to XML
XmlSerializer mfaAuditSerializer = new XmlSerializer(typeof(AppointmentDetails));
using (StringWriter textWriter = new StringWriter())
{
mfaAuditSerializer.Serialize(textWriter, appointmentDetails);
xmlAppointmentDetails = textWriter.ToString();
}
using (SqlConnection conn = new SqlConnection("<<DefineConnection>>"))
{
using (SqlCommand cmd = new SqlCommand("dbo.uspBookNewAppointmentDetails", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
// set up the parameters
cmd.Parameters.Add("@prmAppointmentDetails", SqlDbType.Xml).Value = xmlAppointmentDetails;
cmd.Parameters.Add("@prmOutput", SqlDbType.NVarChar, 20).Direction = ParameterDirection.Output;
// open connection and execute stored procedure
conn.Open();
cmd.ExecuteNonQuery();
// read output value from @prmOutput
string output = Convert.ToString(cmd.Parameters["@prmOutput"].Value);
conn.Close();
}
}
}
Create a table and stored the procedure in SQL Server Database
CREATE TABLE AppointmentDetails(
AppId BIGINT IDENTITY(1,1) PRIMARY KEY,
APP_DATE DATE NULL,
APP_STARTTIME NVARCHAR(12) NULL,
APP_ENDTIME NVARCHAR(12) NULL,
CLIENT_ID INT NULL,
EMP_ID INT NULL,
SERVICE_ID INT NULL,
APP_STATUS NVARCHAR(20) NULL
)
IF EXISTS (SELECT 1 FROM SYS.PROCEDURES WHERE Name = 'uspBookNewAppointmentDetails')
BEGIN
DROP PROC dbo.uspBookNewAppointmentDetails
END
GO
CREATE PROCEDURE [dbo].[uspBookNewAppointmentDetails]
@prmAppointmentDetails XML,
@prmOutput [NVARCHAR](20) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION [Tran1]
BEGIN TRY
INSERT INTO AppointmentDetails(APP_DATE,APP_STARTTIME,APP_ENDTIME,CLIENT_ID,EMP_ID,SERVICE_ID,APP_STATUS)
SELECT
data.col.value('(appDate)[1]', 'DATE'),
data.col.value('(appStartTime)[1]', 'NVARCHAR(12)'),
data.col.value('(appEndTime)[1]', 'NVARCHAR(12)'),
data.col.value('(clientId)[1]', 'INT'),
data.col.value('(empId)[1]', 'INT'),
data.col.value('(serviceId)[1]', 'INT'),
data.col.value('(appStatus)[1]', 'NVARCHAR(20)')
FROM @prmAppointmentDetails.nodes('/BookAppointment') as data(col)
IF(@@ROWCOUNT > 0)
BEGIN
SET @prmOutput='BOOKED';
END
COMMIT TRANSACTION [Tran1]
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION [Tran1]
DECLARE @ErrorMessage NVARCHAR(MAX), @ProcessName NVARCHAR(MAX), @ErrorSeverity NVARCHAR(MAX)
SET @ErrorMessage = ERROR_MESSAGE()
SET @ProcessName = ERROR_PROCEDURE()
SET @ErrorSeverity = ERROR_SEVERITY()
SET @prmOutput=@ErrorMessage;
RAISERROR(@ErrorMessage,@ProcessName,@ErrorSeverity)
END CATCH
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.