如何将sqldatareader传输到文本框?

时间:2014-08-19 13:15:50

标签: c# asp.net sql-server

我只能从gridview读取。我想在用户登录时从文本框传递读取文件..

这是我的数据访问层

public static SqlDataReader GetInformation(string Username)
{
    string sql = "Select Idno, Lname, Mname, Username, Password, ConPass, SchoolYear, TermGraduated from Graduates where Username = @username";
    Open();
    SqlCommand com = new SqlCommand(sql, con);
    com.Parameters.AddWithValue("@username", Username);
    com.CommandType = CommandType.Text;
    SqlDataReader sdr = com.ExecuteReader();
    Close();
    return sdr;    
}

这是我的商务逻辑层

public IDataReader GetInformation()
{
    return DAT.GetInformation(this.username);
}

然后我在form_load中的表示层

b.Username = Session["Username"].ToString();

如何从文本框传输数据?我唯一能做的就是使用gridview传输它。但我怎样才能从文本框中传输数据,如" TextBox1,TextBox2,TextBox3,TextBox4,TextBox5 ......"等等

1 个答案:

答案 0 :(得分:0)

所以,如果我理解你的问题

创建一个表示数据的类,比如说毕业生

public class Graduate
{
     public long Id {get;set;}
     public string LastName {get;set;}
     //rest of the fields
}

public static Graduate GetInformation(string Username)
{

//haven't put all the code here, you have the idea I guess
//also wrap this around try-catch block


    string sql = "Select Idno, Lname, Mname, Username, Password, ConPass, SchoolYear,    TermGraduated from Graduates where Username = @username";
    Open();
    SqlCommand com = new SqlCommand(sql, con);
    com.Parameters.AddWithValue("@username", Username);
    com.CommandType = CommandType.Text;

using(var sdr = com.ExecuteReader())
{
   if(sdr.HasRows)
   {
       while(sdr.Read())
       {
          var objGard = new Graduate()
                     {
                        ID = sdr["Idno"] != DBNull.Value
                                ? long.Parse(sdr["Idno"].ToString())
                                : 0,
                        LastName = reader["Lname"] != DBNull.Value
                                    ? reader["Lname"].ToString()
                                    : ""
                        //rest of the fields
                     };
               return objGard;             
       }
   }
}
return null;

}

public Graduate GetInformation()
{
    return DAT.GetInformation(this.username);
}

var grad = GetInformation();
if(grad ==null) return;

txtId.Text = grad.Id;
txtLasName.Text = grad.LastName;