當前位置: 首頁>>代碼示例>>C#>>正文


C# NpgsqlCommand.CreateParameter方法代碼示例

本文整理匯總了C#中Npgsql.NpgsqlCommand.CreateParameter方法的典型用法代碼示例。如果您正苦於以下問題:C# NpgsqlCommand.CreateParameter方法的具體用法?C# NpgsqlCommand.CreateParameter怎麽用?C# NpgsqlCommand.CreateParameter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Npgsql.NpgsqlCommand的用法示例。


在下文中一共展示了NpgsqlCommand.CreateParameter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: SetData

        public int SetData(string procedureName, IList<SQLProcedureParameterVO> ParameterList)
        {
            int succCount = 0;
            Exception ex = null;
            bool querySucc = true;

            //저장프로시저와 커맨드 객체 연결
            NpgsqlCommand nCMD = new NpgsqlCommand(procedureName, this.NpgConn);
            nCMD.CommandType = CommandType.StoredProcedure;
            foreach (SQLProcedureParameterVO SQLProcedureParameterVO in ParameterList)
            {
                var parameter = nCMD.CreateParameter();
                parameter.ParameterName = SQLProcedureParameterVO.parameterName;
                parameter.DbType = SQLProcedureParameterVO.DBType;
                parameter.Value = SQLProcedureParameterVO.value;
                nCMD.Parameters.Add(parameter);
            }

            using (nCMD)
            {
                using (NpgsqlTransaction tran = this.NpgConn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    nCMD.Transaction = tran;
                    try
                    {
                        lock (SetDataThreadSafe)
                        {
                            succCount += nCMD.ExecuteNonQuery();
                        }
                    }
                    catch (Exception e)
                    {
                        querySucc = false;
                        ex = e;
                    }

                    if (querySucc)
                    {
                        tran.Commit();
                        return succCount;
                    }
                    else
                    {
                        if (tran != null)
                        {
                            tran.Rollback();
                        }
                        throw ex;
                    }
                }//End of using(tran)
            }//End of Using(cmd)
        }
開發者ID:hesed7,項目名稱:hitpan,代碼行數:52,代碼來源:PostgresSQLDBServicecs.cs

示例2: TestXmlParameter_Internal

        private void TestXmlParameter_Internal(bool prepare)
        {
            using (var conn = OpenConnection())
            using (var command = new NpgsqlCommand("select @PrecisionXML", conn))
            {
                var sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <strings type=\"array\"> <string> this is a test with ' single quote </string></strings>";
                var parameter = command.CreateParameter();
                parameter.DbType = DbType.Xml;  // To make it work we need to use DbType.String; and then CAST it in the sSQL: cast(@PrecisionXML as xml)
                parameter.ParameterName = "@PrecisionXML";
                parameter.Value = sXML;
                command.Parameters.Add(parameter);

                if (prepare)
                    command.Prepare();
                command.ExecuteScalar();
            }

        }
開發者ID:ArsenShnurkov,項目名稱:npgsql,代碼行數:18,代碼來源:MiscTypeTests.cs

示例3: TestPreparedStatementParameterCastIsNotAdded

        public void TestPreparedStatementParameterCastIsNotAdded()
        {
            // Test by Waldemar Bergstreiser

            new NpgsqlCommand("create table testpreparedstatementparametercast ( C1 int );", TheConnection).ExecuteNonQuery();
            IDbCommand cmd = new NpgsqlCommand("select C1 from testpreparedstatementparametercast where :p0 is null or  C1 = :p0 ", TheConnection);

            IDbDataParameter paramP0 = cmd.CreateParameter();
            paramP0.ParameterName = "p0";
            paramP0.DbType = DbType.Int32;
            cmd.Parameters.Add(paramP0);
            cmd.Prepare();    // This cause a runtime exception // Tested with PostgreSQL 8.3 //
        }
開發者ID:zapov,項目名稱:Npgsql2,代碼行數:13,代碼來源:CommandTests.cs

示例4: SetParameterValueNull

        public void SetParameterValueNull()
        {
            NpgsqlCommand cmd = new NpgsqlCommand("insert into tablef(field_bytea) values (:val)", TheConnection);
                  NpgsqlParameter param = cmd.CreateParameter();
                  param.ParameterName="val";
            param.NpgsqlDbType = NpgsqlDbType.Bytea;
                  param.Value = DBNull.Value;

                  cmd.Parameters.Add(param);

                  cmd.ExecuteNonQuery();

                  cmd = new NpgsqlCommand("select field_bytea from tablef where field_serial = (select max(field_serial) from tablef)", TheConnection);

                  Object result = cmd.ExecuteScalar();

            Assert.AreEqual(DBNull.Value, result);
        }
開發者ID:zapov,項目名稱:Npgsql2,代碼行數:18,代碼來源:CommandTests.cs

示例5: ParameterExplicitType2DbTypeObjectWithPrepare2

        public void ParameterExplicitType2DbTypeObjectWithPrepare2()
        {
            new NpgsqlCommand("create temp table test ( tc date )", TheConnection).ExecuteNonQuery();

            const string query = @"select * from test where tc=:param or tc=:param2";

            NpgsqlCommand command = new NpgsqlCommand(query, TheConnection);

            IDbDataParameter sqlParam = command.CreateParameter();
            sqlParam.ParameterName = "param";
            sqlParam.Value = "2008-1-1";
            sqlParam.DbType = DbType.Object;
            command.Parameters.Add(sqlParam);

            sqlParam = command.CreateParameter();
            sqlParam.ParameterName = "param2";
            sqlParam.Value = DateTime.Now;
            sqlParam.DbType = DbType.Date;
            command.Parameters.Add(sqlParam);

            command.Prepare();

            command.ExecuteScalar();
        }
開發者ID:zapov,項目名稱:Npgsql2,代碼行數:24,代碼來源:CommandTests.cs

示例6: ParameterExplicitType2DbTypeObjectWithPrepare2

        public void ParameterExplicitType2DbTypeObjectWithPrepare2()
        {
            using (var command = new NpgsqlCommand(@"SELECT * FROM data WHERE field_date=:param or field_date=:param2", Conn))
            {
                var sqlParam = command.CreateParameter();
                sqlParam.ParameterName = "param";
                sqlParam.Value = "2008-1-1";
                sqlParam.DbType = DbType.Object;
                command.Parameters.Add(sqlParam);

                sqlParam = command.CreateParameter();
                sqlParam.ParameterName = "param2";
                sqlParam.Value = DateTime.Now;
                sqlParam.DbType = DbType.Date;
                command.Parameters.Add(sqlParam);

                command.Prepare();
                command.ExecuteScalar();
            }
        }
開發者ID:baondp,項目名稱:Npgsql,代碼行數:20,代碼來源:CommandTests.cs

示例7: TestPreparedStatementParameterCastIsNotAdded

        public void TestPreparedStatementParameterCastIsNotAdded()
        {
            // Test by Waldemar Bergstreiser

            var cmd = new NpgsqlCommand("select field_int4 from data where :p0 is null or field_int4 = :p0 ", Conn);

            var paramP0 = cmd.CreateParameter();
            paramP0.ParameterName = "p0";
            paramP0.DbType = DbType.Int32;
            cmd.Parameters.Add(paramP0);
            cmd.Prepare(); // This cause a runtime exception // Tested with PostgreSQL 8.3 //
        }
開發者ID:baondp,項目名稱:Npgsql,代碼行數:12,代碼來源:CommandTests.cs

示例8: addGrade

        public void addGrade(int index, SolutionDatabase s)
        {
            conn.Open();
            NpgsqlCommand commandsubmission = new NpgsqlCommand("Select s.submissionid from submissions s order by s.submissionid desc limit 1 ",conn);
            int submissionId = (int)commandsubmission.ExecuteScalar();
            conn.Close();

            conn.Open();
            NpgsqlCommand command = new NpgsqlCommand("addGrade", conn);
            command.CommandType = CommandType.StoredProcedure;

            var parameter = command.CreateParameter();
            parameter.ParameterName = "Graded_Against_SolutionId";
            parameter.DbType = System.Data.DbType.Int32;
            parameter.Value = s.solId;
            command.Parameters.Add(parameter);

            var parameter2 = command.CreateParameter();
            parameter2.ParameterName = "SubmissionId";
            parameter2.DbType = System.Data.DbType.Int32;
            parameter2.Value = submissionId;
            command.Parameters.Add(parameter2);

            var parameter3 = command.CreateParameter();
            parameter3.ParameterName = "Grade";
            parameter3.DbType = System.Data.DbType.Int32;
            parameter3.Value = s.grade;
            command.Parameters.Add(parameter3);

            var parameter4 = command.CreateParameter();
            parameter4.ParameterName = "GradeComments";
            parameter4.DbType = System.Data.DbType.AnsiString;
            parameter4.Value = string.Empty;
            command.Parameters.Add(parameter4);
            command.ExecuteNonQuery();
            conn.Close();
        }
開發者ID:TripleSummoningTrapStart,項目名稱:562project,代碼行數:37,代碼來源:Database.cs

示例9: ParameterExplicitType2DbTypeObjectWithPrepare

        public void ParameterExplicitType2DbTypeObjectWithPrepare()
        {
            new NpgsqlCommand("create temp table test ( tc date )", TheConnection).ExecuteNonQuery();
            var command = new NpgsqlCommand(@"select * from test where tc=:param", TheConnection);

            var sqlParam = command.CreateParameter();
            sqlParam.ParameterName = "param";
            sqlParam.Value = "2008-1-1";
            sqlParam.DbType = DbType.Object;
            command.Parameters.Add(sqlParam);
            command.Prepare();
            command.ExecuteScalar();
        }
開發者ID:jvaldezb,項目名稱:Npgsql2,代碼行數:13,代碼來源:CommandTests.cs

示例10: addSubmission

        public int addSubmission(string json,int assnId, int studentId)
        {
            conn.Open();
            NpgsqlCommand command = new NpgsqlCommand("addSubmission", conn);
            command.CommandType = CommandType.StoredProcedure;

            var parameter = command.CreateParameter();
            parameter.ParameterName = "AssignmentId";
            parameter.DbType = System.Data.DbType.Int32;
            parameter.Value = assnId;
            command.Parameters.Add(parameter);

            var parameter2 = command.CreateParameter();
            parameter2.ParameterName = "StudentId";
            parameter2.DbType = System.Data.DbType.Int32;
            parameter2.Value = studentId;
            command.Parameters.Add(parameter2);

            var parameter3 = command.CreateParameter();
            parameter3.ParameterName = "JSonString";
            parameter3.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Varchar;
            parameter3.Value = json;
            command.Parameters.Add(parameter3);
            command.ExecuteNonQuery();
            conn.Close();
            return Grade(json, assnId);
        }
開發者ID:TripleSummoningTrapStart,項目名稱:562project,代碼行數:27,代碼來源:Database.cs

示例11: getSolutions

        public List<SolutionDatabase> getSolutions(int assignmentID)
        {
            conn.Open();
            List<SolutionDatabase> solutions = new List<SolutionDatabase>();
            NpgsqlCommand command = new NpgsqlCommand("getSolution", conn);
            command.CommandType = CommandType.StoredProcedure;

            var parameter = command.CreateParameter();
            parameter.ParameterName = "AssnId";
            parameter.DbType = System.Data.DbType.Int32;
            parameter.Value = assignmentID;
            command.Parameters.Add(parameter);

            NpgsqlDataReader dr = command.ExecuteReader();

            while (dr.Read())
            {
                string check = dr[2].ToString();
                Database d = JsonConvert.DeserializeObject<Database>(dr[2].ToString());
                solutions.Add(new SolutionDatabase(d, (int)dr[1], (int)dr[0]));
            }

            conn.Close();
            return solutions;
        }
開發者ID:TripleSummoningTrapStart,項目名稱:562project,代碼行數:25,代碼來源:Database.cs

示例12: addSolutions

        public void addSolutions(string json, int assnId, int percent)
        {
            conn.Open();
            NpgsqlCommand command = new NpgsqlCommand("addSolution", conn);
            command.CommandType = CommandType.StoredProcedure;

            var parameter = command.CreateParameter();
            parameter.ParameterName = "percentage";
            parameter.DbType = System.Data.DbType.Int32;
            parameter.Value = percent;
            command.Parameters.Add(parameter);

            var parameter2 = command.CreateParameter();
            parameter2.ParameterName = "jsonString";
            parameter2.DbType = System.Data.DbType.AnsiString;
            parameter2.Value = json;
            command.Parameters.Add(parameter2);

            var parameter3 = command.CreateParameter();
            parameter3.ParameterName = "AssnId";
            parameter3.DbType = System.Data.DbType.Int32;
            parameter3.Value = assnId;
            command.Parameters.Add(parameter3);
            command.ExecuteNonQuery();
            conn.Close();
        }
開發者ID:TripleSummoningTrapStart,項目名稱:562project,代碼行數:26,代碼來源:Database.cs

示例13: TestTsQueryIsContainedIn

        public void TestTsQueryIsContainedIn()
        {
            var query = @"select 'cat'::tsquery <@ :param::tsquery";

            using (var command = new NpgsqlCommand(query, Conn))
            {
                var sqlParam = command.CreateParameter();
                sqlParam.ParameterName = "param";
                sqlParam.Value = "cat & rat";
                //sqlParam.DbType = DbType.Object;
                command.Parameters.Add(sqlParam);
                var dr = command.ExecuteReader();
                int i = 0;
                var result = false;
                while (dr.Read())
                {
                    result = Convert.ToBoolean(dr[0]);
                    i++;
                }

                Assert.AreEqual(1, i);
                Assert.IsTrue(result);
            }

        }
開發者ID:jjchiw,項目名稱:Npgsql,代碼行數:25,代碼來源:CommandTests.cs

示例14: TestFullTextSearchOr

 public void TestFullTextSearchOr()
 {
 	BuildFullTextSearchTableTest();
 	
      var query = @"select * 
                       from posts 
                       where search_vector @@ to_tsquery('english', :param)
                       order by ts_rank_cd(search_vector, to_tsquery('english', :param)) desc";
     
     using (var command = new NpgsqlCommand(query, Conn))
     {
         var sqlParam = command.CreateParameter();
         sqlParam.ParameterName = "param";
         sqlParam.Value = "postgres | mysql";
         //sqlParam.DbType = DbType.Object;
         command.Parameters.Add(sqlParam);
         var dr = command.ExecuteReader();
         int i = 0;
         while (dr.Read())
             i++;
         Assert.AreEqual(5, i);
     }
 }
開發者ID:jjchiw,項目名稱:Npgsql,代碼行數:23,代碼來源:CommandTests.cs

示例15: SetParameterValueNull

        public void SetParameterValueNull()
        {
            var cmd = new NpgsqlCommand("insert into data(field_bytea) values (:val)", Conn);
            var param = cmd.CreateParameter();
            param.ParameterName = "val";
            param.NpgsqlDbType = NpgsqlDbType.Bytea;
            param.Value = DBNull.Value;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();

            cmd = new NpgsqlCommand("select field_bytea from data where field_serial = (select max(field_serial) from data)", Conn);
            var result = cmd.ExecuteScalar();
            Assert.AreEqual(DBNull.Value, result);
        }
開發者ID:baondp,項目名稱:Npgsql,代碼行數:14,代碼來源:CommandTests.cs


注:本文中的Npgsql.NpgsqlCommand.CreateParameter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。