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

问题描述

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.

推荐答案

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();
}





试试吧......


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.



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

07-16 19:20
查看更多