protected void Button1_Click(object sender, EventArgs e)
{
string client = TextBox1.Text;
string selected = RadioButtonList1.SelectedValue;
string calendar = Calendar1.SelectedDate.ToShortDateString();
string disease = txtDisease.Text;
SqlCommand insert = new SqlCommand("insert into Appointment(Client_ID, DateofAppointment, TimeofAppointment, Disease) values(@Client_ID, @DateofAppointment, @TimeofAppointment, @Disease)", conn);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Client where Client_ID = @Client_ID", conn);
cmd.Parameters.AddWithValue("@Client_ID", TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
insert.Parameters.AddWithValue("@Client_ID", client);
insert.Parameters.AddWithValue("@DateofAppointment", calendar);
insert.Parameters.AddWithValue("@TimeofAppointment", selected);
insert.Parameters.AddWithValue("@Disease", disease);
insert.ExecuteNonQuery();
TextBox1.Text = "";
RadioButtonList1.SelectedIndex = -1;
txtDisease.Text = "";
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Appointment submitted! Please wait for your SMS confirmation. Thank you!')</script>");
}
else
{
TextBox1.Text = "";
RadioButtonList1.SelectedIndex = -1;
txtDisease.Text = "";
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('You are not a registered client!')</script>");
}
}
catch
{
conn.Close();
}
}
}
我想创建另一个命令来搜索在插入我的数据库之前是否已经采用了日期和领带。
答案 0 :(得分:0)
不确定您的原始例外是什么,但在发布问题时将其包括在内是有帮助的。
关于您的连接,通常不建议使用类级别连接。 Sql Server为您处理连接池,因此最佳做法是在完成连接后立即处理连接。如果要重新查询(在另一个方法或类中),则创建一个新连接。还要将您的连接包裹在using
块中,以确保它们已关闭并处理掉。
关于您的日期,最好始终以其原生类型保留数据。这意味着您的日期列类型应为Sql Date,而不是字符串。这也意味着您的日历的参数值应为DateTime calendar = Calendar1.SelectedDate;
而不是string
。
protected void Button1_Click(object sender, EventArgs e)
{
string client = TextBox1.Text;
string selected = RadioButtonList1.SelectedValue;
// this should be a DateTime instance, not a string
string calendar = Calendar1.SelectedDate.ToShortDateString();
string disease = txtDisease.Text;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select 1 from Appointment WHERE DateofAppointment = @DateofAppointment AND TimeofAppointment = @TimeofAppointment", conn);
cmd.Parameters.AddWithValue("@DateofAppointment", calendar);
cmd.Parameters.AddWithValue("@TimeofAppointment", selected);
bool exists = false;
using(var reader = cmd.ExecuteReader()) {
exists = reader.Read(); // if this returns true there is a record
}
// do something based on exists
SqlCommand insert = new SqlCommand("insert into Appointment(Client_ID, DateofAppointment, TimeofAppointment, Disease) values(@Client_ID, @DateofAppointment, @TimeofAppointment, @Disease)", conn);
// repurpose same pointer but to different instance
cmd = new SqlCommand("select * from Client where Client_ID = @Client_ID", conn);
cmd.Parameters.AddWithValue("@Client_ID", TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
insert.Parameters.AddWithValue("@Client_ID", client);
insert.Parameters.AddWithValue("@DateofAppointment", calendar);
insert.Parameters.AddWithValue("@TimeofAppointment", selected);
insert.Parameters.AddWithValue("@Disease", disease);
insert.ExecuteNonQuery();
TextBox1.Text = "";
RadioButtonList1.SelectedIndex = -1;
txtDisease.Text = "";
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Appointment submitted! Please wait for your SMS confirmation. Thank you!')</script>");
}
else
{
TextBox1.Text = "";
RadioButtonList1.SelectedIndex = -1;
txtDisease.Text = "";
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('You are not a registered client!')</script>");
}
}
catch
{
conn.Close();
}
}
答案 1 :(得分:0)
您可以在第一个命令之前创建命令,并在执行第二个命令之前检查结果:
cmd.CommandText = "SELECT COUNT(*) FROM Client where {you conditions}";
Int32 count = (Int32) cmd.ExecuteScalar();
然后检查count
是否大于0,然后执行第二次。
答案 2 :(得分:0)
protected void Button1_Click(object sender,EventArgs e) { string client = TextBox1.Text; string selected = RadioButtonList1.SelectedValue; string calendar = Calendar1.SelectedDate.ToShortDateString(); string disease = txtDisease.Text;
SqlCommand insert = new SqlCommand("insert into Appointment(Client_ID, DateofAppointment, TimeofAppointment, Disease) values(@Client_ID, @DateofAppointment, @TimeofAppointment, @Disease)", conn);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Client where Client_ID = @Client_ID", conn);
cmd.Parameters.AddWithValue("@Client_ID", TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
SqlCommand search = new SqlCommand("select * from Appointment where DateofAppointment = @DateofAppointment and TimeofAppointment = @TimeofAppointment", conn);
search.Parameters.AddWithValue("@DateofAppointment", calendar);
search.Parameters.AddWithValue("@TimeofAppointment", selected);
Int32 count = (Int32)search.ExecuteScalar();
if (count > 0)
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Error!')</script>");
}
else
{
insert.Parameters.AddWithValue("@Client_ID", client);
insert.Parameters.AddWithValue("@DateofAppointment", calendar);
insert.Parameters.AddWithValue("@TimeofAppointment", selected);
insert.Parameters.AddWithValue("@Disease", disease);
insert.ExecuteNonQuery();
TextBox1.Text = "";
RadioButtonList1.SelectedIndex = -1;
txtDisease.Text = "";
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Appointment submitted! Please wait for your SMS confirmation. Thank you!')</script>");
}
}
else
{
TextBox1.Text = "";
RadioButtonList1.SelectedIndex = -1;
txtDisease.Text = "";
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('You are not a registered client!')</script>");
}
}
catch
{
conn.Close();
}
}
}
它还没有工作...... :(