示例#1
0
        /// <summary>
        /// Programmatically generate an insert command
        /// </summary>
        /// <param name="args"> The array of Values in an insert command </param>
        /// <returns> The insert command string </returns>
        protected string insertValues(params object[] args)
        {
            try
            {
                insertQuery = "INSERT INTO " + tableName + "(";
                int n         = fieldsname.Length;
                int lastIndex = n - 1;
                for (int i = 0; i < n; i++)
                {
                    if (i == lastIndex)
                    {
                        insertQuery += fieldsname[i];
                    }
                    else
                    {
                        insertQuery += fieldsname[i] + ", ";
                    }
                }

                insertQuery += ") VALUES(";

                n         = args.Length;
                lastIndex = n - 1;
                for (int i = 0; i < n; i++)
                {
                    string argsWithQuote    = "'{" + i + "}'";
                    string argsWithoutQuote = "{" + i + "}";

                    if (args[i] is Int32 || args[i] is Decimal)
                    {
                        if (i == lastIndex)
                        {
                            insertQuery += argsWithoutQuote;
                        }
                        else
                        {
                            insertQuery += argsWithoutQuote + ", ";
                        }
                    }

                    if (args[i] is string)
                    {
                        string foo = (string)args[i];
                        if (foo.Contains('(') && foo.Contains(')') || foo == "null")
                        {
                            if (i == lastIndex)
                            {
                                insertQuery += argsWithoutQuote;
                            }
                            else
                            {
                                insertQuery += argsWithoutQuote + ", ";
                            }
                            continue;
                        }

                        if (i == lastIndex)
                        {
                            insertQuery += argsWithQuote;
                        }
                        else
                        {
                            insertQuery += argsWithQuote + ", ";
                        }
                    }
                }

                insertQuery += ")";
                return(String.Format(insertQuery, args));
            }
            catch (Exception ex)
            {
                ErrorLog.Log(ex);
                return(string.Empty);
            }
        }