有这样一个查询过程:
    A表中的一个字段:tablename对应几个表例如:
    tablename的值有:B表,C表,D表
想得到B表,C表,D表中某个表的具体内容
我是这样做的:
string tablename[3];//存储读出的表名称
int i=0
SQL语句是:.....
strSQL=“select tablename from A表“
......
while(datareader.read())
{
tablename[i]=datareader[0].tostring();
i++;}
........那么再查询tablename[i]所指的表时怎么办?
SQL语句:
......for(int j=0;j<tablename.Length;j++)
{
           strSQL="select * from tablename[i]";//这样写对么?如果不对怎样书写?
............
}

解决方案 »

  1.   

    strSQL="select * from " + tablename[i];//
      

  2.   

    "select * from "+tablename[i]+"; "
      

  3.   

    strSQL="select * from tablename[i]";//这样写对么?如果不对怎样书写?==============================================
     strSQL="select * from " + tablename[i];我感觉表不应该这样设计.
      

  4.   

    使用.NET中数据库处理的事务处理机制来处理吧
    lz去查一下
      

  5.   

    SqlTransaction
    微软的例子
    public void RunSqlTransaction(string myConnString) 
     {
        SqlConnection myConnection = new SqlConnection(myConnString);
        myConnection.Open();    SqlCommand myCommand = myConnection.CreateCommand();
        SqlTransaction myTrans;    // Start a local transaction
        myTrans = myConnection.BeginTransaction();
        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        myCommand.Connection = myConnection;
        myCommand.Transaction = myTrans;    try
        {
          myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
          myCommand.ExecuteNonQuery();
          myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
          myCommand.ExecuteNonQuery();
          myTrans.Commit();
          Console.WriteLine("Both records are written to database.");
        }
        catch(Exception e)
        {
          try
          {
            myTrans.Rollback();
          }
          catch (SqlException ex)
          {
            if (myTrans.Connection != null)
            {
              Console.WriteLine("An exception of type " + ex.GetType() +
                                " was encountered while attempting to roll back the transaction.");
            }
          }
        
          Console.WriteLine("An exception of type " + e.GetType() +
                            " was encountered while inserting the data.");
          Console.WriteLine("Neither record was written to database.");
        }
        finally 
        {
          myConnection.Close();
        }
    }