c#使用sqldatareader方法填充文本框

时间:2012-01-06 15:28:09

标签: c# asp.net

我创建了以下测试的方法并确实返回了正确的数据。我感到困惑的是,使用此方法的结果在表单上填充单个文本框的正确方法是什么?

不是使用objectdatasource然后将gridview绑定到有效的objectdatasource,而是需要更多自由来自定义表单。

public MemberDetails GetMemberDetail(int membershipgen)
   {
       SqlConnection con = new SqlConnection(connectionString);
       SqlCommand cmd = new SqlCommand("usp_getmemberdetail", con);
       cmd.CommandType = CommandType.StoredProcedure;

       cmd.Parameters.Add(new SqlParameter("@MEMBERSHIPGEN", SqlDbType.Int, 5));
       cmd.Parameters["@MEMBERSHIPGEN"].Value = membershipgen;

       try
       {
           con.Open();
           SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

           reader.Read();
           MemberDetails mem = new MemberDetails((int)reader["MEMBERSHIPGEN"], (string)reader["MEMBERSHIPID"], (string)reader["LASTNAME"],
                    (string)reader["FIRSTNAME"], (string)reader["SUFFIX"], (string)reader["MEMBERTYPESCODE"]);
           reader.Close();
           return mem;
       }
        catch (SqlException err)
       {
            throw new ApplicationException("Data error.");
        }
        finally
       {
           con.Close();
       }

3 个答案:

答案 0 :(得分:2)

有些事情:

var memberDetails = GetMemberDetail(12345);
textBox1.Text = memberDetails.Prop1;
textBox2.Text = memberDetails.Prop2;
...

此外,我将重构此方法,并确保通过将它们包装在using语句中来正确处理可处理资源,以避免泄漏非托管句柄:

public MemberDetails GetMemberDetail(int membershipgen)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    using (SqlCommand cmd = con.CreateCommand())
    {
        con.Open();
        cmd.CommandText = "usp_getmemberdetail";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@MEMBERSHIPGEN", membershipgen);
        using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
        {
            if (reader.Read())
            {
                return new MemberDetails(
                    reader.GetInt32(reader.GetOrdinal("MEMBERSHIPGEN")), 
                    reader.GetString(reader.GetOrdinal("MEMBERSHIPID")),
                    reader.GetString(reader.GetOrdinal("LASTNAME")),
                    reader.GetString(reader.GetOrdinal("FIRSTNAME")),
                    reader.GetString(reader.GetOrdinal("SUFFIX")),
                    reader.GetString(reader.GetOrdinal("MEMBERTYPESCODE"))
                );
            }
            return null;
        }
    }
}

答案 1 :(得分:1)

获取MemberDetails;

var memberDetails = GetMemberDetail(1);

填充文本框;

TextBox.Text = memberDetails.Property;

答案 2 :(得分:0)

Jawaid在下面提供的正确答案之外我也会设置SqlConnection con = null;和

SqlCommand cmd = null; outside the try and inside the try put the following 
con = new SqlConnection(connectionString); 

这样,如果在执行cmd.Parameters.Add时出现错误 - 您可以捕获该异常 还处理读者对象

if (reader != null)
{
  ((IDisposable)reader).Dispose(); 
  // somthing like that .. do the same for con and cmd objects or wrap them in a using() {}
}

cmd = new SqlCommand("usp_getmemberdetail", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@MEMBERSHIPGEN", SqlDbType.Int, 5));
cmd.Parameters["@MEMBERSHIPGEN"].Value = membershipgen;