Select from multiple tables in one call

前端 未结 7 1141
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 13:01

In my code I have a page that includes information from 3 different tables. To show this information I make 3 SQL select calls and unite them in one list to pass as Model to my

相关标签:
7条回答
  • 2021-02-13 13:58

    you can use below generic code snippet

          /// <method>
    
        /// Select Query
    
        /// </method>
    
        public DataTable executeSelectQuery(String _query, SqlParameter[] sqlParameter)
    
        {
            SqlCommand myCommand = new SqlCommand();
    
            DataTable dataTable = new DataTable();
    
            dataTable = null;
    
            DataSet ds = new DataSet();
    
            try
    
            {
                myCommand.Connection = openConnection();
                myCommand.CommandText = _query;//or proc
                myCommand.Parameters.AddRange(sqlParameter);
                myAdapter.SelectCommand = myCommand;
                myAdapter.Fill(ds);
    
                dataTable = ds.Tables[0];//as per your requirement
            }
            catch (SqlException e)
    
            {//any logger 
                Console.Write("Error - Connection.executeSelectQuery - Query: " + _query + " \nException: " + e.StackTrace.ToString());
                return null;
    
            }            
                return dataTable;
        }
    
    0 讨论(0)
提交回复
热议问题