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

📄 addform.cs

📁 初学Csharp
💻 CS
📖 第 1 页 / 共 2 页
字号:
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(424, 397);
			this.Controls.Add(this.button_close);
			this.Controls.Add(this.button_OK);
			this.Controls.Add(this.groupBox1);
			this.Name = "AddForm";
			this.Text = "添加代码";
			this.Load += new System.EventHandler(this.AddForm_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void AddForm_Load(object sender, System.EventArgs e)
		{
		    OleDbCommand command=new OleDbCommand();
			OleDbDataAdapter oleAdapter=new OleDbDataAdapter(); 
			DataSet dset=new DataSet(); 
			connection.Open();
			command.Connection=connection;
			string sqlstr;
			sqlstr="select LANNAME from LanguageInfo";
			command.CommandText=sqlstr;
			OleDbDataReader DataReader=command.ExecuteReader(); 
            
			//初始化语言类别等属性
			try
			{
				while(DataReader.Read())
				{
					this.comboBox_Lan.Items.Add(DataReader.GetString(0));   
				}
			}
			catch(OleDbException ee)
			{ 
                MessageBox.Show (ee.Message,"错误!");
			}
			DataReader.Close(); 
			////
			sqlstr="select FORMNAME from FormInfo";
			command.CommandText=sqlstr;
			DataReader=command.ExecuteReader(); 
			try
			{
				while(DataReader.Read())
				{
					this.comboBox_form.Items.Add(DataReader.GetString(0));   
				}
			}
			catch(OleDbException ee)
			{ 
				MessageBox.Show (ee.Message,"错误!");
			}
            DataReader.Close();
			////
			sqlstr="select PLATFORMNAME from PlatformInfo";
			command.CommandText=sqlstr;
			DataReader=command.ExecuteReader();
			try
			{
				while(DataReader.Read())
				{
					this.comboBox_plat.Items.Add(DataReader.GetString(0));   
				}
			}
			catch(OleDbException ee)
			{ 
				MessageBox.Show (ee.Message,"错误!");
			}
			DataReader.Close();
			////
			sqlstr="select CLASSNAME from CLASSINFO";
			command.CommandText=sqlstr;
			DataReader=command.ExecuteReader(); 
			try
			{
				while(DataReader.Read())
				{
					this.comboBox_class.Items.Add(DataReader.GetString(0));   
				}
			}
			catch(OleDbException ee)
			{ 
				MessageBox.Show (ee.Message,"错误!");
			}
            DataReader.Close();
			//生成可用编号
			sqlstr="select SCRNUM from SourceInfo";
			command.CommandText=sqlstr;
			oleAdapter.SelectCommand=command;
//			DataReader=command.ExecuteReader();
            int length=0;
			int i=0;
			try
			{   
				oleAdapter.Fill(dset,"tmp"); 
				length=dset.Tables["tmp"].Rows.Count;   
			}
			catch(OleDbException ee)
			{ 
				MessageBox.Show (ee.Message,"错误!");
			}
			finally
			{
				connection.Close();
			}
//            DataReader.Close();
			Array tempArray= Array.CreateInstance(typeof(int),length); 
			for(i=0;i<length;i++)
			{
				DataRow row;
				row=dset.Tables["tmp"].Rows[i];
				tempArray.SetValue(row["SCRNUM"],i); 
			}
		    //查找当前最大编号
			if(tempArray.Length!=0)
			{
				Array.Sort(tempArray); 
				m_SrcNum=(int)tempArray.GetValue(tempArray.Length-1)+1; 
			}
			else
			{
                m_SrcNum=1;
			}
			this.textBox_sourceNum.Text=m_SrcNum.ToString();
			//获取当前日期
            DateTime date1=DateTime.Now;
			this.textBox_Cdata.Text=date1.ToString();  
		}

		private void button_openfile_Click(object sender, System.EventArgs e)
		{
		    openFileDialog1.ShowDialog(this);
            this.textBox_SourceFile.Text=openFileDialog1.FileName;  
		}

		public void SetConnection(OleDbConnection conn)
		{
		  connection=conn;
		}

		private void groupBox1_Enter(object sender, System.EventArgs e)
		{
		   
		}

		private void button_close_Click(object sender, System.EventArgs e)
		{
			this.Close(); 
		}

		private void button_OK_Click(object sender, System.EventArgs e)
		{
		    //拷贝文件到指定目录
			#region 拷贝文件
			bool flag=false;
			string destpath=Application.StartupPath;
			string temp=this.textBox_SourceFile.Text ;
			string filename=temp.Substring(temp.LastIndexOf("\\")+1);
			destpath=destpath+"\\source"+this.textBox_dict.Text+filename; 
			if(temp=="")
			{
                MessageBox.Show("没有指定源文件","错误");
                return;
			}
            try
			{
				File.Copy(this.textBox_SourceFile.Text,destpath,false); 
			}
			catch(Exception ee)
			{
               MessageBox.Show(ee.ToString(),"错误");  
			   flag=true;
			}
	        #endregion
			//插入信息
			if(!flag)
			{   
                //1检查数据有效性
                if(this.comboBox_class.Text=="")
                {
                     MessageBox.Show("领域不能为空","错误");
                     return;
                }
				if(this.comboBox_plat.Text=="")
                {
                     MessageBox.Show("平台不能为空","错误");
                     return;
                }  
		        if(this.comboBox_Lan.Text=="")
                {
                     MessageBox.Show("语言不能为空","错误");
                     return;
                }  
				if(this.comboBox_form.Text=="")
                {
			         MessageBox.Show("类别不能为空","错误");
                     return;
                }
		        if(this.textBox_key.Text=="")
                {
                      MessageBox.Show("关键词不能为空","错误");
                      return;
                }
                #region 查询各属性对应ID
				OleDbCommand command=new OleDbCommand();
				OleDbDataReader DataReader; 
                string sqlstr;
                string lanid="",platid="",formid="",classid=""; 
				DateTime date=DateTime.Now;
				//////////////////////
                sqlstr="select LANID from LanguageInfo where LANNAME='"+comboBox_Lan.Text+"'";
                command.Connection=connection;
				command.CommandText=sqlstr;
				connection.Open(); 
				DataReader=command.ExecuteReader(); 
				try
				{   
					if(DataReader.Read())
						lanid=DataReader.GetString(0);   
					
				}
				catch(OleDbException ee)
				{ 
					MessageBox.Show (ee.Message,"错误!");
				}
				finally
				{
					DataReader.Close();
				}
                sqlstr="select FORMID from FormInfo where FORMNAME='"+comboBox_form.Text+"'";
				command.CommandText=sqlstr;
				DataReader=command.ExecuteReader();
				try
				{   
					if(DataReader.Read())
						formid=DataReader.GetString(0);   
					
				}
				catch(OleDbException ee)
				{ 
					MessageBox.Show (ee.Message,"错误!");
				}
				finally
				{
					DataReader.Close();
				}            
				sqlstr="select PLATFORMID from PlatformInfo where PLATFORMNAME='"+comboBox_plat.Text+"'";
				command.CommandText=sqlstr;
				DataReader=command.ExecuteReader();
				try
				{   
					if(DataReader.Read())
						platid=DataReader.GetString(0);   
					
				}
				catch(OleDbException ee)
				{ 
					MessageBox.Show (ee.Message,"错误!");
				}
				finally
				{
					DataReader.Close();
				}
				sqlstr="select CLASSID from CLASSINFO where CLASSNAME='"+comboBox_class.Text+"'";
				command.CommandText=sqlstr;
				DataReader=command.ExecuteReader();
				try
				{   
					if(DataReader.Read())
						classid=DataReader.GetString(0);   
					
				}
				catch(OleDbException ee)
				{ 
					MessageBox.Show (ee.Message,"错误!");
				}
				finally
				{
					DataReader.Close();
				}
				connection.Close();
				#endregion
				//3插入//(SCRNUM,CLASSID,FORMID,LANID,FUNCDSC,KEYWORD,PLATFORMID,DIRECTORY)
				#region 插入 
				bool flag1=false;
				 
				sqlstr="Insert into SourceInfo values("+this.textBox_sourceNum.Text+ ",'"+classid+"','"+formid+"','"+lanid+"',"+"#"+date.ToShortDateString()+"#,'"+textBox_func.Text+"','"+this.textBox_key.Text+"','"+platid+"','"+this.textBox_dict.Text+filename+"')";       
                command.Connection=connection;
				command.CommandText=sqlstr;
				try
				{
					connection.Open(); 
					command.ExecuteReader(); 
				}
				catch (OleDbException ee)
				{
                    MessageBox.Show (ee.Message,"错误!");
                    flag1=true;
				}
				finally
				{
					connection.Close(); 
				}
				if(!flag1)
				{//成功所有控件清空
                   m_SrcNum++;
                   AllClear();
				}
				else
				{
					//没有成功
                    //删除拷贝的文件
					File.Delete(destpath); 
				}
				#endregion
				
			}
			 
		}

		private int m_SrcNum;

		private void comboBox_Lan_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		  this.textBox_dict.Text = "\\"+this.comboBox_Lan.Text+"\\";
		}

		private void AllClear()
		{
		     this.textBox_Cdata.Text=DateTime.Now.ToString();
			 this.textBox_dict.Text ="";
			 this.textBox_func.Text ="";
			 this.textBox_key.Text ="";
			 this.textBox_SourceFile.Text ="";
			 this.textBox_sourceNum.Text =m_SrcNum.ToString() ;
			 this.comboBox_Lan.Text ="";
			 this.comboBox_plat.Text ="";
			 this.comboBox_class.Text ="";
			 this.comboBox_form.Text =""; 
		}

	}
}

⌨️ 快捷键说明

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