控制台应用程序不写入debug.writeline而不是在我的类中填充属性 [英] Console app not writing to debug.writeline and not populating properties in my class

查看:82
本文介绍了控制台应用程序不写入debug.writeline而不是在我的类中填充属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法和扩展方法库,我在过去10个月里一直在为项目添加,因为我真的要通过写它来学习C#。但是这次我把它添加到了一个控制台应用项目中。



过去两天我一直在努力研究为什么(1)Debug.WriteLine isn'当我正在阅读的所有东西都应该或者(2)我的代码没有在我的外部类中执行时,我将任何错误报告和调试代码输出到输出屏幕,因此调试代码没有任何内容可以写入。



如果没有从我的手表回来或从错误记录到输出屏幕,我会失明。



我不知道控制台应用程序是否与Windows窗体不同,或者它是否是我尝试在C#中使用的新构造之一,但是我在Nutshell书中阅读并重新阅读了关于C#7课程的章节,我想是不知所措。



可以有人建议我阅读或调查一下?



我不知道要发布什么代码,因为我不知道是什么与此相关,但这里的代码肯定没有按预期工作:



注意:我确定我的代码残暴对大多数人来说!任何建议都表示赞赏。



这是设置我的OleDb数据库连接的类的一部分:

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; }


		/// <summary>
		/// Connect to SQLServer or Oracle with windows auth
		/// NOTE: Add User and Password via overloaded constructors below
		/// </summary>
		/// <param name="dbserver">Server EX: @"(localdb)\MSSQLLocalDB"</param>
		/// <param name="dbname">Database name</param>
		public RBDataOleDb ( string server , string database)
		{
			DbType = "MDF";
			DbServer = server;
			DbName = database;
			DbDataSource = database;
		}


		/// <summary>
		/// Connect to SQLServer or Oracle with password
		/// NOTE: Add User and Password seperately
		/// </summary>
		/// <param name="dbserver">Server name in @"(localdb)\MSSQLLocalDB" format or null</param>
		/// <param name="dbname">Database name</param>
		public RBDataOleDb ( string server , string database, string username, string password )
		{
			DbType = "MDF";
			DbServer = server;
			DbName = database;
			DbUser = username;
			DbPassword = password;
			DbDataSource = database;
		}


		/// <summary>
		/// Use this for connections to files, such as MS ACCESS, EXCEL, etc.
		/// </summary>
		/// <param name="file_path">Path to Database</param>
		public RBDataOleDb ( string file_path )
		{
			DbType = System.IO.Path.GetExtension ( file_path ).Replace ( "." , "" ).ToUpper ( );
			DbPath = file_path;
			DbDataSource = file_path;
		}


		/// <summary>
		/// Use this for connections to files, such as MS ACCESS, EXCEL, etc.
		/// </summary>
		/// <param name="file_path">Path to Database</param>
		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;
		}

		/// <summary>
		/// NOTE: Provider: "Microsoft.ACE.OLEDB.12.0" is the 2010 Office System Driver, which has both 32 bit and 64 bit versions that let your app connect 
		/// to Access, Excel and Text file in a 64bit environment.
		/// </summary>
		/// <returns></returns>
		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 );





这是我的Globals.Log方法,已编辑为写入控制台。





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 );
}





我尝试过:



我已经在我的本地项目中重写了一些调试日志记录,这样我就可以写入Console而不是Debug但我不能从我的库中的类中做到这一点,否则它会影响我的其他正在使用它的项目。



至于从我的课程中获取值,我尝试使用方法,使用getter和setter的不同方法,以及多次更改访问修饰符。你看到的是那些迭代工作的结果。



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.

推荐答案

DbType的值为{DbType},这是无效的.RBDataOleDb无法确定哪个数据库提供者连接数据库时使用);
}
返回结果;
}

///< summary>
///注意:提供者:Microsoft.ACE.OLEDB.12.0是2010 Office System驱动程序,它具有32位和64位版本,可让您的应用程序连接
///到Access 64位环境中的Excel,Excel和文本文件。
///< / summary>
///< returns>< / returns>
public string GetConnString()
{
string conn =;


if(DbServer!= null)
{
conn =
"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; } /// <summary> /// NOTE: Provider: "Microsoft.ACE.OLEDB.12.0" is the 2010 Office System Driver, which has both 32 bit and 64 bit versions that let your app connect /// to Access, Excel and Text file in a 64bit environment. /// </summary> /// <returns></returns> public string GetConnString ( ) { string conn = ""; if ( DbServer != null ) { conn =


Provider = SQLNCLI11; Data Source = { DbServer};初始目录= [{DbName}];;
if(!string.IsNullOrEmpty(DbPassword))
conn + =
"Provider=SQLNCLI11;Data Source={DbServer};Initial Catalog=[{DbName}];"; if ( !string.IsNullOrEmpty ( DbPassword ) ) conn +=


User ID = {DbUser}; Password = {DbPassword};;
else
conn + =
"User ID={DbUser};Password={DbPassword};"; else conn +=


这篇关于控制台应用程序不写入debug.writeline而不是在我的类中填充属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆