2012-02-19 38 views
1

這是我的存儲過程:如何在文本框中顯示數據庫值?

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個不同的文本框

我怎樣才能做到這一點選擇了所有三個值?

回答

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(); 
+1

@raghu marcs answer也不錯... – SoftwareNerd 2012-02-21 05:37:17

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; 
+0

ty for your help ...... – 2012-02-21 05:39:34

相關問題