如何从Asp.Net和C#中获取数据库中的值 [英] How to get Values from Database to list in Asp.Net and C#

查看:207
本文介绍了如何从Asp.Net和C#中获取数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码从表中返回一行,如何使用以下代码从db获取多行作为列表



The below code returns a single row from table,how to get multiple row from db as a list using the below code

    public BusinessObj.Tables.GetLocal GetItem(object TransId)
    {
     try
      {
        BusinessObj.Tables.GetLocal objGetLocal;
        string spName = "spGetLocal";
        Database db = DatabaseFactory.CreateDatabase(ConnectionString);
        DbCommand dbCommand = db.GetStoredProcCommand(spName);
        db.AddInParameter(dbCommand, "@billId", DbType.Int32, ((int)TransId));
        DataTable dt = new DataTable();
        dt = db.ExecuteDataSet(dbCommand).Tables[0];
        objGetLocal = new BusinessObj.Tables.GetLocal();                
        if (dt.Rows.Count != 0)
         {
          objGetLocal=(BusinessObj.Tables.GetLocal)FillRecords(dt.Rows[0], objGetLocal);
         }
    return objGetLocal;
      }
}

推荐答案

希望以下修改后的代码肯定对您有所帮助: -



首先包括标题
Hope the below modified code will definitely of help to you :-

First of all include the header
System.Collections.Generic;







然后这里是修改后的代码:






Then here is the modified code:

public List<businessobj.tables.getlocal> GetItem(object TransId)
{
 try
 {
   List<businessobj.tables.getlocal> objGetLocalList = new List<businessobj.tables.getlocal>();
   string spName = "spGetLocal";
   Database db = DatabaseFactory.CreateDatabase(ConnectionString);
   DbCommand dbCommand = db.GetStoredProcCommand(spName);
   db.AddInParameter(dbCommand, "@billId", DbType.Int32, ((int)TransId));
   DataTable dt = new DataTable();
   dt = db.ExecuteDataSet(dbCommand).Tables[0];
   foreach(DataRow dr in dt.Rows)
   {
	BusinessObj.Tables.GetLocal objGetLocal = new BusinessObj.Tables.GetLocal();
	objGetLocal=(BusinessObj.Tables.GetLocal)FillRecords(dr, objGetLocal);
	objGetLocalList.Add(objGetLocal);
   }
   return objGetLocalList;
 }
}





我尽力给你一份代码的工作副本,如果它不工作的话我希望你能对如何实现目标有正确的认识。



快乐编码。



Tried my best give you a working copy of code, still if it is not working i hope you will get the proper idea on how to accomplish your goal.

Happy coding.


这篇关于如何从Asp.Net和C#中获取数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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