从DataSet获取值到变量中

时间:2011-09-02 14:00:26

标签: c# .net asp.net ado.net

我有以下方法返回数据集。我使用的是.NET 2.0

DataSet ds = GetAllRecords();

我想从特定行的每列中获取值并将其绑定到变量。 如何实现这一目标?

目前该方法返回表中的所有行,我必须根据ID找到该特定行。

但是,如果不可能,我可以随身携带

DataSet ds = GetSpecificRecord(id);

但是我仍然需要从每列获取值并将其绑定到变量。 请指教。

6 个答案:

答案 0 :(得分:6)

// Create a table 
DataTable table = new DataTable("users");
table.Columns.Add(new DataColumn("Id", typeof(int)));
table.Columns.Add(new DataColumn("Name", typeof(string)));

table.Rows.Add(1, "abc");
table.Rows.Add(2, "ddd");
table.Rows.Add(3, "fff");
table.Rows.Add(4, "hhh d");
table.Rows.Add(5, "hkf ds");

// Search for given id e.g here 1

DataRow[] result = table.Select("Id = 1"); // this will return one row for above data 
foreach (DataRow row in result)
{
    Console.WriteLine("{0}, {1}", row[0], row[1]);
}

答案 1 :(得分:3)

这将从行0获取值,显然您需要修改多行返回

long id = ds.Tables[0].Rows[0].Field<long>("ID");

答案 2 :(得分:1)

您是否考虑过使用类型化数据集?如果您正在查找基于表的主键的行,它将自动生成一个方法,您可以按ID获取行,并且它将被强类型化,以便您可以按名称获取列值。

它们在.Net 2.0中非常方便 - 但LINQ使它们相当过时。

编辑:稍好的参考网址

http://www.c-sharpcorner.com/UploadFile/rupadhyaya/TypedDataSets12032005021013AM/TypedDataSets.aspx

答案 3 :(得分:1)

我们可以通过这种简单的方法从数据集中获取价值:

  System.Data.DataSet ds = db.MySelect(
                    "Fields",
                    "Table",
                     "Condition"
                     );

        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
               object value=Tables[0].Rows[indx]["field name"];
            }
        }

答案 4 :(得分:0)

您可以使用LINQ执行此操作。

DataRow resultRow = ds.Tables["SomeTable"].AsEnumerable().Where(row => row.Field<int>("SomeID") == 1).FirstOrDefault();

适用于.NET 2.0及以下版本

如果您的DataTable定义了主键,您可以找到如下所示的行:

DataTable table = ds.Tables["SomeTable"];
DataRow row = table.Rows.Find(1);

如果没有主键,您可以分配如下:

DataTable table = ds.Tables["SomeTable"];
table.PrimaryKey = new DataColumn[] { table.Columns["SomeID"] };

DataRow row = table.Rows.Find(1);

另一种选择是使用DefaultView的RowFilter属性,如下所示:

DataTable table = ds.Tables["SomeTable"];

table.DefaultView.RowFilter = "SomeID == 1"; //not sure if it's == or = here
DataRow row = table.DefaultView[0].Row;

最后,您还可以使用DataTable.Select()方法查找行。

答案 5 :(得分:0)

从学生表中获取值并将其显示在控制台窗口中。

class Class1
{
    static void Main(string[] args)
    { 
          SqlConnection con=new SqlConnection(<connectionstring>);           
          SqlCommand cmd = new SqlCommand("select * from Student", con);
          SqlDataAdapter ad = new SqlDataAdapter(cmd);
          DataSet ds=new DataSet();
          ad.Fill(ds);
          for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
          {
              try
              {

                  DataRow r = ds.Tables[0].Rows[i];
                  Console.WriteLine(r.Field<Int32>(0));
                  Console.WriteLine(r.Field<String>(1));
                  Console.WriteLine(r.Field<String>(2));
                  Console.WriteLine(r.Field<Decimal>(3));
                  Console.WriteLine(r.Field<Int16>(4));

              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex);
              }
          }

          Console.ReadKey();
    }
}

sql学生表信息

Id int
Name varchar(50)
EmailId nvarchar(50)
MobileNo numeric(10,0)
Standard smallint