动态表的SQL插入方法

来源:互联网 发布:mac拔出u盘 编辑:程序博客网 时间:2024/06/03 23:19
        public OracleCommand cmd = null;
        public OracleConnection conn = null;
        private WriteData LA = new WriteData();
        public bool insertValues(string TableName, string[] ColumnName, Type[] ColumnType, object[] ColumnValues)
        {
            bool flag = false;
            try
            {
                this.conn.Open();
                if ((ColumnName.Length == ColumnType.Length) && (ColumnName.Length == ColumnValues.Length))
                {
                    string strCmd = "Insert Into" + TableName + "(";
                    for (int i = 0; i < ColumnName.Length; i++)
                    {
                        strCmd += ColumnName[i] + ",";
                     }
                    strCmd = strCmd.Remove(strCmd.Length - 1, 1);
                    strCmd += ") Values (";
                    for (int i = 0; i < ColumnType.Length; i++)
                    {
                        strCmd += "'" + ColumnValues[i].ToString() + "',";
                        strCmd += ColumnValues[i].ToString() + ",";
                        strCmd = strCmd.Remove(strCmd.Length - 1, 1) + ")";
                    }
                    cmd = new OracleCommand(strCmd, conn);
                    if (cmd.ExecuteNonQuery() != 0)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
               else 
                   {
                        throw new Exception("Number of Arguments do not match");
                   }
             }
           catch (Exception exception)
            {
               this.LA.WriteLine(exception.Message);
            }
            finally
            {
              this.conn.Close();
            }
            return flag;
        }
原创粉丝点击