如何在文本框中显示数据库值?

时间:2012-02-19 10:04:13

标签: asp.net c#-4.0

这是我的存储过程:

create procedure SP_ShowUse
   (@employeeName varchar (50))
as 
begin
     select 
         PTS_Employee.Emp_Username, PTS_Approval.Approval_ApprovedBY,  
         PTS_Branches.Branch_BranchName
    from 
         PTS_Employee, PTS_Approval, PTS_Branches
    where 
         PTS_Employee.Branch_BranchID = PTS_Branches.Branch_BranchID
         AND PTS_Employee.Approval_ApprovedID = PTS_Approval.Approval_ApprovedID
         AND PTS_Employee.Emp_Username = @employeeName
end

我想显示在3个不同文本框中选择的所有三个值

我该怎么做?

3 个答案:

答案 0 :(得分:2)

使用“最老的”解决方案 - ADO.NET - 您可以编写如下内容:

// define a class to hold the data returned from the stored procedure
public class SPReturnData
{
    public string EmployeeUsername { get; set; }
    public string ApprovedBy { get; set; }
    public string BranchName { get; set; }
}

// define a method to call the stored proc and return the data 
public SPReturnData LoadData(string connectionString, string employeeName)
{
    // initialize the structure to be returned
    SPReturnData result = new SPReturnData();

    // setup ADO.NET connection and command
    using(SqlConnection conn = new SqlConnection(connectionString))
    using(SqlCommand cmd = new SqlCommand("dbo.SP_ShowUse", conn))
    {
       // it's a stored procedure
       cmd.CommandType = CommandType.StoredProcedure;

       // set up the command's parameters
       cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar, 50).Value = employeeName;

       // open connection, execute command, close connection
       conn.Open();

       // execute reader
       using(SqlDataReader rdr = cmd.ExecuteReader())
       {
          if(rdr.Read())
          {
              // first column -> employee user name
              result.EmployeeUserName = rdr.GetString(0);

              // second column -> approved by name
              result.ApprovedBy = rdr.GetString(1);

              // third column -> Branch name
              result.BranchName = rdr.GetString(2);
          }

          rdr.Close();
       }

       conn.Close();
    }

    return result;
}

从你的代码 - 使用这样的东西:

// pass in the connection string (e.g. get it from config or something)
// and the employee name - get back a "SPReturnData" object with the data items
SPReturnData data = LoadData("server=.;database=.....;", "Fred Flintstone");

// set your textboxes to the values returned    
txtEmployeeName.Text = data.EmployeeName;
txtApprovedBy.Text = data.ApprovedBy;
txtBranchName.Text = data.BranchName;

答案 1 :(得分:2)

将数据存储在数据集中并从中提取...

      SqlConnection con = new SqlConnection(connstring);
        con.Open();
        SqlCommand mycomm=new SqlCommand ("SP_ShowUse",con);
        mycomm.CommandType=CommandType.StoredProcedure;
        mycomm.Parameters.Add("@employeeName", SqlDbType.VarChar).Value = "Anil";
        SqlDataAdapter showdata = new SqlDataAdapter(mycomm);
        DataSet ds = new DataSet();
        showdata.Fill(ds);
        txtEmployeename.Text = ds.Tables[0].Rows[0]["Emp_Username"].ToString();
        txtBranchName.Text = ds.Tables[0].Rows[0]["Branch_BranchName"].ToString();
        txtApprvdby.Text = ds.Tables[0].Rows[0]["Approval_ApprovedBY"].ToString();
        binddropdownlist();
        con.Close();

答案 2 :(得分:1)

您是否熟悉以下其中一项?

  1. ADO.NET - http://msdn.microsoft.com/en-us/library/h43ks021(v=vs.100).aspx
  2. NHibernate - http://nhforge.org/Default.aspx
  3. EntityFramework - http://msdn.microsoft.com/en-us/library/bb399572.aspx
  4. ADO.Net是最早的,您可以在那里执行选择命令,我建议您阅读并使用其他两个选项之一。