【发布时间】:2016-04-15 03:47:28
【问题描述】:
我有一个“我的用户帐户”网页,其中包含 18 个控件,我想从我的 Azure 数据库中的 18 列中填充这些控件。我已将用户登录电子邮件设置为会话值“新”,用于搜索数据库并找到相应的行。
我正在使用以下代码,但我认为这执行起来很慢。
谁能建议一种更有效的编码方式?
try
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
string str = Convert.ToString(Session["New"]);
string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection);
oSqlConnection.Open();
SqlCommand oSqlCommand = new SqlCommand();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = CommandType.Text;
oSqlCommand.CommandText = "select account_no from users where email_1 = '" + str + "'";
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
oSqlDataAdapter.SelectCommand = oSqlCommand;
SqlDataReader reader = oSqlCommand.ExecuteReader();
if (reader.Read())
{
AcNo.Text = reader["account_no"].ToString();
}
string str1 = Convert.ToString(Session["New"]);
string cpUsersConnection1 = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
SqlConnection oSqlConnection1 = new SqlConnection(cpUsersConnection);
oSqlConnection.Open();
SqlCommand oSqlCommand1 = new SqlCommand();
oSqlCommand1.Connection = oSqlConnection;
oSqlCommand1.CommandType = CommandType.Text;
oSqlCommand1.CommandText = "select registration_date from users where email_1 = '" + str + "'";
SqlDataAdapter oSqlDataAdapter1 = new SqlDataAdapter();
oSqlDataAdapter1.SelectCommand = oSqlCommand1;
SqlDataReader reader1 = oSqlCommand1.ExecuteReader();
if (reader1.Read())
{
RegDate1.Text = reader1["registration_date"].ToString();
}
}
}
catch
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
AcNo.Text = "";
RegDate1.Text = "";
SetDBdateMessages.Text = "User Session Error - Please Contact Support";
}
一如既往地非常感谢您的想法。
【问题讨论】:
-
修改后的代码中没有任何内容可以添加杂散双引号。相反,我认为您误解了调试器显示的字符串,该字符串显示了一个初始双引号,后跟文本、许多空格和一个在小工具提示中不可见的最终双引号。尝试添加一个 Trim() var findBusType = reader1["business_type"].ToString().Trim();
-
你说得对,这不是一个流浪报价。 Trim 给了我“广告”,但它仍然没有选择我的 DropDownList 中的项目。
-
如何填写 DropDownList?仅当您已用字符串填充它或用于填充 DropDownList 的类具有对 ToString() 的覆盖时,对 ComboBox 的项目调用 ToString 才能正常工作。我建议您可以发布一个新问题来详细说明该问题。不要继续在原来的问题中添加新问题,否则会越来越让其他读者感到困惑。
-
好的,我发了一个新问题stackoverflow.com/questions/34712312/…
标签: c# html css asp.net sql-server