我能成功调用我的日志吗?我不能尝试它,因为他有时间建议谢谢 [英] Will I be able to call my log successfully? I cant try it for he time being do advise thanks

查看:54
本文介绍了我能成功调用我的日志吗?我不能尝试它,因为他有时间建议谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Data.SqlClient;
使用 System.Data;
使用 System.IO;

命名空间 ExportDataTableApplication
{
class ExportDataTable
{
DateTime datetime = DateTime.Now;
// 执行存储过程
static void Main( string [] args)
{
Console.WriteLine( 正在运行的应用程序);

DataSet ds = new DataSet();
DateTime datetime = DateTime.Now;

使用(SqlConnection conn = new SqlConnection( 测试连接字符串))
{

SqlCommand sqlComm = new SqlCommand( Test,conn);

sqlComm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(sqlComm);

da.Fill(ds);

}
// 创建csv文件
StreamWriter sw = null ;
StringBuilder sb = new StringBuilder();

foreach (DataTable dt in ds.Tables)
{

sw = new StreamWriter( string .Format( @ C:\ Users \Desktop\ConsoleApplication2 \ConsoleApplication2 \ CSV File \ Export {0} + datetime.ToString( dd-MM-yyyy_hh-mm-ss)+ 。csv,dt.TableName));

// 写入数据表列
int iColCount = dt.Columns.Count;

for int i = 0 ; i < iColCount; i ++)
{
sw.Write(dt.Columns [i]);
if (i < iColCount - 1
{
sw.Write( ) ;
}
}

sw.Write(sw.NewLine);

// 写入所有数据表行。
foreach (DataRow dr in dt.Rows)
{
for int i = 0 ; i < iColCount; i ++)
{
if ((!Convert.IsDBNull(dr [i]) )&&(i == 7 ))
{
sw.Write(dr [i] .ToString()。Replace( \ rr < span class =code-string>));
}
else
{
sw.Write(dr [i] .ToString()。Replace( ));
}

if (i < iColCount - 1
{
sw.Write( );
}
}

sw.Write(sw.NewLine);
}

sw.Close();
}
ExportDataTable program = new ExportDataTable();
program.log( );

Console.WriteLine( 应用程序成功运行);
}

// 创建日志文件

public void log( String message)
{

String oFileName = @ < span class =code-string> C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2 \Log\Log _
+ datetime.ToString( dd-MM-yyyy_hh-mm-ss)+ TXT;
if (!File.Exists(oFileName))
{
System.IO.FileStream f = File.Create(oFileName) ;
f.Close();
}

try
{
System.IO.StreamWriter writter = File.AppendText(oFileName) ;
writter.WriteLine(datetime.ToString( dd-MM-yyyy_hh-mm-ss)+ > + message);
writter.Flush();
writter.Close();
}

catch (例外e)
{
Console.WriteLine(e.Message.ToString ());
}
}

}

}

解决方案

< blockquote>那是因为无处调用 log 函数...


 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Data.SqlClient;
使用 System.Data;
使用 System.IO;

命名空间 ExportDataTableApplication
{
class ExportDataTable
{
DateTime datetime = DateTime.Now;
// 执行存储过程
static void Main( string [] args)
{
Console.WriteLine( 正在运行的应用程序);
// 在类级别创建对象//从try catch外部
ExportDataTable program = new ExportDataTable();
DataSet ds = new DataSet();
DateTime datetime = DateTime.Now;
尝试
{
使用(SqlConnection conn = new SqlConnection( 测试连接字符串))
{

SqlCommand sqlComm = new SqlCommand( 测试,conn);

sqlComm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(sqlComm);

da.Fill(ds);

}
// 创建csv文件
StreamWriter sw = null ;
StringBuilder sb = new StringBuilder();

foreach (DataTable dt in ds.Tables)
{

sw = new StreamWriter( string .Format( @ C:\ Users \Desktop\ConsoleApplication2 \ConsoleApplication2 \ CSV File \ Export {0} + datetime.ToString( dd-MM-yyyy_hh-mm-ss)+ 。csv,dt.TableName));

// 写入数据表列
int iColCount = dt.Columns.Count;

for int i = 0 ; i < iColCount; i ++)
{
sw.Write(dt.Columns [i]);
if (i < iColCount - 1
{
sw.Write( ) ;
}
}

sw.Write(sw.NewLine);

// 写入所有数据表行。
foreach (DataRow dr in dt.Rows)
{
for int i = 0 ; i < iColCount; i ++)
{
if ((!Convert.IsDBNull(dr [i]) )&&(i == 7 ))
{
sw.Write(dr [i] .ToString()。Replace( \ rr < span class =code-string>));
}
else
{
sw.Write(dr [i] .ToString()。Replace( ));
}

if (i < iColCount - 1
{
sw.Write( );
}
}

sw.Write(sw.NewLine);
}

sw.Close();
}
// 评论它来自这个地方,因为我们已经创建了对象
// ExportDataTable program = new ExportDataTable();
// program.log();

Console.WriteLine( 应用程序成功运行);
}
catch (Exception exp)
{
// 调用程序对象函数日志(并在此放置异常的属性消息,其中包含有关错误的信息);
// 它将记录
program.log(exp.Message);
}
}

// 创建日志文件

public void log(字符串消息)
{

字符串 oFileName = @ C:\Users\Desktop\ConsoleApplication2 \ConsoleApplication2 \Log\Log _ + datetime.ToString( dd-MM-yyyy_hh-mm-ss)+ 。txt;
if (!File.Exists(oFileName))
{
System.IO.FileStream f = File.Create(oFileName) ;
f.Close();
}

try
{
System.IO.StreamWriter writter = File.AppendText(oFileName) ;
writter.WriteLine(datetime.ToString( dd-MM-yyyy_hh-mm-ss)+ > + message);
writter.Flush();
writter.Close();
}

catch (例外e)
{
Console.WriteLine(e.Message.ToString ());
}
}

}

}





如果您发现任何问题,请告诉我。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace ExportDataTableApplication
{
    class ExportDataTable
    {
        DateTime datetime = DateTime.Now;
        //Execute stored procedure
        static void Main(string[] args)
        {
            Console.WriteLine("Running App");

            DataSet ds = new DataSet();
            DateTime datetime = DateTime.Now;

            using (SqlConnection conn = new SqlConnection("Test connection string"))
            {

                SqlCommand sqlComm = new SqlCommand("Test", conn);

                sqlComm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(sqlComm);

                da.Fill(ds);

            }
            //create csv file
            StreamWriter sw = null;
            StringBuilder sb = new StringBuilder();

            foreach (DataTable dt in ds.Tables)
            {

                sw = new StreamWriter(string.Format(@"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\CSV File\Export{0}" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv", dt.TableName));

                // Write data table columns
                int iColCount = dt.Columns.Count;

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }

                sw.Write(sw.NewLine);

                // Write all data table rows.
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if ((!Convert.IsDBNull(dr[i])) && (i == 7))
                        {
                            sw.Write(dr[i].ToString().Replace("\r", " "));
                        }
                        else
                        {
                            sw.Write(dr[i].ToString().Replace(",", " "));
                        }

                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }

                    sw.Write(sw.NewLine);
                }

                sw.Close();
            }
            ExportDataTable program = new ExportDataTable();
            program.log(" ");

            Console.WriteLine("App ran successfully");
        }

        //Create log file

        public void log(String message)
        {

            String oFileName = @"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\Log\Log_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".txt";
            if (!File.Exists(oFileName))
            {
                System.IO.FileStream f = File.Create(oFileName);
                f.Close();
            }

            try
            {
                System.IO.StreamWriter writter = File.AppendText(oFileName);
                writter.WriteLine(datetime.ToString("dd-MM-yyyy_hh-mm-ss") + " > " + message );
                writter.Flush();
                writter.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }

    }

}

解决方案

That's because nowhere a call for the log function...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;
 
namespace ExportDataTableApplication
{
    class ExportDataTable
    {
        DateTime datetime = DateTime.Now;
        //Execute stored procedure
        static void Main(string[] args)
        {
            Console.WriteLine("Running App");
			//Create object on class level//Outside from try catch
            ExportDataTable program = new ExportDataTable();
            DataSet ds = new DataSet();
            DateTime datetime = DateTime.Now;
			try	
			{
				using (SqlConnection conn = new SqlConnection("Test connection string"))
				{
	 
					SqlCommand sqlComm = new SqlCommand("Test", conn);
	 
					sqlComm.CommandType = CommandType.StoredProcedure;
	 
					SqlDataAdapter da = new SqlDataAdapter(sqlComm);
	 
					da.Fill(ds);
	 
				}
				//create csv file
				StreamWriter sw = null;
				StringBuilder sb = new StringBuilder();
	 
				foreach (DataTable dt in ds.Tables)
				{
	 
					sw = new StreamWriter(string.Format(@"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\CSV File\Export{0}" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv", dt.TableName));
	 
					// Write data table columns
					int iColCount = dt.Columns.Count;
	 
					for (int i = 0; i < iColCount; i++)
					{
						sw.Write(dt.Columns[i]);
						if (i < iColCount - 1)
						{
							sw.Write(",");
						}
					}
	 
					sw.Write(sw.NewLine);
	 
					// Write all data table rows.
					foreach (DataRow dr in dt.Rows)
					{
						for (int i = 0; i < iColCount; i++)
						{
							if ((!Convert.IsDBNull(dr[i])) && (i == 7))
							{
								sw.Write(dr[i].ToString().Replace("\r", " "));
							}
							else
							{
								sw.Write(dr[i].ToString().Replace(",", " "));
							}
	 
							if (i < iColCount - 1)
							{
								sw.Write(",");
							}
						}
	 
						sw.Write(sw.NewLine);
					}
	 
					sw.Close();
				}
				//Comment it form this place because we have created object already
				// ExportDataTable program = new ExportDataTable();
				// program.log(" ");
	 
				Console.WriteLine("App ran successfully");
			}
			catch(Exception exp)
			{
				//Call program object function log( and place here exception's property message which contains info about Error);
				//It will log 
				program.log(exp.Message);
			}            
        }
 
        //Create log file

        public void log(String message)
        {
 
            String oFileName = @"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\Log\Log_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".txt";
            if (!File.Exists(oFileName))
            {
                System.IO.FileStream f = File.Create(oFileName);
                f.Close();
            }
 
            try
            {
                System.IO.StreamWriter writter = File.AppendText(oFileName);
                writter.WriteLine(datetime.ToString("dd-MM-yyyy_hh-mm-ss") + " > " + message );
                writter.Flush();
                writter.Close();
            }
 
            catch (Exception e)
            { 
                Console.WriteLine(e.Message.ToString());
            }
        }
 
    }
 
}



If you find any issue then let me know.


这篇关于我能成功调用我的日志吗?我不能尝试它,因为他有时间建议谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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