Based on the feedback I have got from the guys reviewing the code as posted in my previous question.
Create database connection and run the insert, delete, update queries class
I have used interfaces and wrote the following code
/* */
namespace SampleInterfaceT1
{
public interface IDbConSetup
{
DbConnection OpenConnection(string connectionString);
void CloseConnection();
}
public interface ISqlDataProvider : IDbConSetup
{
int ExecuteCmd(string dbCmd, SqlParameter param);
}
public class SqlClient : ISqlDataProvider
{
private SqlConnection _dbconnect;
private string _conString;
public DbConnection OpenConnection(string connectionString)
{
if (String.IsNullOrWhiteSpace(connectionString))
{
throw new ArgumentException("Invalid Connection String");
}
_conString = connectionString;
Console.WriteLine("Sql Connection Opened.");
_dbconnect = new SqlConnection(_conString);
_dbconnect.Open();
return _dbconnect;
}
public int ExecuteCmd(string dbCmd, SqlParameter param)
{
int _rc;
if (String.IsNullOrWhiteSpace(dbCmd))
{
throw new ArgumentException("Invalid Connection String");
}
using (SqlCommand _sqlCmd = new SqlCommand(dbCmd, _dbconnect))
{
_sqlCmd.CommandType = CommandType.Text;
_sqlCmd.Parameters.Add(param);
_rc = _sqlCmd.ExecuteNonQuery();
}
Console.WriteLine("Command executed");
return _rc;
}
public void CloseConnection()
{
_dbconnect.Close();
Console.WriteLine("Sql Connection Closed.");
_dbconnect.Dispose();
}
}
}
/* Main Program execution */
namespace SampleInterfaceT1
{
public class Program
{
private static void Main(string[] args)
{
string conString = @""; /* Connection String*/
string dbCommand = @"INSERT INTO [dbo].[TestTable]([Name]) VALUES (@Name)";
ISqlDataProvider SqlDataProviderObject = new SqlClient();
SqlDataProviderObject.OpenConnection(conString);
SqlDataProviderObject.ExecuteCmd(dbCommand, new SqlParameter("Name", "HellofromnewDictionary"));
SqlDataProviderObject.CloseConnection();
Console.ReadLine();
}
}
}
I'm a newbie to the concept of interfaces. I'm I on the right path or did I got it completely wrong?