Exemplo n.º 1
0
        private static int UpdateOrInsert(string connStr, string tableName, string procName, List <EditFieldParam> fieldList, string editWhereName, string editWhereValue, string type)
        {
            string commandText         = string.Empty;
            string str2                = string.Empty;
            string str3                = string.Empty;
            List <EditFieldParam> list = new List <EditFieldParam>();
            string str4                = string.Empty;

            foreach (EditFieldParam param in fieldList)
            {
                if ((param.FieldType == FieldType.DateTime) && param.IsTimeUpdate)
                {
                    str4 = str4 + param.FieldName + "=GETDATE(),";
                }
                if (param.IsEdit && !param.FieldName.Equals(editWhereName, StringComparison.OrdinalIgnoreCase))
                {
                    list.Add(param);
                }
            }
            if (!string.IsNullOrEmpty(str4))
            {
                str4 = "," + str4.Remove(str4.Length - 1, 1);
            }
            SqlParameter[] commandParameters = new SqlParameter[list.Count];
            int            index             = 0;
            int            count             = list.Count;

            while (index < count)
            {
                string str5 = HttpContext.Current.Request.Form[list[index].FieldName];
                commandParameters[index] = new SqlParameter("@" + list[index].FieldName, string.IsNullOrEmpty(str5) ? list[index].DefaultValue : str5);
                string str6 = commandText;
                commandText = str6 + list[index].FieldName + "=@" + list[index].FieldName + ",";
                str2        = str2 + list[index].FieldName + ",";
                str3        = str3 + "@" + list[index].FieldName + ",";
                index++;
            }
            commandText = commandText.Remove(commandText.Length - 1, 1);
            str2        = str2.Remove(str2.Length - 1, 1);
            str3        = str3.Remove(str3.Length - 1, 1);
            if (type == "edit")
            {
                if (!string.IsNullOrEmpty(procName))
                {
                    return(MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.StoredProcedure, procName, commandParameters));
                }
                commandText = "update " + tableName + " set " + commandText + str4 + " where " + editWhereName + "='" + editWhereValue + "'";
                return(MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.Text, commandText, commandParameters));
            }
            if (!string.IsNullOrEmpty(procName))
            {
                return(MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.StoredProcedure, procName, commandParameters));
            }
            return(MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.Text, "insert into " + tableName + "(" + str2 + ") values(" + str3 + ")", commandParameters));
        }
Exemplo n.º 2
0
        // Methods
        private static void Execute(string delName, string connStr, string tableName, string procName, bool isRealDel, string delFieldName, string delFieldValue, string editWhereField, bool IsCustom, string delUpdateTimeField, string tn)
        {
            string str = tableName.ToUpper().Replace("WITH(NOLOCK)", "");

            if (!string.IsNullOrEmpty(procName))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@" + editWhereField, HttpContext.Current.Request.Form["txtDel" + tn + editWhereField]) };
                if (MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.StoredProcedure, procName, commandParameters) > 0)
                {
                    MyControl_MessageBox.Msg(delName + "成功!");
                }
            }
            else if (!isRealDel)
            {
                SqlParameter[] parameterArray2 = null;
                parameterArray2 = new SqlParameter[] { new SqlParameter("@1", delFieldValue), new SqlParameter("@2", HttpContext.Current.Request.Form["txtDel" + tn + editWhereField]) };
                if (IsCustom)
                {
                    parameterArray2 = new SqlParameter[] { new SqlParameter("@1", HttpContext.Current.Request.Form["txtCustom" + tn]), new SqlParameter("@2", HttpContext.Current.Request.Form["txtDel" + tn + editWhereField]) };
                }
                if (MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.Text, " update " + str + " set " + delFieldName + "=@1" + (string.IsNullOrEmpty(delUpdateTimeField) ? "" : (", " + delUpdateTimeField + "=getdate()")) + " where " + editWhereField + "=@2", parameterArray2) > 0)
                {
                    if (IsCustom)
                    {
                        delName = "操作";
                    }
                    MyControl_MessageBox.Msg(delName + "成功!");
                }
            }
            else
            {
                SqlParameter[] parameterArray3 = new SqlParameter[] { new SqlParameter("@1", HttpContext.Current.Request.Form["txtDel" + tn + editWhereField]) };
                if (MyControl_SqlHelper.ExecuteNonQuery(connStr, CommandType.Text, " delete from " + str + " where " + editWhereField + "=@1", parameterArray3) > 0)
                {
                    MyControl_MessageBox.Msg("彻底删除成功!");
                }
            }
        }