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(sConne
ction,"GET
_HOTELS") ;
DataRow[] currRows = mydataTable.Select(null, null, DataViewRowState.CurrentRo
ws);
...
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,
Darren
Start Free Trial