⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 频繁模式挖掘搜索给顶数据几中反复出现的联系.介绍发现事物或关系数据库中关联
💻 CS
📖 第 1 页 / 共 2 页
字号:
			
			try
			{
				FileStream s = new FileStream(fileName,FileMode.Open,FileAccess.Read);
				fileStreamLengthTemp = s.Length;
				s.Close();

				if(fileStreamLength<fileStreamLengthTemp)
				{
					fileStreamLength = fileStreamLengthTemp;
					this.textBox_txtFileLengh.Text =fileStreamLength.ToString();
					if(this.radioButton_auto.Checked==true)
					{
						//MessageBox.Show("自动");
						
						//插入AccessValueTable
						ValuesFromTxt = readFile();
						IsOKAccess = WriteToAccess(ValuesFromTxt,"ValueTable","",GangTiHao);
						if(IsOKAccess)
						{
							this.label_Message.Text = "写入Access数据库成功!" + "\n";
							ValueTableData = SelectData("ValueTable");
							IsOKSQL = InsertIntoSQL(ValueTableData);
							if(IsOKSQL)
							{
								this.label_Message.Text +="写入SQL数据库成功!" + "\n";
								UpdateAccess();
							}
						}
					}
					else
					{
						if(this.radioButton_manual.Checked==true)
						{
							//MessageBox.Show("手动");

							//插入AccessValueTableTemp
							ValuesFromTxt = readFile();
							IsOKAccess = WriteToAccess(ValuesFromTxt,"ValueTableTemp","",GangTiHao);
							this.dataGrid_tempValue.DataSource =  SelectData("ValueTableTemp").Tables[0];
//							if(IsOKAccess)
//							{
//								ValueTableData = SelectData();
//								IsOKSQL = InsertIntoSQL(ValueTableData);
//								if(IsOKSQL)
//								{
//									UpdateAccess();
//								}
//							}
						}
					}
					
				}

			}
			catch(Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "出错");
			}

		}

		/// <summary>
		/// 读txt文件
		/// </summary>
		/// <returns></returns>
		private string  readFile()
		{			
			string buffer ="";
			string TxtContent = "";
			try
			{
				FileStream s = new FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
				StreamReader r = new StreamReader(s);
				r.BaseStream.Seek(-40,SeekOrigin.End);//设置当前流中的位置			
                
				while((buffer=r.ReadLine()) != null)
				{
					TxtContent=buffer; 				    
				}
				r.Close();
				s.Close();
				//TxtContent就是最后一行记录
				return TxtContent; 
			}
			catch(Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "readFile()读txt文件出错");
				return null;
			}

		}
		/// <summary>
		/// 将读出的字符串写入Access数据库
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		private bool  WriteToAccess(string data,string tableName,string DataBaseCode,string GangTihao)
		{
						
			//拆分从txt文件中读出的字符串
			string delimStr = " ";
			char [] delimStrArray = delimStr.ToCharArray();
			string [] values = new string[3];			
			values = data.Split(delimStrArray);
			 		
			float DataBaseValue1 = float.Parse(values[0]);
			float DataBaseValue2 = float.Parse(values[1]);
				//DateTime DataBaseUpdateTime = DateTime.Parse(values[2]);
			string DataBaseUpdateTime = values[2];
			
			//界面显示
			this.textBox_Value1.Text = DataBaseValue1.ToString();
			this.textBox_Value2.Text = DataBaseValue2.ToString();
			this.textBox_ValueTime.Text = DataBaseUpdateTime.ToString();

			//与数据库连接并写入Access
			string ConStr = System.Configuration.ConfigurationSettings.AppSettings.GetValues("ConnectionStringAccess")[0].ToString(); 

			OleDbConnection oc = new OleDbConnection(ConStr);
			OleDbCommand command = new OleDbCommand();
			try
			{	
				command.CommandType = CommandType.Text;

				command.CommandText = "INSERT INTO "+tableName + " (Value1,Value2,UpdateTime,Code,OPNumber,IsOK) VALUES ("+ DataBaseValue1+","+ DataBaseValue2 +"," + "'"+DataBaseUpdateTime +"'" + ",'"+ DataBaseCode +" ','"+GangTihao+" ',1)";
				string aaa = command.CommandText;
				
				command.Connection = oc;
				oc.Open();
				command.ExecuteNonQuery();
				oc.Close();

				return true;
			}
			catch(Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "数据库连接出错");
				oc.Close();
				return false;
			}

		}
		/// <summary>
		/// 查询Access中没有上传的数据,IsOK字段是1,返回个数据集
		/// </summary>
		/// <returns></returns>
		private DataSet SelectData(string tableName)
		{
			
			string ConStr = System.Configuration.ConfigurationSettings.AppSettings.GetValues("ConnectionStringAccess")[0].ToString(); 
			OleDbConnection oc = new OleDbConnection(ConStr);
			OleDbDataAdapter oa;
			try
			{
				//打开数据库连接
				oc.Open();
//				if(tableName=="ValueTableTemp")
//				{
//					oa = new OleDbDataAdapter("select * from " +tableName +" where IsOK=1 ", oc);
//				}
//				else
//				{
//					oa = new OleDbDataAdapter("select Id, Value1, Value2, UpdateTime, Code, OPNumber, IsOK from " +tableName +" where IsOK=1 ", oc);
//				}
				oa = new OleDbDataAdapter("select * from " +tableName +" where IsOK=1 ", oc);
				DataSet ds = new DataSet();

				oa.Fill(ds,tableName);
				return ds;

			}
			catch (Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "SelectData()数据库Access查询出错");
                return null;

			}
			finally
			{
				//关闭数据库连接
				oc.Close();
			}
		}
		/// <summary>
		/// 把没有上传的数据插入SQL
		/// </summary>
		/// <param name="dataset"></param>
		/// <returns></returns>
		private bool InsertIntoSQL(DataSet dataset)
		{
			//用参数方法写如数据库
			//DataSet ValueTableData = SelectData();
			//this.dataGrid1.DataSource = dataset.Tables[0];

			string ConStr = System.Configuration.ConfigurationSettings.AppSettings.GetValues("ConnectionStringSQLServer")[0].ToString(); 	
		
			SqlConnection oc = new SqlConnection(ConStr);
			SqlCommand command = new SqlCommand ();
			
			string selectStr = "select * From " + dataset.Tables[0].TableName;
		
			try
			{
				SqlDataAdapter adp = new SqlDataAdapter(selectStr,ConStr);
				SqlCommandBuilder scb = new SqlCommandBuilder(adp);
				
				DataTable dt = new DataTable(dataset.Tables["ValueTable"].TableName);
				adp.Fill(dt);
				int j = 0;
				for (int i = 0; i < dataset.Tables["ValueTable"].Rows.Count; i++)
				{ 
					DataRow row = dt.NewRow();
					//int a = resultDataTable.Columns.Count;
					for (j = 0; j < dataset.Tables["ValueTable"].Columns.Count; j++)
					{                   
						row[j] = dataset.Tables["ValueTable"].Rows[i][j].ToString();
					}
					dt.Rows.Add(row);
				}
				//MessageBox.Show("保存成功!");
				adp.Update(dt);
				return true;
				
			}
			catch (Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "InsertIntoSQL() 数据库SQL插入出错");
				return false;
			}

		}

		/// <summary>
		///上传成功后更新 Access,也就是把IsOK字段置成2
		/// </summary>

		private void UpdateAccess()
		{
			string ConStr = System.Configuration.ConfigurationSettings.AppSettings.GetValues("ConnectionStringAccess")[0].ToString(); 

			OleDbConnection oc = new OleDbConnection(ConStr);
			OleDbCommand command = new OleDbCommand();
			try
			{
				command.CommandType = CommandType.Text;
				command.CommandText = "update ValueTable set IsOK=2 where IsOK = 1";
				string aaa = command.CommandText;
				command.Connection = oc;
				oc.Open();
				command.ExecuteNonQuery();
				oc.Close();
			}
			catch(Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "UpdateAccess() 数据库Access更新出错");
				oc.Close();

			}

		}

				
		private void dataGrid_tempValue_CurrentCellChanged(object sender, System.EventArgs e)
		{
			int rowNumber = this.dataGrid_tempValue.CurrentCell.RowNumber;
			int columnNumber = this.dataGrid_tempValue.CurrentCell.ColumnNumber;
			DataTable ValueTableTemp = SelectData("ValueTableTemp").Tables[0];
			this.textBox_ValueTemp1.Text = ValueTableTemp.Rows[rowNumber][1].ToString();
			this.textBox_ValueTemp2.Text = ValueTableTemp.Rows[rowNumber][2].ToString();
			this.textBox_ValueTempTime.Text = ValueTableTemp.Rows[rowNumber][3].ToString();
		
		}

		private void button_submit_Click(object sender, System.EventArgs e)
		{
			string Updatedata = this.textBox_ValueTemp1.Text +" "+this.textBox_ValueTemp2.Text + " "+this.textBox_ValueTempTime.Text;
			string codeNumber = this.textBox_CodeNumber.Text;
			bool TempDataIsOKAccess = WriteToAccess(Updatedata,"ValueTable",codeNumber,GangTiHao);
			if(TempDataIsOKAccess == true)
			{
				this.label_Message.Text = "写入Access数据库成功!" + "\n";
				//上传到SQL
				DataSet ValueTableData = SelectData("ValueTable");
				bool IsOKSQL = InsertIntoSQL(ValueTableData);
				if(IsOKSQL)
				{
					this.label_Message.Text += "写入SQL数据库成功!" + "\n";
					//更新数据库Access
					UpdateAccess();
					deleteValueTableTemp();
                    this.dataGrid_tempValue.DataSource = SelectData("ValueTableTemp").Tables[0];
				}

			}

		}
		private void deleteValueTableTemp()
		{
			int rowNumber = this.dataGrid_tempValue.CurrentCell.RowNumber;
			DataTable ValueTableTemp = SelectData("ValueTableTemp").Tables[0];
			int DataBaseid = int.Parse(ValueTableTemp.Rows[rowNumber][0].ToString());

			string ConStr = System.Configuration.ConfigurationSettings.AppSettings.GetValues("ConnectionStringAccess")[0].ToString(); 

			OleDbConnection oc = new OleDbConnection(ConStr);
			OleDbCommand command = new OleDbCommand();
			
			try
			{
				command.CommandType = CommandType.Text;
				//command.CommandText = @"delete from ValueTableTemp where Value1 = "+ float.Parse(this.textBox_ValueTemp1.Text) + " and Value2 = " +float.Parse(this.textBox_ValueTemp2.Text) + " and UpdateTime= '" +this.textBox_ValueTempTime.Text + "'";
				
				command.CommandText = "delete from ValueTableTemp where Id= " +DataBaseid ;
				command.Connection = oc;
				oc.Open();
				command.ExecuteNonQuery();
				oc.Close();
			}
			catch(Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "deleteValueTableTemp() 数据库Access删除出错");
				oc.Close();

			}
		}
		

		private void radioButton_auto_CheckedChanged(object sender, System.EventArgs e)
		{
			this.label_Message.Text = "";
			this.dataGrid_tempValue.Enabled =false;
			this.textBox_CodeNumber.Enabled =false;
			this.button_submit.Enabled = false;

			this.textBox_ValueTemp1.Text = "";
			this.textBox_ValueTemp2.Text = "";
			this.textBox_ValueTempTime.Text ="";

		}

		private void radioButton_manual_CheckedChanged(object sender, System.EventArgs e)
		{
			this.label_Message.Text = "";
			this.dataGrid_tempValue.Enabled =true;
			this.textBox_CodeNumber.Enabled =true;
			this.button_submit.Enabled = true;
		
			this.textBox_ValueTemp1.Text = "";
			this.textBox_ValueTemp2.Text = "";
			this.textBox_ValueTempTime.Text ="";
		}
		private void Form1_Load(object sender, System.EventArgs e)
		{
			string errorLog = "\r\n开机时间为: " + System.DateTime.Now.ToString();
			ApplicationLog.WriteLog(new Exception(errorLog), "初始化系统");		
		}

		private void Form1_Closed(object sender, System.EventArgs e)
		{
			string errorLog = "\r\n关机时间:" + System.DateTime.Now.ToString();
			ApplicationLog.WriteLog(new Exception(errorLog), "退出系统");
		}

		private void button_PrintCode_Click(object sender, System.EventArgs e)
		{
			try
			{
				printBarCodeSystem.PrintBarCode(showProduction.textBox_EngineCode.Text);

			}
			catch(Exception err)
			{
				string errorLog = "\r\n错误位置为:" + err.StackTrace + "  错误信息:" + err.Message + "  出错时间:" + System.DateTime.Now.ToString();
				ApplicationLog.WriteLog(new Exception(errorLog), "button_PrintCode_Click 测试打印条码,并且输出到激光条码打印机");
				

			}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -