首页 Npgsql 用户手册 简体中文版

Npgsql 用户手册 简体中文版

举报
开通vip

Npgsql 用户手册 简体中文版 Npgsql: 用户使用手册 版权所有© Npgsql 开发小组 最后更新时间: 2009/12/20 02:33:47 作者: fxjr 翻译:badgereden@gmail.com(刘保恩) 于 2012年 11月 5日 类别: 扩展文档 预期读者: Npgsql 用户 1. Npgsql 是什么 ? Npgsql 是一种 PostgreSQL 的.Net 数据提供者.(即.Net语言下的访问 PostgreSQL的数据中间层) 它允许一个.Net 客户端应用程序(如 Console,WinForms...

Npgsql 用户手册 简体中文版
Npgsql: 用户使用手册 版权所有© Npgsql 开发小组 最后更新时间: 2009/12/20 02:33:47 作者: fxjr 翻译 阿房宫赋翻译下载德汉翻译pdf阿房宫赋翻译下载阿房宫赋翻译下载翻译理论.doc :badgereden@gmail.com(刘保恩) 于 2012年 11月 5日 类别: 扩展文档 预期读者: Npgsql 用户 1. Npgsql 是什么 ? Npgsql 是一种 PostgreSQL 的.Net 数据提供者.(即.Net语言下的访问 PostgreSQL的数据中间层) 它允许一个.Net 客户端应用程序(如 Console,WinForms,ASP.NET,Web Services等)直接访问 PostgreSQL。它是在.Net 文档的严格规范 下积极开发的。 2. 如何获得和编译 Npgsql? 2.1 二进制包 你可以直接从该项目的文件列表里直接下载专门为MS.Net和Mono的已编译版本。 该包里的文件列表如下: Npgsql/bin/docs – 文档 Npgsql/bin/docs/apidocs - API 文档 Npgsql/bin/ms1.1 - MS.Net 1.1 编译版本 Npgsql/bin/mono - Mono 编译版本 Npgsql发布了其它平台或者新版本后,会尽快的添加到此列表。 2.2 安装二进制包 为了让.Net 运行时可以查找到 Npgsql.dll文件,该文件必须位于你的应用程序的目录下——除非你通过配置文件(使用 probing元素)另外指 定一个目录为你的私有组件的路径。相关内容可以查阅.Net文档的关于运行时探查装配件如何被加载的内容,特别是 “私有组件的路径”一节。 对于 ASP.NET 和Web Services .NET 应用程序,在 ASP.NET应用程序的根目录下一定有一个名为 bin的目录,比如:如果应用程序的目录名 为 ASPNETApplication,那么 Npgsql.dll和 Mono.Security.dll必须位于 ASPNETApplication\bin目录下。如果他们出现在正确的目录里,在 编译使用过 Npgsql的代码时会产生预期的错误提示。 另外,你可以把 Npgsql程序集放在全局程序集缓存(GAC)下,从 0.4版开始,Npgsql使用强签名,你可以通过使用”gacuil”命令来安装它,像下 面这样使用这个简单的命令即可完成安装。 gacutil -i Npgsql.dll 相关内容请参考 MSDN文档的“在 GAC(全局程序集缓存)下安装程序集”一节。在你这样做之前,最好能完全理解 GAC(全局程序集缓存)的正 确使用。 记住:要想使 Visual Studio .Net在 设计 领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计 时支持 Npgsql,必须把 Npgsql放在 GAC中。 (Npgsql的 Mono编译版本不需要 Mono.Security.dll文件,因为 Mono运行时已经集成了.) 一旦你复制并设置好程序集,你就可以尝试一下这些示例——跳到第 3节。 2.3 通过 CVS获得 Npgsql To get Npgsql from CVS, use the following info in your CVS client: 在你的 CVS客户端中使用信息,即可通过 CVS获得 Npgsql: Server: cvs.pgfoundry.org Repository: /cvsroot/npgsql Module name: Npgsql2 User: anonymous Password: 如果通过命令行使用 CVS,那么使用下面的命令: cvs -d :pserver:anonymous@cvs.pgfoundry.org:/cvsroot/npgsql login 按下 Enter键时,会提示你输入密码(不是必需的): cvs -d :pserver:anonymous@cvs.pgfoundry.org:/cvsroot/npgsql checkout Npgsql2 接下来开始传送代码了: $ cvs -d :pserver:anonymous@cvs.pgfoundry.org:/cvsroot/npgsql login Logging in to :pserver:anonymous@cvs.pgfoundry.org:2401/cvsroot/npgsql CVS password: $ cvs -d :pserver:anonymous@cvs.pgfoundry.org:/cvsroot/npgsql co Npgsql2 cvs checkout: Updating Npgsql2 cvs checkout: Updating Npgsql2/admin U Npgsql2/admin/release.pl cvs checkout: Updating Npgsql/docs U Npgsql2/docs/Npgsql.zargo U Npgsql2/docs/NpgsqlConnectionStateMachine.png U Npgsql2/docs/SuggestedReadings.htm ... 2.4 编译 Npgsql 编译 Npgsql的官方支持的做法是用 NAnt,且版本为 0.86或更高版本,之所以需要 0.86版式因为它可以支持编译 sattelite(这个单词估计是拼 写错误,或者是 Npgsql下特有一个模块)资源程序集。你只需要从 Npgsql/src/Npgsql文件夹下简单的运行 nant 命令,就是可以在 build/ms 文件夹下找到编译好的程序集。当然,话说回来,你至少得有 MonoDevelop、Visual studio .Net或者 Visual studio .Net 2008; 2.5 运行 Npgsql单元测试 为了能运行 nunit 测试,我们需要配置下测试数据库,首先,创建一个名为 npgsql_test的数据库: createdb npgsql_tests 然后,创建一个名为 npgsql_tests 的用户,密码设置为 npgsql_tests: createuser -NP npgsql_tests 现在,运行测试脚本以添加数据表,函数,数据等。这些脚本文件都位于 testsuite/noninteractive文件夹下,通过简单的输入以下命令来运行 nUnit测试。 nant tests 3. NpgsqlNpgsqlNpgsqlNpgsql 的用法 本节,我们将在.Net应用程序(windows或者ASP.NET)中解释Npgsql的用法。如果你有使用Sql Server、OleDB或者ODBC.NET providers 开发过数据访问类应用程序的话,你将发现,Npgsql跟它们很相似。大多数情况下都是一样或者更强大,还有一个活跃的组织在提供支持。 为了能使用 Npgsql,PostgreSQL服务器必须监听 TCP/IP的连接。PostgreSQL默认允许使用 TCP连接。之前的版本需要在启动时添加“-i”选 项。更多细节请见 PostgreSQL文档:http://www.postgresql.org/docs/7.4/static/postmaster-start.html 注意:Npgsql仍在不断发展中,这里只是演示目前已支持的特性。随着 Npgsql的日渐成熟,将会有更多的功能。 给你的代码文件增加一个必需的命名空间 首先,为了能更简单的访问 Npgsql对象(即使用 Visual Studio .Net的智能感知),需要让编译器能使用 Npgsql命名空间,为了能操作 Npgsql 返回的数据,我们还需要 System.Data下的类,在 C#中,将以下指令增加到适当的页面或者类中。 using System.Data; using Npgsql; 如果你使用没有代码隐藏的 ASP.NET文件,可以在 ASPX页面的头部增加以下这几行: <%@ Assembly name="System.Data" %> <%@ Assembly name="Npgsql" %> 你可以从这里获得更多的细节 FAQ Mono Page about ASP.NET 建立连接 使用名为“joe”密码为“secret”的用户,建立一个到 IP地址为 127.0.0.1,端口为 5432的服务器上的“joedata”的数据库的连接,可以使 用以下的连接字符串来打开 NpgsqlConnection。 using System; using System.Data; using Npgsql; public class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); conn.Close(); } } 连接字符串里的参数 建立连接的时候,NpgsqlConnection可以接受一些用以改变其行为的参数,下面就是现在你可以使用的参数列表:(源自 NpgsqlConnection 的源代码) 连接 PostgreSQL数据库时,可以获取或者设置这些字符串。合法的值有: Server PostgreSQL 服务器的 IP地址或名字 Port 用以连接的端口 Protocol 使用的协议版本,可以用 Integer类型的 2或者 3代替 automatic Database 数据库名字,如果没有指定,默认为与用户名相同 User Id 用户名 Integrated Security 设置为Windows集成安全性,默认为 false Password 纯文本的身份验证密码 SSL True或者 False,控制是否使用一个安全的连接(SSL连接),默认为 false Pooling True或者 False,控制是否使用连接池,默认为 True MinPoolSize 连接池的最小值,指定了连接池的最小值,可以让 NpgsqlConnection预先分配指定数量的连接。默认值为 1. MaxPoolSize 连接池的最大值,如果连接池内的连接的数量大于这个值,那么堵塞的连接将被处置掉。默认值为:20 Encoding 已废弃。总是返回“Unicode”字符串,即使设置它也会被忽略。 Timeout 等待连接的事件,单位为秒。默认值为:15 CommandTimeout 等待命令完成执行的时间,超时将抛出异常。单位为秒。默认值为:20 Sslmode 连接控制的方式,可以是以下值中的一个: Prefer 如果有可能使用 SSL连接,那么就采用 SSL连接。(首选) Require 如果不能建立 SSL连接,将抛出异常。(必须) Allow 如果还不支持,那么就使用非 SSL连接。(允许) Disable 使用非 SSL连接(不允许) 默认值是“Disable” ConnectionLifeTime 连接池中不再使用的连接关闭的等待时间。单位:秒,默认值为:15 SyncNotification 指定的话 Npgsql将使用同步消息机制 SearchPath 改变 public的和特定的模式的搜索路径 Preload Reader 如果设置为为“true”(默认值为“false”)的话,数据读取器将在 ExecuteReader全部返回前被加载。这将导致性能损失(特别是在大量的记 录集时,可能导致不可容忍的性能损失),它保留下来是为了和之前版本的Npgsql保持兼容。根据 ADO.NET文档,当 IDataReader打开用于 获取数据的 IDbConnection时,它的状态被设置为“busy”,不能用于其他任何操作(有一些明确的异常(Exception 类)专门针对此规则)。 Npgsql 强制执行这个规则,所以当 NpgsqlDataReader 打开后,其他的基于这个用于获取数据的 IDbConnection 上的其它操作都将导致 InvalidOperationException(Npgsql放松了这个规则,如果 NpgsqlDataReader已经读到结果集的结尾但是还没有被关闭,Npgsql允许你使 用这个连接。因为这时,它不再从连接上使用什么资源。) 但是在以前,Npgsql允许用户完全忽略这个规则。这完全是一个内置的执行问题的副作用,严格来说,它从来没有被支持过(因为它总是违背 ADO.NET的规范),而且这一点偷懒可能会让你发现以前运行良好的代码突然挂掉了。如果你确定无论如何都要使用它,可以将它作为修复代码 前的权益之计,基于下两个原因: 性能,尤其是可伸缩性,不适用这个选项伸缩性将更好. Use Extended Types 这个选项可决定 DataAdaptors 到底是使用.NET的 System.DataTime 类型还是使用类似 NpgsqlTimeStamp 这样比 System.DataTime类 型具有更多功能和使用范围的Npgsql的日期和事件类型。选项设置为“Either”允许使用 Npgsql和 System类型,但是如果设置为“true”, DataAdaptors将考虑为字段使用使用特定的 Npgsql类型,反之,如果设置为“false”将使用 System.DataTime. 这个选项是实验性的,有可能因为冲突在新版本中简化或删掉 默认值为“false” Compatibility 这个版本的目的是为了应付不断增加新的连接字符串选项而带来的破坏性的变更,它的值可以设置为版本号的形式(像这样 a.b[.c[.c]]).变化, 可能会破坏现有的代码,所以,在可能的情况下,尽量复制以前版本的行为。第一个这么做的版本是 2.0.2.1,所以在 2.0.2中,这个值不会有新 的行为。 版本 行为 2.0.2 如果指定字段名找不到,则 GetOrdinal 返回-1 GetOrdinal 区分假名的宽度 版本 行为 2.0.2.1 如果指定字段名找不到,则 GetOrdinal 将抛出 IndexOutOfRangeException 异常 GetOrdinal 不区分假名宽度 使用 NpgsqlCommand向表格增加一个行 前面的示例其实一点有用的事情也没有做,仅仅是连接数据库,然后断开。如果有错误的话,将抛出一个 NpgsqlException异常。现在假设你有 一个名为“table1”的表格,该表格拥有“fielda”和“fieldb”两个字段,类型都是 int.如果你想向该表格中插入(1,1)元组,可以使用以下插入 语句。 using System; using System.Data; using Npgsql; public static class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); NpgsqlCommand command = new NpgsqlCommand("insert into table1 values(1, 1)", conn); Int32 rowsaffected; try { rowsaffected = command.ExecuteNonQuery(); Console.WriteLine("It was added {0} lines in table table1", rowsaffected); } finally { conn.Close(); } } } ExecuteNonQuery()是插入和更新查询的理想方法,因为它返回一个指示受最后一次操作影响的行数的整数。 使用 NpgsqlCommand.ExecuteScalar()方法获取单一的返回值 有些情况下,你只想从函数取回会一个单独的值(scalar).这是你可以使用 Command对象的 ExecuteScalar()方法。 using System; using System.Data; using Npgsql; public static class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); NpgsqlCommand command = new NpgsqlCommand("select version()", conn); String serverversion; try { serverversion = (String)command.ExecuteScalar(); Console.WriteLine("PostgreSQL server version: {0}", serverversion); } finally { conn.Close(); } } } 你也可以使用 ExecuteScalar执行返回记录集的查询,比如“select count(*) from table1”。但是,当调用返回一条或更多记录的函数时, 只 有 第 一 行 的 第 一 列 能 返 回 ( 即 DataSet.Tables[0].Rows[0][0]) 。 一 般 来 说 , 任 何 一 个 返 回 单 一 值 的 查 询 都 应 该 使 用 Command.ExecuteScalar。 使用 NpgsqlCommand.ExecuteReader()方法和 NpgsqlDataReader获得完整的结果集 有很多方式可以获得 Npgsql 返回的记录集。当你想通过一个 SQL 语句作为命令的文本和使用高效内存数据读取器访问结果的时候,请使用 NpgsqlCommand对象的 ExecuteReader()方法。 using System; using System.Data; using Npgsql; public static class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn); try { NpgsqlDataReader dr = command.ExecuteReader(); while(dr.Read()) { for (i = 0; i < dr.FieldCount; i++) { Console.Write("{0} \t", dr[i]); } Console.WriteLine(); } } finally { conn.Close(); } } } 记住,你可以在一个 Command对象的 CommandText属性中使用一连串的 select语句来检索多余一个的记录集,就像这样:“select * from tablea;select * from tableb” 在查询中使用参数 参数可以让你在运行时动态的向 SQL查询中插入多个值,一般而言,参数绑定是你在客户端代码中产生动态 SQL语句的最好方法。而其他的方法, 如基本的串联字符串的方法,是不太可靠的,它可能会招致注入攻击。通过在参数名称前附加“:”前缀的方式向你的 SQL查询字符串中增加参 数。下面的示例使用一个名为“value1”的参数(就像这样“:value1”) using System; using System.Data; using Npgsql; public static class NpgsqlUserManual { public static void Main(String[] args) { using(NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;")) { conn.Open(); // 声明查询语句中使用的参数 using(NpgsqlCommand command = new NpgsqlCommand("select * from tablea where column1 = :value1", conn)) { // Now add the parameter to the parameter collection of the command specifying its type. //现在按照参数的类型将参数添加到参数集合中。 command.Parameters.Add(new NpgsqlParameter("value1", NpgsqlDbType.Integer)); // Now, add a value to it and later execute the command as usual. //现在,设置参数的值,并在稍后执行查询。 command.Parameters[0].Value = 4; using(NpgsqlDataReader dr = command.ExecuteReader()) { while(dr.Read()) { for (i = 0; i < dr.FieldCount; i++) { Console.Write("{0} \t", dr[i]); } Console.WriteLine(); } } } } } } 你也可以使用 NpgsqlParameter和 NpgsqlParameterCollection对象向服务器发送一个参数化的查询。 这段代码假设已存在一个名为“tablea”的表,且它至少有一个名为“column1”类型为 int4的字段。 使用预备语句 预备语句可以优化你经常使用的查询的性能,Prepare()基本上就是“缓存”的查询计划,这样就可以在以后的调用使用它。(记住:这个特性只 能在 7.3以后的版本中使用,如果你在一个不支持它的服务器中使用它,Npgsql将悄悄的忽略它)在执行查询之前简单的调用NpgsqlCommand 的 Prepare()方法就可以了。 using System; using System.Data; using Npgsql; public static class NpgsqlUserManual { public static void Main(String[] args) { using(NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;")) { conn.Open(); // 在查询语句中声明参数 using(NpgsqlCommand command = new NpgsqlCommand("select * from tablea where column1 = :column1", conn)) { // 现在,把参数添加到命令的参数集合中,并指定其类型 command.Parameters.Add(new NpgsqlParameter("column1", NpgsqlDbType.Integer); // 现在,预备这个命令 command.Prepare(); //现在,给参数添加一个值,并在稍后像普通方式一样执行这个命令 command.Parameters[0].Value = 4; using(NpgsqlDataReader dr = command.ExecuteReader()) { while(dr.Read()) { for (i = 0; i < dr.FieldCount; i++) { Console.Write("{0} \t", dr[i]); } Console.WriteLine(); } } } } } } 这段代码假定已存在一个名为“tablea”的表,且它至少有个一个名为“column1”类型为 int4的字段。 函数调用 要调用函数,需要将 NpgsqlCommand对象的 CommandType属性设置为 CommandType.StoredProcedure,并将你要调用的函数名作为 查询字符串(在 CommandText属性设置查询字符串) 译者注:因为 Npgsql 中标识符是区分大小写的,所以,如果你的函数名中有大小写,需要将函数名用双引号括起来,比如: command.CommandText=”\”getTableName\””,这样实际上发送给服务器的函数名字外就有双引号了。 using System; using System.Data; using Npgsql; //这个示例使用一个名为”funcC()”的函数,它的定义如下: // create function funcC() returns int8 as ' // select count(*) from tablea; // ' language 'sql'; // Note that the return type of select count(*) changed from int4 to int8 in 7.3+ versions. To use this function // in a 7.2 server, change the return type from int8 to int4. //注意:在 7.3以前的版本中,select count(*) 将 int4类型转换为 int8类型,如果要在 7.2版本中使用,需要将 int8转化为 int4类型. public static class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); try { NpgsqlCommand command = new NpgsqlCommand("funcC", conn); command.CommandType = CommandType.StoredProcedure; Object result = command.ExecuteScalar(); Console.WriteLine(result); } finally { conn.Close(); } } } 向 PostgreSQL增加参数很像我们前面的示例。不过,当我们指定 CommandText字符串的时候,可以省略参数的名字,只使用函数名字就可以 了。 using System; using System.Data; using Npgsql; // This example uses a function called funcC with the following definition: // create function funcC(int4) returns int8 as ' // select count(*) from tablea where field_int4 = $1; // ' language 'sql'; public static class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); try { NpgsqlCommand command = new NpgsqlCommand("funcC", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new NpgsqlParameter()); command.Parameters[0].NpgsqlDbType = NpgsqlDbType.Integer; command.Parameters[0].Value = 4; Object result = command.ExecuteScalar(); Console.WriteLine(result); } finally { conn.Close(); } } } 这段代码假定已存在一个名为“tablea”的表,且它至少有个一个名为“column1”类型为 int4的字段。 在 DataSet对象中获得全部结果:使用 refcursor Refcursor对象是 PostgreSQL构建向客户端返回大量结果集的函数的强有力的方法之一。通过使用多个 refcursor,单一的函数在一个往返行程 中向客户端返回多个查询的结果。大多数 Npgsql开发者将发现,一旦你掌握了它的基本语法,refcursor使用起来很简单。这个例子使用 refcursor 从函数返回两个结果集。有 Npgsql稳健的 refcorsor支持,你尽可以放心的获取很多结果集,而不用担心 refcursor在 PostgreSQL内部的运行 机制。 假定有以下这个基于 refcursor的函数: CREATE OR REPLACE FUNCTION testrefcursor(int4) RETURNS SETOF refcursor AS 'DECLARE ref1 refcursor; ref2 refcursor; ref3 refcursor; BEGIN OPEN ref1 FOR SELECT * FROM table1; RETURN NEXT ref1; OPEN ref2 FOR SELECT * FROM table2; RETURN next ref2; OPEN ref3 FOR EXECUTE 'SELECT * FROM table3 WHERE keyfield = ' || $1; RETURN next ref3; RETURN; END;' LANGUAGE plpgsql; 这个函数返回三个 select语句的全部结果。记住:最后一个 select语句是在服务器上动态创建的。现在,你可以使用下列代码调用函数并通过 DataReader取回数据。 using System; using System.Data; using Npgsql; using NpgsqlTypes; public class c { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Initial Catalog=eeeeee;User id=npgsql_tests;password=npgsql_tests;"); conn.Open(); NpgsqlTransaction t = conn.BeginTransaction(); NpgsqlCommand command = new NpgsqlCommand("testrefcursor", conn); command.CommandType = CommandType.StoredProcedure; NpgsqlDataReader dr = command.ExecuteReader(); while(dr.Read()) { Console.WriteLine(dr.GetValue(0)); } dr.NextResult(); while(dr.Read()) { Console.WriteLine(dr.GetValue(0)); } dr.Close(); t.Commit(); conn.Close(); } } 当然,你也可以将查询结果放入一个 DataSet对象。 using System; using System.Data; using Npgsql; using NpgsqlTypes; public class c { public static void Main(String[] args) { DataSet myDS; NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Initial Catalog=eeeeee;User id=npgsql_tests;password=npgsql_tests;"); conn.Open(); NpgsqlTransaction t = conn.BeginTransaction(); NpgsqlCommand command = new NpgsqlCommand("testrefcursor", conn); command.CommandType = CommandType.StoredProcedure; con.Open(); NpgsqlDataAdapter da = new NpgsqlDataAdapter(command); da.Fill(myDS); t.Commit(); conn.Close(); } } 就这样了。最后,还有一点要注意:为了防止从 refcursor函数返回的游标被隐式的事务在完成后关闭(在你调用函数之后),你必须显式使用一 个事务来完成以上操作。如果在函数中使用了参数,只需要将函数的名字指定给 CommandText 属性,并照例将参数添加到 NpgsqlCommand.Parameters集合。Npgsql将负责正确的绑定这些参数。 在查询中使用输出参数 Npgsql中可以使用输出参数。不过要注意,Npgsql其实是通过解析执行查询第一个结果并将它转换为输出参数的值来模拟输出参数的。这可以 通过两种方式来完成:使用映射,或者不用。一个映射分析器尝试去用参数的名字去匹配返回的结果集中的相同的字段名字。如果匹配不成功,那 么只有匹配成功的输出参数的值才会被更新。如果一个映射没有找到,那么输出参数会根据他们被添加到命令参数集的顺序来更新它的值。映射是 自动完成的,解析结果集的时候,Npgsql会尝试查找匹配,Output和 InputOutput类型的参数都支持。 using System; using System.Data; using Npgsql; public static class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); // Send a query to backend. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where column1 = 2", conn); // Now declare an output parameter to receive the first column of the tablea. NpgsqlParameter firstColumn = new NpgsqlParameter("firstcolumn", NpgsqlDbType.Integer); firstColumn.Direction = ParameterDirection.Output; command.Parameters.Add(firstColumn); try { command.ExecuteNonQuery(); // Now, the firstcolumn parameter will have the value of the first column of the resultset. Console.WriteLine(firstColumn.Value); } finally { conn.Close(); } } } 使用.Net的 Dataset对象 Npgsql允许你将对.Net DataSet对象的更改存回数据库。下面这个示例演示了向 DataSet中插入一条记录,随后,通过调用更新相关的数据库。 // 假定你有以下表: // // create table tableb(field_int2 int2, field_timestamp timestamp, field_numeric numeric); // // void AddWithDataSet(NpgsqlConnection conn) { conn.Open(); DataSet ds = new DataSet(); NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", conn); da.InsertCommand = new NpgsqlCommand("insert into tableb(field_int2, field_timestamp, field_numeric) " + " values (:a, :b, :c)", conn); da.InsertCommand.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Smallint)); da.InsertCommand.Parameters.Add(new NpgsqlParameter("b", NpgsqlDbType.Timestamp)); da.InsertCommand.Parameters.Add(new NpgsqlParameter("c", NpgsqlDbType.Numeric)); da.InsertCommand.Parameters[0].Direction = ParameterDirection.Input; da.InsertCommand.Parameters[1].Direction = ParameterDirection.Input; da.InsertCommand.Parameters[2].Direction = ParameterDirection.Input; da.InsertCommand.Parameters[0].SourceColumn = "field_int2"; da.InsertCommand.Parameters[1].SourceColumn = "field_timestamp"; da.InsertCommand.Parameters[2].SourceColumn = "field_numeric"; da.Fill(ds); DataTable dt = ds.Tables[0]; DataRow dr = dt.NewRow(); dr["field_int2"] = 4; dr["field_timestamp"] = new DateTime(2003, 03, 03, 14, 0, 0); dr["field_numeric"] = 7.3M; dt.Rows.Add(dr); DataSet ds2 = ds.GetChanges(); da.Update(ds2); ds.Merge(ds2); ds.AcceptChanges(); } 使用强类型数据集 以下示例演示了如何使用由 XSD工具产生的强类型数据集。为了能工作起来,我们需要一个 XSD文件来指定恰当的架构(模式)。你可以手工 制作,或是使用 XSD工具来为你生成。为了让 NpgsqlDataAdapter产生 XSD,你需要给它提供一个 XML文件,XML文件的 XML架构允许推 理。 public void GenerateXmlFromDataSet(NpgsqlConnection conn) { conn.Open(); NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", conn); DataSet ds = new DataSet(); da.Fill(ds); ds.WriteXml("StrongDataSetFeed.xml"); } The example code results in a file which looks similar to: 1Random text
24
38
4true
5Text with ' single quote
下面这个命令使用以上文件来产生 XSD: xsd StrongDataSetFeed.xml XSD will produce an XML schema in which all types are specified as string. As a consequence, we need to change the XSD to specify the correct types, resulting in an XSD file similar to: XSD将产生一个 XML架构,里面的类都被指定为字符串。所以,我们还得将 XSD里面的类型指定为正确的类型。产生的 XSD文件类似于这样:
本文档为【Npgsql 用户手册 简体中文版】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_428606
暂无简介~
格式:pdf
大小:345KB
软件:PDF阅读器
页数:35
分类:互联网
上传时间:2012-11-06
浏览量:388