【问题标题】:How will I execute another SELECT command after I executed the first one?在执行第一个 SELECT 命令后,我将如何执行另一个 SELECT 命令?
【发布时间】:2017-04-14 02:00:11
【问题描述】:
    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();
        }

    }


}

我想创建另一个命令来搜索在将日期和领带插入到我的数据库之前是否已经使用它。

【问题讨论】:

  • 创建第二个命令对象并调用它,就像第一个一样
  • 更好的是,你为什么不写一个存储过程,然后所有的东西都一次性执行?
  • 你能告诉我怎么做吗?我尝试在 insert.parameters 之前创建另一个命令,但它不起作用。
  • 目标是什么?查看是否没有使用日期和时间然后插入?
  • 是的。如果日期和时间已被占用,系统不应接受。

标签: sql asp.net sqlcommand selectcommand


【解决方案1】:

不确定您最初的例外是什么,但在发布问题时将其包括在内会很有帮助。

关于您的连接,通常不建议使用类级连接。 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();
    }
}

【讨论】:

  • 我认为 OP 想要检查日期和时间是否可用,而不考虑客户端 ID,即从 Appointment WHERE DateofAppointment = @DateofAppointment" AND TimeofAppointment = @TimeofAppointment 中选择 1
【解决方案2】:

您可以在第一个命令之前创建一个命令,并在执行第二个命令之前检查结果:

cmd.CommandText = "SELECT COUNT(*) FROM Client where {you conditions}";
Int32 count = (Int32) cmd.ExecuteScalar();

然后检查count是否大于0然后执行第二个。

【讨论】:

  • 我发布了,请检查。我需要这个来工作。 ://
  • 您在查询中缺少计数:select count(*)
【解决方案3】:

protected void Button1_Click(object sender, EventArgs e) { 字符串客户端 = TextBox1.Text; 字符串选择 = RadioButtonList1.SelectedValue; 字符串日历 = 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();
        }

    }


}

它仍然无法正常工作... :(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多