Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 存储选择结果_C# - Fatal编程技术网

C# 存储选择结果

C# 存储选择结果,c#,C#,我想提取我的表名并将其保存到变量中这是我的cod,返回3个答案:学生、老师和分数。如何更改它以将这些树表名保存为3变量。多谢各位 try { SqlDataReader myreader = null; SqlCommand mycommand = new SqlCommand("select * FROM information_schema.tables WHERE table_type = 'BASE TABLE'", myconnect); myreader = mycomm

我想提取我的表名并将其保存到变量中这是我的cod,返回3个答案:学生、老师和分数。如何更改它以将这些树表名保存为3变量。多谢各位

try
{
  SqlDataReader myreader = null;
  SqlCommand mycommand = new SqlCommand("select * FROM information_schema.tables WHERE table_type = 'BASE TABLE'", myconnect);
  myreader = mycommand.ExecuteReader();
  while (myreader.Read())
  {
    Console.WriteLine(myreader[2].ToString());
  }
}
您可以使用以下选项:

string tableName ="" ; // Variable to stroe the table names
string connectionString = ""; //Your connectionstring
// get records from the table
string commandString =""; //Your query
// create the data set command object
// and the DataSet
SqlDataAdapter DataAdapter =new SqlDataAdapter(commandString, connectionString);
DataSet DataSet = new DataSet( );

// fill the DataSet object
DataAdapter.Fill(DataSet, "Customers");
// Get the one table from the DataSet
DataTable dataTable = DataSet.Tables[0];
// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
 {

  tableName = dataRow[0].tostring();
  //...
  }

如果要为将来的用户或其他会话保存结果,则可以使用以下任何方法

第一个 使用“插入”查询将结果逐个保存在另一个表中,该表是专门为保存数据而创建的 您可以将insert命令/语句直接放入for循环中

第二种方法
使用xml存储值非常简单且内存友好

一种简单的内置方式是:


我修改了@Doctor代码,使用ArrayList在单个变量中存储表名的数量

ArrayList alTableName  = new ArrayList(); // Variable to stroe the table names
string connectionString = ""; //Your connectionstring
// get records from the table
string commandString =""; //Your query
// create the data set command object
// and the DataSet

SqlDataAdapter DataAdapter =new SqlDataAdapter(commandString, connectionString);
DataSet DataSet = new DataSet( );

// fill the DataSet object
DataAdapter.Fill(DataSet, "Customers");

// Get the one table from the DataSet
DataTable dataTable = DataSet.Tables[0];

// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
{

    alTableName.Add(dataRow[0].tostring());
    //...
}

如果您已经知道查询的答案,为什么要从数据库中获取它?只要用适当的值设置三个变量,就可以了。
string tNames = string.Join(",", allTableNames);
Console.Write("all tables in the given database: " + tNames);
ArrayList alTableName  = new ArrayList(); // Variable to stroe the table names
string connectionString = ""; //Your connectionstring
// get records from the table
string commandString =""; //Your query
// create the data set command object
// and the DataSet

SqlDataAdapter DataAdapter =new SqlDataAdapter(commandString, connectionString);
DataSet DataSet = new DataSet( );

// fill the DataSet object
DataAdapter.Fill(DataSet, "Customers");

// Get the one table from the DataSet
DataTable dataTable = DataSet.Tables[0];

// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
{

    alTableName.Add(dataRow[0].tostring());
    //...
}