I have a library of methods and extension methods that I've been adding to projects for the past 10 months, as I really to learn C# by writing it. But I've added it to a console app project this time.
I'm struggling the past two days with researching why either (1) Debug.WriteLine isn't outputting any of my error reporting and debug code to output screen when everything I'm reading says that it should or (2) My code is not executing in my external class and so there's nothing for the Debug code to write.
Without getting anything back from my watches or from error logging to the output screen, I'm flying blind.
I don't know if console apps are different from, say, windows forms or if it's one of the new constructs I'm trying to use in C#, but I have read and re-read the sections about classes in my C# 7 in a Nutshell book and am overwhelmed, I suppose.
Can someone suggest something for me to read about or investigate?
I don't know what code to post because I don't know what is relevant to this, but here's some code that definitely isn't working as expected:
NOTE: I'm sure my code is atrocious to most of you! Any suggestions are appreciated.
This is part of the class that sets up my OleDb Database Connection:
public class RBDataOleDb
{
private string nl = Environment.NewLine;
public string DbPassword { get; }
public string DbUser { get; }
public string DbType { get; }
public string DbServer { get; }
public string DbName { get; }
public string DbDataSource { get; }
public string DbPath { get; }
public RBDataOleDb ( string server , string database)
{
DbType = "MDF";
DbServer = server;
DbName = database;
DbDataSource = database;
}
public RBDataOleDb ( string server , string database, string username, string password )
{
DbType = "MDF";
DbServer = server;
DbName = database;
DbUser = username;
DbPassword = password;
DbDataSource = database;
}
public RBDataOleDb ( string file_path )
{
DbType = System.IO.Path.GetExtension ( file_path ).Replace ( "." , "" ).ToUpper ( );
DbPath = file_path;
DbDataSource = file_path;
}
public RBDataOleDb ( string file_path, string username, string password)
{
DbType = System.IO.Path.GetExtension( file_path ).Replace(".","").ToUpper ( );
DbPath = file_path;
DbUser = username;
DbPassword = password;
DbDataSource = file_path;
}
public string GetProvider ( )
{
string result = "";
switch ( DbType )
{
case "MDF":
result = "SQLNCLI11";
break;
case "MDB":
case "ACCDB":
case "XLSX":
case "XLS":
case "DBF":
result = "Microsoft.ACE.OLEDB.12.0";
break;
default:
result = "!!UNABLE TO DETERMINE PROVIDER!!";
throw new ArgumentException ( $"DbType has a value of {DbType}, which is not valid. RBDataOleDb clould not determine what database provider to use when connecting to database" );
}
return result;
}
public string GetConnString ( )
{
string conn = "";
if ( DbServer != null )
{
conn = $"Provider=SQLNCLI11;Data Source={DbServer};Initial Catalog=[{DbName}];";
if ( !string.IsNullOrEmpty ( DbPassword ) )
conn += $"User ID={DbUser};Password={DbPassword};";
else
conn += $"Integrated Security=SSPI;";
}
else if ( DbType == "MDB" || DbType == "ACCDB" )
{
conn = $"Provider=Microsoft.ACE.OLEDB.12.0;data source=[{DbPath}];";
if ( !string.IsNullOrEmpty ( DbPassword ) ) conn += $"Jet OLEDB:Database Password={DbPassword};";
}
else if ( DbType == "MDB" )
{
conn = $@"Provider=Microsoft.Jet.OLEDB.4.0;data source=""{DbPath}"";";
if ( !string.IsNullOrEmpty ( DbPassword ) ) conn += $"User Id={DbUser};Jet OLEDB:Database Password={DbPassword};";
}
else if ( DbType == "XLSX" )
{
conn = $@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[{DbPath}];Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
}
else if ( DbType == "XLS" )
{ conn = $@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[{DbPath}];Extended Properties='Excel 8.0;HDR=YES'";
}
else
{
conn = "* * * NO DbType WAS SET SO RBDataOleDb.getConnString() UNABLE TO CREATE CONNECTION STRING * * *";
}
return conn;
}
}
This is how I am instantiating and returning values from some of it's properties and methods:
RBDataOleDb ole = new RBDataOleDb ( Globals.dbpath );
string provider = ole.GetProvider();
string ds = ole.DbDataSource;
string constring = ole.GetConnString();
Globals.Log ( ds );
Globals.Log ( provider );
Globals.Log ( constring );
This is my Globals.Log method, which has been edited to write to Console.
public static void Log ( string message )
{
string output = Environment.NewLine + message + Environment.NewLine;
Console.WriteLine ( output );
}
What I have tried:
I've rewritten some of my debug logging in my local project so that I can write to Console instead of Debug but I cant' do that from classes in my library or it will effect my other projects that are using it.
As far as getting values back form my class, I've tried using methods, different ways of using getters and setters, and changed access modifiers several times. What you see if the result of those iterative efforts.