C# 常用数据操作处理方法整理


                #region 数据操作
		/// <summary>
		/// 去除字符串的所有空格。
		/// </summary>
		/// <param name="text">字符串</param>
		/// <returns>字符串</returns>
		public static string StringTrimAll( string text )
		{
			string _text = ToObjectString(text);
			string returnText = String.Empty;
			char[] chars = _text.ToCharArray();
			for( int i=0;i<chars.Length;i++)
			{
				if( chars[i].ToString() != string.Empty)
					returnText += chars[i].ToString();
			}
			return returnText;
		}

		/// <summary>
		/// 去除数值字符串的所有空格。
		/// </summary>
		/// <param name="numricString">数值字符串</param>
		/// <returns>String</returns>
		public static string NumricTrimAll( string numricString )
		{
			string text = ToObjectString(numricString).Trim();
			string returnText = String.Empty;
			char[] chars = text.ToCharArray();
			for( int i=0;i<chars.Length;i++)
			{
				if( chars[i].ToString() == "+" || chars[i].ToString() == "-" || IsDouble( chars[i].ToString()) )
					returnText += chars[i].ToString();
			}
			return returnText;
		}

		/// <summary>
		/// 在数组中查找匹配对象类型
		/// </summary>
		/// <param name="array">数组</param>
		/// <param name="obj">对象</param>
		/// <returns>Boolean</returns>
		public static bool ArrayFind(Array array,object obj)
		{
			bool b = false;
			foreach(object obj1 in array)
			{
				if(obj.Equals(obj1))
				{
					b = true;
					break;
				}
			}
			return b;
		}

		/// <summary>
		/// 在数组中查找匹配字符串
		/// </summary>
		/// <param name="array">数组</param>
		/// <param name="obj">对象</param>
		/// <param name="unUpLower">是否忽略大小写</param>
		/// <returns>Boolean</returns>
		public static bool ArrayFind(Array array,string obj,bool unUpLower)
		{
			bool b = false;
			foreach(string obj1 in array)
			{
				if(!unUpLower)
				{
					if(obj.Trim().Equals(obj1.ToString().Trim()))
					{
						b = true;
						break;
					}
				}
				else
				{
					if(obj.Trim().ToUpper().Equals(obj1.ToString().Trim().ToUpper()))
					{
						b = true;
						break;
					}
				}
			}
			return b;
		}
		/// <summary>
		/// 替换字符串中的单引号。
		/// </summary>
		/// <param name="inputString">字符串</param>
		/// <returns>String</returns>
		public static string ReplaceInvertedComma( string inputString )
		{
			return inputString.Replace("'","''");
		}

		
		/// <summary>
		/// 判断两个字节数组是否具有相同值.
		/// </summary>
		/// <param name="bytea">字节1</param>
		/// <param name="byteb">字节2</param>
		/// <returns>Boolean</returns>
		public static bool CompareByteArray(byte[] bytea,byte[] byteb)
		{
			if(null == bytea || null == byteb)
			{
				return false;
			}
			else
			{
				int aLength = bytea.Length;
				int bLength = byteb.Length;
				if(aLength != bLength)
					return false;
				else
				{
					bool compare = true;
					for(int index = 0; index < aLength; index++)
					{
						if(bytea[index].CompareTo(byteb[index]) != 0)
						{
							compare = false;
							break;
						}
					}
					return compare;
				}
			}
		}

		
		/// <summary>
		/// 日期智能生成。
		/// </summary>
		/// <param name="inputText">字符串</param>
		/// <returns>DateTime</returns>
		public static string BuildDate( string inputText )
		{
			try
			{
				return DateTime.Parse( inputText ).ToShortDateString();
			}
			catch
			{
				string text = NumricTrimAll( inputText );
				string year = DateTime.Now.Year.ToString();
				string month = DateTime.Now.Month.ToString();
				string day = DateTime.Now.Day.ToString();
				int length = text.Length;
				if( length == 0 )
					return String.Empty;
				else
				{
					if( length<=2 )
						day = text;
					else if( length<=4 )
					{
						month = text.Substring(0,2);
						day = text.Substring(2,length-2);
					}
					else if( length<=6 )
					{
						year = text.Substring(0,4);
						month = text.Substring(4,length-4);
					}
					else if( length>6)
					{
						year = text.Substring(0,4);
						month = text.Substring(4,2);
						day = text.Substring(6,length-6);
					}
					try
					{
						return DateTime.Parse( year+"-"+month+"-"+day ).ToShortDateString();
					}
					catch
					{
						return String.Empty;
					}
				}
			}
		}

		

		/// <summary>
		/// 检查文件是否真实存在。
		/// </summary>
		/// <param name="path">文件全名(包括路径)。</param>
		/// <returns>Boolean</returns>
		public static bool IsFileExists(string path)
		{
			try
			{
				return File.Exists(path);
			}
			catch
			{	return false; }
		}

		/// <summary>
		/// 检查目录是否真实存在。
		/// </summary>
		/// <param name="path">目录路径.</param>
		/// <returns>Boolean</returns>
		public static bool IsDirectoryExists(string path)
		{
			try
			{
				return Directory.Exists(Path.GetDirectoryName(path));
			}
			catch
			{	return false; }
		}
		
		/// <summary>
		/// 查找文件中是否存在匹配行。
		/// </summary>
		/// <param name="fi">目标文件.</param>
		/// <param name="lineText">要查找的行文本.</param>
		/// <param name="lowerUpper">是否区分大小写.</param>
		/// <returns>Boolean</returns>
		public static bool FindLineTextFromFile(FileInfo fi,string lineText,bool lowerUpper)
		{
			bool b = false;
			try
			{
				if(fi.Exists)
				{
					StreamReader sr=new StreamReader(fi.FullName);
					string g = "";
					do
					{
						g=sr.ReadLine();
						if(lowerUpper)
						{
							if(ToObjectString(g).Trim() == ToObjectString(lineText).Trim())
							{
								b = true;
								break;
							}
						}
						else
						{
							if(ToObjectString(g).Trim().ToLower() == ToObjectString(lineText).Trim().ToLower())
							{
								b = true;
								break;
							}
						}
					}
					while(sr.Peek()!=-1);
					sr.Close();
				}
			}
			catch
			{	b =false;	}
			return b;
		}


		/// <summary>
		/// 判断父子级关系是否正确。
		/// </summary>
		/// <param name="table">数据表。</param>
		/// <param name="columnName">子键列名。</param>
		/// <param name="parentColumnName">父键列名。</param>
		/// <param name="inputString">子键值。</param>
		/// <param name="compareString">父键值。</param>
		/// <returns></returns>
		public static bool IsRightParent(DataTable table,string columnName,string parentColumnName,string inputString,string compareString)
		{
			ArrayList array = new ArrayList();
			SearchChild(array,table,columnName,parentColumnName,inputString,compareString);
			return array.Count == 0;
		}

		// 内部方法
		private static void SearchChild(ArrayList array,DataTable table,string columnName,string parentColumnName,string inputString,string compareString)
		{
			DataView view = new DataView(table);
			view.RowFilter = parentColumnName+"='"+ReplaceInvertedComma(inputString.Trim())+"'";//找出所有的子类。
			//查找表中的数据的ID是否与compareString相等,相等返回 false;不相等继续迭代。
			for(int index = 0 ;index < view.Count;index ++)
			{
				if(Utility.ToObjectString(view[index][columnName]).ToLower() == compareString.Trim().ToLower())
				{
					array.Add("1");
					break;
				}
				else
				{
					SearchChild(array,table,columnName,parentColumnName,Utility.ToObjectString(view[index][columnName]),compareString);
				}
			}
		}

		#endregion

欢迎分享,(木庄网络博客交流QQ群:562366239

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论