Ejemplo n.º 1
0
        /// <summary>
        /// 通过Hashtable修改数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="pkName">字段主键</param>
        /// <param name="pkValue"></param>
        /// <param name="ht">Hashtable</param>
        /// <returns>int</returns>
        public static int UpdateByHashtable(string connectionString, string tableName, string pkName, string pkVal, Dictionary <string, string> ht)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" Update ");
            sb.Append(tableName);
            sb.Append(" Set ");
            bool isFirstValue = true;

            foreach (string key in ht.Keys)
            {
                if (isFirstValue)
                {
                    isFirstValue = false;
                    sb.Append(key);
                    sb.Append("=");
                    sb.Append("?" + key);
                }
                else
                {
                    sb.Append("," + key);
                    sb.Append("=");
                    sb.Append("?" + key);
                }
            }
            sb.Append(" Where ").Append(pkName).Append("=").Append("?" + pkName);
            ht[pkName] = pkVal;
            MySqlParameter[] _params = MySqlHelper.GetParameter(ht);
            object           _object = MySqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sb.ToString(), _params);

            return((_object == DBNull.Value) ? 0 : Convert.ToInt32(_object));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 通过Dictionary插入数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="ht">Hashtable</param>
        /// <returns>int</returns>
        public static int InsertByHashtable(string connectionString, string tableName, Dictionary <string, string> ht)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" Insert Into ");
            sb.Append(tableName);
            sb.Append("(");
            StringBuilder sp       = new StringBuilder();
            StringBuilder sb_prame = new StringBuilder();

            foreach (string key in ht.Keys)
            {
                sb_prame.Append("," + key);
                sp.Append(",?" + key);
            }
            sb.Append(sb_prame.ToString().Substring(1, sb_prame.ToString().Length - 1) + ") Values (");
            sb.Append(sp.ToString().Substring(1, sp.ToString().Length - 1) + ")");
            int _object = MySqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sb.ToString(), MySqlHelper.GetParameter(ht));

            return(_object);
        }