Advertisement

02.10.2004 at 09:33AM PST, ID: 20879954
[x]
Attachment Details

Seperation of Interface and Implementation

Asked by DarrenD in C# Programming Language

Tags: idatabase, executesqlquery, interface

Hi,

What I am trying to do is to create two dlls one with the interface and one with the implementation

1: Create an Interface in a DLL
2: Create an implementation in a seperate DLL
3: Use this in a client exe

This is what I have

1: Interface
using System ;
using System.Data ;

namespace ICTLDatabase
{
      public interface IDatabase
      {
            DataTable ExecuteSQLQuery(string sConnection, string sSQLQuery) ;
      }
}

2: Implementation
References the Interface
using System ;
using System.Data ;
using System.Data.SqlClient ;
using System.Diagnostics ;
using ICTLDatabase ;

namespace CTLDatabase
{
      public class cSQLDatabase : IDatabase
      {
      private SqlConnection m_oconn = null ;
      private SqlCommand m_cmd = null ;
      private SqlTransaction m_trans ;
      
      public cSQLDatabase()
      {
            m_cmd = new SqlCommand() ;
      }

      DataTable IDatabase.ExecuteSQLQuery(string sConnection, string sSQLQuery)
      {
            // using guarantees that Dispose is called on m_oconn, which will close the connection.
            using (m_oconn = new SqlConnection(sConnection))
            {
                  // Set the Command propeties.
                  m_cmd.CommandText = sSQLQuery ;
                  m_cmd.Connection = m_oconn ;
                  m_cmd.CommandType = CommandType.Text ;
                  SqlDataAdapter adapter = new SqlDataAdapter(m_cmd) ;
                  DataTable dataTable = new DataTable(sSQLQuery) ;
                  m_oconn.Open() ;

                  // Run the stored Procedure and place the results in dataTable.
                  adapter.Fill(dataTable) ;

                  return dataTable ;
            }
      }
}

            
               } // End cSQLDatabase
} // End Namespace


3: Client
Cut Down Version...
The client references both the above dll's like so
using ICTLDatabase ;
using CTLDatabase ;

cSQLDatabase oSQL = new cSQLDatabase() ;
IDatabase ISQL = oSQL ;
      
DataTable mydataTable = new DataTable("GET_HOTELS") ;
mydataTable = ISQL.ExecuteSPQuery(sConnection,"GET_HOTELS") ;
DataRow[] currRows = mydataTable.Select(null, null, DataViewRowState.CurrentRows);
...


When I compile everything goes fine but when I run the application in debug mode

I get an error telling me that the ICTLDatabase.dll could not be loaded or one of it's dependencies

Basically what I want to know is should what I am doing work and if so what can I do to make it work correctly.
If there is any other info you require just let me know.

FYI: When I take the interface and place it in the implementation dll  code block everythnig works fine..... Why???

Same as above but in one dll:

namespace ICTLDatabase
{
      public interface IDatabase
      {
            DataTable ExecuteSQLQuery(string sConnection, string sSQLQuery) ;
      }
}
using System ;
using System.Data ;
using System.Data.SqlClient ;
using System.Diagnostics ;
using ICTLDatabase ;

namespace CTLDatabase
{
      public class cSQLDatabase : IDatabase
      {
      private SqlConnection m_oconn = null ;
      private SqlCommand m_cmd = null ;
      private SqlTransaction m_trans ;
      
      public cSQLDatabase()
      {
            m_cmd = new SqlCommand() ;
      }

      DataTable IDatabase.ExecuteSQLQuery(string sConnection, string sSQLQuery)
      {
            // using guarantees that Dispose is called on m_oconn, which will close the connection.
            using (m_oconn = new SqlConnection(sConnection))
            {
                  // Set the Command propeties.
                  m_cmd.CommandText = sSQLQuery ;
                  m_cmd.Connection = m_oconn ;
                  m_cmd.CommandType = CommandType.Text ;
                  SqlDataAdapter adapter = new SqlDataAdapter(m_cmd) ;
                  DataTable dataTable = new DataTable(sSQLQuery) ;
                  m_oconn.Open() ;

                  // Run the stored Procedure and place the results in dataTable.
                  adapter.Fill(dataTable) ;

                  return dataTable ;
            }
      }
}

            
               } // End cSQLDatabase
} // End Namespace


Cheers,

DarrenStart Free Trial
 
 
[+][-]02.10.2004 at 12:03PM PST, ID: 10325638

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.10.2004 at 01:14PM PST, ID: 10326359

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.10.2004 at 01:41PM PST, ID: 10326578

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: idatabase, executesqlquery, interface
Sign Up Now!
Solution Provided By: AvonWyss
Participating Experts: 2
Solution Grade: A
 
 
[+][-]02.11.2004 at 02:14AM PST, ID: 10330797

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32