我对 ASP.Net C# 还是很陌生,我有这段代码可以正常工作。我在 web.config 文件中有多个连接字符串,我根据下拉列表的文本在它们之间切换。我希望对任何更整洁/更清洁的方法提出一些建议来存档它,而不是我在下面的内容(如果有的话)。谢谢!
protected void search_ClickALL(object sender, EventArgs e)
{
if (ddlALL.SelectedItem.Text == "ENWL")
{
using (
SqlConnection conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["ConHprENWL"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT dpCreatedDT, enStatusCH, enNotificationNoNI FROM dp_enquiry WHERE ennotificationnoni = @JobnoALL", conn);
try
{
SqlParameter search = new SqlParameter();
search.ParameterName = "@JobnoALL";
search.Value = JobnoALL.Text.Trim();
cmd.Parameters.Add(search);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
gridviewALL.DataSource = dt;
gridviewALL.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (cmd.ExecuteScalar() == null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('Job Number not found!');</script>");
}
conn.Close();
mpePopUpALL.Show();
}
}
}
else if (ddlALL.SelectedItem.Text == "NW")
{
using (
SqlConnection conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["ConHprNorthumbrian"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT dpCreatedDT, enStatusCH, enNotificationNoNI FROM dp_enquiry WHERE ennotificationnoni = @JobnoALL", conn);
try
{
SqlParameter search = new SqlParameter();
search.ParameterName = "@JobnoALL";
search.Value = JobnoALL.Text.Trim();
cmd.Parameters.Add(search);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
gridviewALL.DataSource = dt;
gridviewALL.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (cmd.ExecuteScalar() == null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('Job Number not found!');</script>");
}
conn.Close();
mpePopUpALL.Show();
}
}
}
}