C# 存储选择结果
我想提取我的表名并将其保存到变量中这是我的cod,返回3个答案:学生、老师和分数。如何更改它以将这些树表名保存为3变量。多谢各位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
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());
//...
}