日期:2014-05-16  浏览次数:20574 次

多进程同时操作数据库并同时写日志文件,方法中带参数传递
多进程同时操作数据库并同时写日志文件
namespace ThreadConsole
{
    class Log
    {
        public delegate void Delegate_Insert();
        private int _TM_NO;

        public int TM_NO
        {
            get { return _TM_NO; }
            set { _TM_NO = value; }
        }
        public Log()
        {
        }
        public Log(int tm_no)
        {
            _TM_NO = tm_no;
        }
        public void WriteLog()
        {
            //lock (this)
            //{
            //    StreamWriter sw = File.AppendText("E:\\WebSite\\RMAAUTO\\LOG\\Test.log");
            //    sw.WriteLine(DateTime.Now.ToString() + "  " + Thread.CurrentThread.Name);
            //    sw.Flush();
            //    sw.Close();
            //}
        }
        public void WriteLog(string content)
        {
            FileStream stream = new FileStream("E:\\WebSite\\RMAAUTO\\LOG\\Test.log", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            StreamWriter sw = new StreamWriter(stream, Encoding.UTF8);
            sw.WriteLine(DateTime.Now.ToString() + "  " + Thread.CurrentThread.Name + "  " + content);
            sw.Flush();
            sw.Close();

        }
        public void insert()
        {
            string con1 = "Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=88888888;Max Pool Size = 51200;";
            string con2 = "Data Source=128.45.4.91;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123456;Max Pool Size = 51200;";
            using (SqlConnection cn = new SqlConnection(con2))
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                string sql = "";
                for (int i = 0; i < 10; i++)
                {
                    sql += "INSERT INTO dbo.Thread( ThreadName ,InsertTime ,UpdateTime ,Remark)VALUES  ('" + Thread.CurrentThread.Name + "',GETDATE() ,GETDATE() ,'');";
                }
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                cn.Close();
            }
        }
        public void insertReturnID()
        {
            int ID = 0;
            string con2 = "Data Source=128.45.4.91;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123456;Max Pool Size = 51200;";
            for (int i = 0; i < 1; i++)
            {
                using (SqlConnection cn = new SqlConnection(con2))
                {
                    cn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cn;
                    string sql = "INSERT INTO dbo.Thread( ThreadName ,InsertTime ,UpdateTime ,Remark)VALUES  ('" + Thread.CurrentThread.Name + "',GETDATE() ,GETDATE() ,'') Select @@IDENTITY;";
                    cmd.CommandText = sql;
                    object O = cmd.ExecuteScalar();
                    if (!string.IsNullOrEmpty(O.ToString()))
                        ID = int.Parse(O.ToString());
                    cn.Close();
                }
                WriteLog(ID.ToString());
            }
        }
        public void insertReturnID2()
        {
            lock (this)
            {
                int order_ID = 0;
                string con2 = "server=128.45.4.34;database=RMA0130;uid=sa;pwd=23WSXCDE#@";

                try
                {
                    using (SqlConnection cn = new SqlConnection(con2))
                    {
                        if (cn.State == ConnectionState.Closed)
                            cn.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = c