多进程同时操作数据库并同时写日志文件,方法中带参数传递

 
多进程同时操作数据库并同时写日志文件 
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=host2;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=host2;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=host;database=db;uid=sa;pwd=23WSXCDE#@";

                try
                {
                    using (SqlConnection cn = new SqlConnection(con2))
                    {
                        if (cn.State == ConnectionState.Closed)
                            cn.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = cn;
                        cmd.CommandText = "EXEC Pro_Thread @TM_No=" + _TM_NO;
                        object O = cmd.ExecuteScalar();
                        WriteLog("得到result=" + O.ToString());
                        if (!string.IsNullOrEmpty(O.ToString()))
                            order_ID = int.Parse(O.ToString());
                        if (cn.State == ConnectionState.Open)
                            cn.Close();
                    }
                    WriteLog("得到OrderID=" + order_ID.ToString());
                    WriteLog("执行sql=" + "EXEC Pro_Thread @TM_No=" + _TM_NO);
                }
                catch (Exception err)
                {
                    WriteLog("3:Failed: " + err.Message + ";\t");
                }
            }
        }
        public void Delegate_Insert_Fun()
        {

        }
    }
    class GetDataFromDB
    {
        static void Main(string[] args)
        {
            //Log L = new Log();
            //L.WriteLog();
            //for (int i = 0; i < 10; i++)
            //{
            //    //Thread T = new Thread(new ThreadStart(L.WriteLog));
            //    Thread T = new Thread(new ThreadStart(L.insertReturnID2));
            //    T.Name = "第" + (i + 1).ToString() + "个线程";
            //    T.Start();
            //}
            //L.WriteLog();

            for (int TM_NO = 3466; TM_NO < 3480; TM_NO++)
            {
                Log L = new Log(TM_NO);
                Thread T = new Thread(new ThreadStart(L.insertReturnID2));
                T.Name = TM_NO.ToString();
                T.Start();
            }
        }
    }
    public class OrderBuy
    {
        public string FileName { get; set; }

        public string TM_Name { get; set; }

        public DateTime UploadTime { get; set; }

        public int UploadUserID { get; set; }

        public string SourceType { get; set; }

        public string SeasonOfTM { get; set; }

        public string OrderType { get; set; }

        public int TM_NO { get; set; }

        public string Remark { get; set; }

        public string TM_CHANNEL { get; set; }

        public string Region { get; set; }

        public int MileStoneID { get; set; }
    }
}

 

 

 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值