使用最小代码从SQL数据库行填充TextBox

时间:2016-01-10 22:03:16

标签: c# html css asp.net sql-server

我有一个“我的用户帐户”'我想要从我的Azure数据库中的18列填充18个控件的网页。我已将用户登录电子邮件设置为会话值'新'我用它来搜索数据库并找到相应的行。

我正在使用以下代码,但我认为这必须是漫长的(并且执行缓慢)方式。之前我被告知如果我发现自己使用了复制>粘贴,那么我做错了!

有人可以建议更短(阅读:"更好")的方式来编码吗?

显示的代码适用于页面上的前两个标签。其余控件是TextBoxes和DropDownList。我认为必须有一种方法,除了SQL CommandText需要为每个控件更改以获取更正列数据(account_no,first_name,last_name,email_1,phone_1等)

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

有什么想法吗?

更新:

以下答案中的代码似乎正在生成一个迷路引号,这会影响我的数据库搜索我的下拉列表。见这里:

            else
            {

                string str = Convert.ToString(Session["New"]);
                string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
                string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
                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 reader1 = oSqlCommand.ExecuteReader())
                    {
                        if (reader1.Read())
                        {
                            AcNo.Text = reader1["account_no"].ToString();
                            TextBoxFName.Text = reader1["first_name"].ToString();
                            TextBoxLName.Text = reader1["last_name"].ToString();
                            TextBoxEmail1.Text = reader1["email_1"].ToString();
                            TextBoxEmail2.Text = reader1["email_2"].ToString();
                            TextBoxBusAddr1.Text = reader1["business_street_1"].ToString();
                            TextBoxBusAddr2.Text = reader1["business_street_2"].ToString();
                            TextBoxBusAddr3.Text = reader1["business_street_3"].ToString();
                            TextBoxBusAddrCity.Text = reader1["business_city"].ToString();
                            TextBoxBusAddrState.Text = reader1["business_state"].ToString();
                            TextBoxPostalCode.Text = reader1["business_postal_code"].ToString();
                            TextBoxBusAddrCountry.Text = reader1["business_country"].ToString();
                            TextBoxCoyName.Text = reader1["company_name"].ToString();
                            TextBoxPhn1.Text = reader1["phone_1"].ToString();
                            TextBoxPhn2.Text = reader1["phone_2"].ToString();
                            TextBoxWebsite.Text = reader1["website"].ToString();
                            RegDate1.Text = reader1["registration_date"].ToString();
                            TextBoxScreensNo.Text = reader1["screens_no"].ToString();

                            var findBusType = reader1["business_type"].ToString();
                                var selectedIndex = -1;

                                for (int i = 0; i < BusTypeddl.Items.Count; i++)
                                {
                                    if (BusTypeddl.Items[i].ToString() == findBusType)
                                    {
                                        selectedIndex = i;
                                        break;
                                    }
                                }
                                if (selectedIndex > -1)
                                {
                                    BusTypeddl.SelectedIndex = selectedIndex;
                                }                                
                        }
                    }
                }
            }

business_typereader1列检索的值应该是Advertising,它存在于我的DropDownList中,但当我单步执行调试器时,findBusType的值为"Advertising {1}}显然不匹配我的DropDownList中的任何值。我无法找到迷路"出现的位置。你的想法一如既往地非常感激。

1 个答案:

答案 0 :(得分:3)

SQL中的SELECT语句允许选择要从表中检索的字段。您不仅限于一个领域。

.....
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 ...
        }
    }
}

此示例中有两点需要强调:

  • 定义sql语句,使用单个字符串声明要检索的所有字段,并使用参数占位符而不是字符串连接
  • 将您的一次性物品放入使用声明中以正确使用 退出使用区块时将它们丢弃