程序调试时可以正常执行指定的操作,直接运行时则不行,求助我想让程序在收到数据后插入数据库或者写入XML,在调试中设置断点,每次都能命中,都能成功写入
但是每次直接运行时就不能写入这是什么奇葩问题啊,求助!
谢谢各位

解决方案 »

  1.   

    代码是这样的private static string insertStr = "";
            private static string usingStr = "";
            private static string RealName = "";
            private static void DataReceviedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
            {
                SerialPort sp = (SerialPort)sender;
                string indata = sp.ReadExisting();
                insertStr += indata;            if (insertStr.Length > 12)
                {
                    string insertStr2 = insertStr.Split('o')[0];
                    insertStr = insertStr2;
                }            foreach (char ch in insertStr)
                {
                    if (ch != 'E' && ch != 'S'&& ch != 'o')
                    {
                        usingStr += ch;
                    }
                }
                
                switch (usingStr)
                {
                    case "2011072069\0":
                        RealName = "a";
                        break;
                    case "2010073065\0":
                        RealName = "b";
                        break;
                    default:
                        break;
                }            if (RealName != "")
                {
                    if (insertStr.Contains("E"))
                    {
                        string Sqlcom = "INSERT INTO tb_out(RealName, Num, Date) VALUES('" + RealName + "'," + "'" + usingStr + "', GetDate());";
                        ExecuteSQL(Sqlcom);
                        usingStr = string.Empty;
                        insertStr = string.Empty;
                        RealName = string.Empty;
                    }
                    if (insertStr.Contains("S"))
                    {
                        string Sqlcom = "INSERT INTO tb_in(RealName, Num, Date) VALUES('" + RealName + "'," + "'" + usingStr + "', GetDate());";
                        ExecuteSQL(Sqlcom);
                        usingStr = string.Empty;
                        insertStr = string.Empty;
                        RealName = string.Empty;
                    }
                }
                
            }        public static void ExecuteSQL(string SQLwords)
            {
                //执行插入、删除、更新的函数
                string myConStr = "user id=sa; password=sa;";
                myConStr += "Initial Catalog = AccessControl; Server=PC2012050719LFQ;";
                //myConStr += "Connect Timeout = 10";            SqlConnection myCon = new SqlConnection(myConStr);
                myCon.Open();
                SqlCommand myCom = new SqlCommand();
                myCom.CommandType = CommandType.Text;
                myCom.CommandText = SQLwords;
                myCom.Connection = myCon;
                try
                {
                    myCom.ExecuteNonQuery();
                }
                catch
                {
                }
                myCon.Close();
            }