介绍
介绍如何使用和PostgreSQL创建CRUD (创建,检索,更新和删除)表单,使用sharpdevelop3.1 、postgresql8.3forwindows,Npgsql,将应用程序连接到PostgreSQL服务器。
创建数据库
创建表:
/* This is our table to store the artists info, we define a primary key so we can edit our data in PgAdmin */CREATETABLE artists ( artistid integer NOTNULL, name character varyingNOTNULLDEFAULT100, CONSTRAINT pk_artist PRIMARYKEY (artistid) );
现在,应该在表中插入几行,可以测试在表单中编辑数据的能力,可以使用PgAdmin数据编辑器插入数据:
现在数据库已经准备好了,我们可以创建SharpDevelop项目。
创建解决方案
使用SharpDevelop,创建一个新的解决方案,在c#类别下选择"Window Application";在示例中,解决方案名称为Music,
选择并添加两个Label
组件,并添加两个TextBox
es;
在"工具"菜单的"数据"类别下,选择以下组件并将其添加到表单中:
组件 | 名称 | 需要- |
BindingSource | bsource | 它将用作绑定导航器和数据之间的链接, |
BindingNavigator | bnavigator | 它将用作我们对数据的主要控制, |
DataSet | mainDS | 此组件将用作主数据源, |
现在,在自定义组件下,必须添加以下组件:
组件 | 名称 | 需要- |
NpgsqlConnection | connection | PG服务器连接, |
NpgsqlDataAdapter | adapter | 这个组件使用下面的命令保持控件连接到表, |
NpgsqlCommand | cmdSelect | 这是Create命令, |
NpgsqlCommand | cmdInsert | 这是检索命令, |
NpgsqlCommand | cmdDelete | 这是删除命令, |
NpgsqlCommand | cmdUpdate | 这是更新命令, |
OK,添加所有这些组件后,表单应该如下所示:
- 在
BindingNavigator
bnavigator
上,将BindingSource
属性设置为bsource
- 在
BindingSource
bsource
上,将DataSource
属性设置为mainDS
- 在连接上,将
ConnectionString
属性设置为如下内容:HOST=localhost;DATABASE=music;USER ID=postgres;PASSWORD=admin (根据服务器主机,数据库和用户信息更改此项) - 在适配器上,将属性
DeleteCommand
,InsertCommand
,SelectCommand
和UpdateCommand
设置为各自的组件
- 现在我们必须编辑命令的属性,每个属性都根据函数;在四个组件上,我们必须设置
connection
属性来使用连接组件 - 在
cmdSelect
上,将Command Text
属性设置为:SELECT artistID,name from artists
- 在
cmdInsert
上,将Command Text
属性设置为:INSERT into artists (artistID,name) VALUES (:p_artistID, :p_name )
注意:p_xxx
是SQL命令使用的参数;不久,将这些参数绑定到表单中的TextBox
元素 - 在
cmdUpdate
上,将Command Text
属性设置为:UPDATE artists SET name=:p_name where artistID=:p_artistID
- 在
cmdDelete
上,将Command Text
属性设置为:DELETE from artists where artistID=:p_artistID
属性被设置好了,
必须添加:
Using Npgsql;
在Music.cs文件的开头,位于其他using
子句之后。
现在,看看MainFormLoad
代码:
void MainFormLoad(object sender, EventArgs e) { // We fill our DataSet and we set the table in the Dataset as"artists" adapter.Fill(mainDS,"artists"); // We must set the DataMember property in the BindingSource// it cannot be set at the designer since our DataSet isn't populated bsource.DataMember="artists"; // We add the DataBinding to the TextBox linked to the"Text" property// and linked to the BindingSource t_artistID.DataBindings.Add("Text",bsource,"artistid"); t_name.DataBindings.Add("Text",bsource,"name"); // Now we must set the Relation between the parameters and the fields// in the Database cmdInsert.Parameters.Add(new NpgsqlParameter("p_artistID", NpgsqlTypes.NpgsqlDbType.Integer,0,"artistid")); cmdInsert.Parameters.Add(new NpgsqlParameter("p_name", NpgsqlTypes.NpgsqlDbType.Varchar,0,"name")); cmdDelete.Parameters.Add(new NpgsqlParameter("p_artistID", NpgsqlTypes.NpgsqlDbType.Integer,0,"artistid")); cmdUpdate.Parameters.Add(new NpgsqlParameter("p_artistID", NpgsqlTypes.NpgsqlDbType.Integer,0,"artistid")); cmdUpdate.Parameters.Add(new NpgsqlParameter("p_name", NpgsqlTypes.NpgsqlDbType.Varchar,0,"name")); }
请注意,在每个命令上,我们必须设置此命令所使用的参数,并特别注意根据数据库中的数据类型设置各自的DBType
。
转到代表BindingNavigator
的工具栏,并添加新的Button
:
你可以添加此图像:
使用新创建的按钮的Image
属性,现在,双击new按钮打开button事件处理程序:
void ToolStripButton1Click(object sender, EventArgs e) { // We call this method to officially end the editing of the Bindingsource bsource.EndEdit(); // Now we will try to update our tabletry { adapter.Update(mainDS.Tables["artists"]); } // We must catch the Exception, because the user may cause a SQL exceptioncatch (Exception e_exception) { MessageBox.Show("Error...."+e_exception.Message,"Error trying to commit"); } }
现在,我们可以使用新表单创建,检索,更新和删除数据。