如何访问选择查询的行和列 [英] How to access rows and columns of a select query

查看:93
本文介绍了如何访问选择查询的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var q = db.Execute("a select query");





嗨!我wana访问从此查询获得的列(和行)(如q [0] .column1)。但是当我将它存储在var中时,它是不可能的。我可以使用哪种数据类型或技术?这样做时,单行输出和多行输出之间有区别吗?我在webmatrix中使用sql server CE db(sdf文件)。



Hi! I wana access the columns (and rows) obtained from this query (like q[0].column1). But when I store it in a var it is not possible. What kind of datatype or technique can I use? Is there a difference between single row output and multiple row output when doing this? Im working with sql server CE db (sdf file) in webmatrix.

推荐答案

如果你在asp.net中使用c#:





if you are working in asp.net with c# :


using(SqlConnection con = new SqlConnection(connectionstring))
{
  con.Open();

  SqlCommand cmd = new SqlCommand("SELECT * FROM tableName", con);
  SqlDataadapter adpt = new SqlDataAdapter(cmd);

  DataSet ds = new DataSet();
  adpt.Fill(ds);

  DataColumn dclA = ds.Tables[0].Columns[0];
  // OR
  DataColumn dclB = ds.Tables[0].Columns["ColumnName"];

  DataRow drA = ds.Tables[0].Rows[0];

  string DataA = drA["ColumnName"].ToString();
  // OR
  string DataB = drA[0].ToString();
}





试试吧......


试试这个。





Try this.


SqlCommand command = new SqlCommand();
command.Connection = sqlCon;
command.CommandType = CommandType.Text;
command.CommandTimeout = 216000;
command.CommandText = "Your Query";
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
adapter.Fill(dt);

string dataValue = dt.Rows[0]["Column_Name"]; // returns First Rows







希望它可以帮到你。




Hope it helps you.


HI,



请参阅以下链接,这些可能会对您有所帮助。



C#DataTable

数据集

DATATABLE和DATASET



这些是您可以访问列和行的不同方法。



谢谢


Refer the following links these may help you.

C# DataTable
Dataset
DATATABLE and DATASET

These are the different ways that you can access the columns and rows.

Thanks


这篇关于如何访问选择查询的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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