如何使用NpgsqlParameter列表

时间:2017-04-30 09:29:31

标签: c# postgresql npgsql

我是postgresql的新手,现在我们必须在C#中更改我们的代码,以便从sql使用db作为postgresql,以下代码可以正常使用sql

        public PL.Type GetDetails(int ID)
        {
            PL.Type obj = null;

            SqlParameter[] parameters = new SqlParameter[]
            {
              new SqlParameter("@Action", "GetDetails"),
              new SqlParameter("@ID", ID)
            };
            //Lets get the list of all Type in a datataable
            using (DataTable table = DL.SqlDbHelper.ExecuteParamerizedSelectCommand("sp_Type", CommandType.StoredProcedure, parameters))
            {
                //check if any record exist or not
                if (table.Rows.Count == 1)
                {
                    DataRow row = table.Rows[0];

                    //Lets go ahead and create the list of Type
                    obj = new PL.Type();

                    //Now lets populate the Type details into the list of Types
                    obj.ID = Convert.ToInt32(row["ID"]);
                    obj.Name = row["Name"].ToString();
                }
            }

            return obj;
        }

但是当我们尝试使用postgresql db时它不起作用,更改后的postgresql代码就是

        public PL.Type GetDetails(int ID)
        {
        PL.Type obj = null;

        NpgsqlParameter[] parameters = new NpgsqlParameter[]
        {
            new NpgsqlParameter("@Action", "GetDetails"),
            new NpgsqlParameter("@ID", ID)
        };
        //Lets get the list of all Type in a datataable
        using (DataTable table = DL.PostgreSQLDbHelper.ExecuteParamerizedSelectCommand("sp_Type", CommandType.StoredProcedure, parameters))
        {
            //check if any record exist or not
            if (table.Rows.Count == 1)
            {
                DataRow row = table.Rows[0];

                //Lets go ahead and create the list of Type
                obj = new PL.Type();

                //Now lets populate the Type details into the list of Types
                obj.ID = Convert.ToInt32(row["ID"]);
                obj.Name = row["Name"].ToString();
            }
        }

        return obj;
    }

0 个答案:

没有答案