简体   繁体   English

使用最少的代码从 SQL 数据库行填充文本框

[英]Populate TextBoxes from SQL Database Row Using Minimum Code

I have a 'My User Account' web page with 18 controls that I want to populate from 18 columns in my Azure database.我有一个“我的用户帐户”网页,其中包含 18 个控件,我想从我的 Azure 数据库中的 18 个列中填充这些控件。 I have set the users login email as the session value 'New' which I use to search the database and find the corresponding row.我已将用户登录电子邮件设置为会话值“新建”,我用它来搜索数据库并找到相应的行。

I am using the following code but I think this is slow in executing.我正在使用以下代码,但我认为这执行起来很慢。

Can anyone suggest a more efficient way to code this?谁能建议一种更有效的编码方式?

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";
}

Your thoughts are as always very much appreciated.一如既往地非常感谢您的想法。

The SELECT statement in SQL allows to choose what fields you want to retrieve from your table. SQL 中的 SELECT 语句允许选择要从表中检索的字段。 You are not limited to one field at time.您一次不限于一个字段。

.....
string cmdText = @"select account_no,registration_date, other_field1, 
                   other_field2, other_fieldN
                   from users 
                   where email_1 = @email";

using(SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
using(SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
{
     oSqlConnection.Open();
     oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
     using(SqlDataReader reader = oSqlCommand.ExecuteReader())
     {
        if (reader.Read())
        {
             AcNo.Text = reader["account_no"].ToString();
             RegDate1.Text = reader["registration_date"].ToString();
             otherTextBox.Text = reader["other_field1"].ToString();
             ... and so on ...
        }
    }
}

There are two things to highlight in this example:这个例子中有两点需要强调:

  • Define your sql statement declaring all the fields you want to retrieve with a single string and use parameter placeholders instead of string concatenations定义您的 sql 语句,声明您要使用单个字符串检索的所有字段,并使用参数占位符而不是字符串连接
  • Put your disposable objects inside a using statement to correctly dispose them when you exit from the using block将一次性对象放入 using 语句中,以便在退出 using 块时正确处理它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM