使用它的Id如何在asp.net中使用3层架构检索记录? [英] Retrieving Record using 3-tier architecture in asp.net using its Id how to do it?

查看:96
本文介绍了使用它的Id如何在asp.net中使用3层架构检索记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据访问层中的代码



  public   void  FindRecord( int  RollNumber)
{
使用(SqlConnection con = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand( spfindtblstudent ,con))
{
cmd.Parameters.Add( @ RollNumber,RollNumber);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
}
}
}





商业逻辑代码



  public   void  find( int  RollNumber)
{
Dal objdal = new Dal();
objdal.FindRecord(RollNumber);
}





代码背后代码



protected void btnfind_Click(object sender,EventArgs e)

{



Bo objbo = new Bo();

objbo .Name = txtname.Text;

objbo.Address = txtaddress.Text;

objbo.Phone = txtphone.Text;

Bll objbll = new Bll();

objbll.find(RollNumber);





}



完全不工作



我刚接触3层但是试图找到记录代码必须写的是什么我的代码是否有意义



帮助我

提前感谢

解决方案

ExecuteNonQuery不返回执行的sql语句的结果,可以使用 中的DataReader [<啊ref =http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]或< a href =http://msdn.microsoft.com/en-us/library/bh8kx08z(v=vs.110).aspx> DataAdapter [ ^ ]获取DataSet或DataTable结果。阅读文档并查看MSDN中的示例以获取更多信息。

获得DataTable或DataSet后,您可以通过更改签名从FindRecord方法返回该对象

  public  DataTable FindRecord( int  RollNumber)
{

// 从数据库中检索数据
// 最后返回datatable;
}



然后你可以得到如下数据表

 Dal objdal =  new  Dal(); 
DataTable dt = objdal.FindRecord(RollNumber);



示例代码:

  public  DataTable FindRecord( int  RollNumber)
{
使用(SqlConnection sqlConn = new SqlConnection(cs))
使用 (SqlCommand cmd = new SqlCommand( spfindtblstudent,sqlConn))
{
cmd.Parameters.Add( @ RollNumber,RollNumber);
cmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
}





我假设您的存储过程具有返回匹配数据的select语句。


code in Data Access Layer

public void FindRecord(int RollNumber)
       {
           using (SqlConnection con = new SqlConnection(cs))
           {
               using(SqlCommand cmd = new SqlCommand("spfindtblstudent",con))
               {
                   cmd.Parameters.Add("@RollNumber", RollNumber);
                   cmd.CommandType = CommandType.StoredProcedure;
                   con.Open();
                   cmd.ExecuteNonQuery();
               }
           }
       }



Code in Business Logic

public void find(int RollNumber)
      {
          Dal objdal = new Dal();
          objdal.FindRecord(RollNumber);
      }



Code in code behind

protected void btnfind_Click(object sender, EventArgs e)
{

Bo objbo = new Bo();
objbo.Name = txtname.Text;
objbo.Address = txtaddress.Text;
objbo.Phone = txtphone.Text;
Bll objbll = new Bll();
objbll.find(RollNumber);


}

Not Working At all

Iam new to 3-Tier but trying to find the record what is thec code have to write and does my code make any sense
Please
Help me out
thanks in advance

解决方案

ExecuteNonQuery is not return the results of sql statement which executed, you can use DataReader[^] or DataAdapter[^] to get DataSet or DataTable result. Read the documentation and check the samples in the MSDN for more information.
after you get DataTable or DataSet you can return that object from FindRecord method by changing the signature as below

public DataTable FindRecord(int RollNumber)
{

      // retrieve data from database
      //finally return datatable; 
}


Then you can get the data table as below

Dal objdal = new Dal();
DataTable dt= objdal.FindRecord(RollNumber);


sample code:

public DataTable FindRecord(int RollNumber)
{
    using(SqlConnection sqlConn = new SqlConnection(cs))
    using(SqlCommand cmd = new SqlCommand("spfindtblstudent", sqlConn))
    {
        cmd.Parameters.Add("@RollNumber", RollNumber);
        cmd.CommandType = CommandType.StoredProcedure;
        sqlConn.Open();
        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        return dt;
    }
}



I assume that your stored procedure having select statement which return matching data.


这篇关于使用它的Id如何在asp.net中使用3层架构检索记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆