Permalink
Browse files

2003-01-15 Gonzalo Paniagua Javier <gonzalo@ximian.com>

	* INSTALL:
	* Makefile:
	* server/Makefile:
	* server/server.exe.config:
	* server/web.config:
	* test/dbpage1.aspx:
	* test/dbpage2.aspx: make the database accessing samples take some
	parameters from the configuration file to choose the IDbConnection
	and documented it.

svn path=/trunk/xsp/; revision=10530
  • Loading branch information...
1 parent de59603 commit d20b09d1cfee13738d8bac6719ceab02141a58c9 @gonzalop gonzalop committed Jan 15, 2003
Showing with 211 additions and 74 deletions.
  1. +4 −0 INSTALL
  2. +5 −0 Makefile
  3. +8 −0 server/ChangeLog
  4. +2 −2 server/Makefile
  5. +6 −0 server/server.exe.config
  6. +6 −0 server/web.config
  7. +7 −0 test/ChangeLog
  8. +90 −38 test/dbpage1.aspx
  9. +83 −34 test/dbpage2.aspx
View
4 INSTALL
@@ -15,3 +15,7 @@ Steps for installing the server:
Or you can use the --port command line option.
+ If you're gonna try the samples that use a database (such as dbpage1.aspx),
+ you may need to modify the values of DBProviderAssembly, DBConnectionType
+ and/or DbConnectionString in server.exe.config file.
+
View
5 Makefile
@@ -11,6 +11,11 @@ install: all
@echo 'Then point your web browser to http://127.0.0.1:8080/'
@echo 'You can change the default port (8080) in the server.exe.config file.'
@echo ""
+ @echo "If you're gonna try the samples that use a database (such as dbpage1.aspx),"
+ @echo "you may need to modify the values of DBProviderAssembly, DBConnectionType"
+ @echo "and/or DbConnectionString in server.exe.config file."
+
+ @echo ""
@echo 'Enjoy!'
@echo "-------------"
@echo ""
View
8 server/ChangeLog
@@ -1,3 +1,11 @@
+2003-01-15 Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+ * Makefile:
+ * server.exe.config:
+ * web.config: make the database accessing samples take some
+ parameters from the configuration file to choose the IDbConnection
+ and documented it.
+
2003-01-14 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* MonoWorkerRequest.cs:
View
4 server/Makefile
@@ -23,8 +23,8 @@ install: server.exe web.config global.asax
mkdir -p test/bin
cp server.exe *.config global.asax test
cp server.exe test/bin
- test -f server.dbg && cp server.dbg test
- test -f server.dbg && cp server.dbg test/bin
+ -test -f server.dbg && cp server.dbg test
+ -test -f server.dbg && cp server.dbg test/bin
cp ../test/*.aspx ../test/*.ascx ../test/*.xml ../test/*.png test
cp ../test/*.dll test/bin
# Remove next line once assembly loading is fixed.
View
6 server/server.exe.config
@@ -2,6 +2,12 @@
<configuration>
<appSettings>
<add key="MonoServerPort" value="8080"/>
+ <add key="DBProviderAssembly"
+ value="Mono.Data.PostgreSqlClient"/>
+ <add key="DBConnectionType"
+ value="Mono.Data.PostgreSqlClient.PgSqlConnection"/>
+ <add key="DBConnectionString"
+ value="hostaddr=127.0.0.1;user=monotest;password=monotest;dbname=monotest"/>
</appSettings>
</configuration>
View
6 server/web.config
@@ -6,6 +6,12 @@
</system.web>
<appSettings>
<add key="MonoServerPort" value="8080"/>
+ <add key="DBProviderAssembly"
+ value="Mono.Data.PostgreSqlClient"/>
+ <add key="DBConnectionType"
+ value="Mono.Data.PostgreSqlClient.PgSqlConnection"/>
+ <add key="DBConnectionString"
+ value="hostaddr=127.0.0.1;user=monotest;password=monotest;dbname=monotest"/>
</appSettings>
</configuration>
View
7 test/ChangeLog
@@ -1,3 +1,10 @@
+2003-01-15 Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+ * dbpage1.aspx:
+ * dbpage2.aspx: make the database accessing samples take some
+ parameters from the configuration file to choose the IDbConnection
+ and documented it.
+
2003-01-08 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* web_datagrid.aspx: simple datagrid test.
View
128 test/dbpage1.aspx
@@ -1,42 +1,73 @@
<%@ language="C#" %>
-<%@ import namespace="System" %>
+<%@ import namespace="System.Configuration" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Reflection" %>
<html>
<script runat=server>
- // FIXME: temporary hack to get this working
- static Assembly dbAssembly = null;
- static Type typ = null;
+ static Type cncType = null;
- /* You must setup a user (monotest, pw=monotest), a database
- *called monotest with a table called test which has two columns:
- *person and email
- */
-
- IDbConnection cnc;
- void Page_Init (object sender, EventArgs e)
+ void GetConnectionData (out string providerAssembly, out string cncTypeName, out string cncString)
{
- // FIXME: temporary hack to get this working until
- // we can use global.asax file (no support fot it yet)
- // to dynamically load an assembly
- if(dbAssembly == null) {
- const string connectionTypeName = "Mono.Data.PostgreSqlClient.PgSqlConnection";
- const string providerAssemblyName = "Mono.Data.PostgreSqlClient";
- dbAssembly = Assembly.Load (providerAssemblyName);
- typ = dbAssembly.GetType (connectionTypeName);
+ providerAssembly = null;
+ cncTypeName = null;
+ cncString = null;
+ NameValueCollection config = ConfigurationSettings.AppSettings as NameValueCollection;
+ if (config != null) {
+ foreach (string s in config.Keys) {
+ if (0 == String.Compare ("DBProviderAssembly", s, true)) {
+ providerAssembly = config [s];
+ } else if (0 == String.Compare ("DBConnectionType", s, true)) {
+ cncTypeName = config [s];
+ } else if (0 == String.Compare ("DBConnectionString", s, true)) {
+ cncString = config [s];
+ }
+ }
}
- cnc = (IDbConnection) Activator.CreateInstance (typ);
+ if (providerAssembly == null || providerAssembly == "")
+ providerAssembly = "Mono.Data.PostgreSqlClient";
- string connectionString = "hostaddr=127.0.0.1;" +
- "user=monotest;" +
- "password=monotest;" +
- "dbname=monotest";
+ if (cncTypeName == null || cncTypeName == "")
+ cncTypeName = "Mono.Data.PostgreSqlClient.PgSqlConnection";
+
+ if (cncString == null || cncString == "")
+ cncString = "hostaddr=127.0.0.1;user=monotest;password=monotest;dbname=monotest";
+ }
+
+ void ShowError (Exception exc)
+ {
+ noDBLine.InnerHtml += "<p><b>The error was:</b>\n<pre> " + exc + "</pre><p>";
+ theForm.Visible = false;
+ noDBLine.Visible = true;
+ }
+
+ IDbConnection cnc;
+ void Page_Init (object sender, EventArgs e)
+ {
+ string connectionTypeName;
+ string providerAssemblyName;
+ string cncString;
- cnc.ConnectionString = connectionString;
- cnc.Open ();
+ GetConnectionData (out providerAssemblyName, out connectionTypeName, out cncString);
+ if (cncType == null) {
+ Assembly dbAssembly = Assembly.Load (providerAssemblyName);
+ cncType = dbAssembly.GetType (connectionTypeName, true);
+ if (!typeof (IDbConnection).IsAssignableFrom (cncType))
+ throw new ApplicationException ("The type '" + cncType +
+ "' does not implement IDbConnection.\n" +
+ "Check 'DbConnectionType' in server.exe.config.");
+ }
+
+ cnc = (IDbConnection) Activator.CreateInstance (cncType);
+ cnc.ConnectionString = cncString;
+ try {
+ cnc.Open ();
+ } catch (Exception exc) {
+ ShowError (exc);
+ cnc = null;
+ }
}
void Page_Load (object sender, EventArgs e)
@@ -51,10 +82,9 @@
void Filter_Changed (object sender, EventArgs e)
{
UpdateTable (PersonFilter.Text, MailFilter.Text);
-
}
- private void UpdateTable (string filterPerson, string filterMail)
+ void UpdateTable (string filterPerson, string filterMail)
{
IDbCommand selectCommand = cnc.CreateCommand();
IDataReader reader;
@@ -64,16 +94,19 @@
"email like '" + filterMail + "'";
selectCommand.CommandText = selectCmd;
- reader = selectCommand.ExecuteReader ();
- while (reader.Read ()) {
- TableRow row = new TableRow ();
- for (int i = 0; i < reader.FieldCount; i++) {
- TableCell cell = new TableCell ();
- cell.Controls.Add (new LiteralControl (reader.GetValue (i).ToString ()));
- row.Cells.Add (cell);
+ try {
+ reader = selectCommand.ExecuteReader ();
+ while (reader.Read ()) {
+ TableRow row = new TableRow ();
+ for (int i = 0; i < reader.FieldCount; i++) {
+ TableCell cell = new TableCell ();
+ cell.Controls.Add (new LiteralControl (reader.GetValue (i).ToString ()));
+ row.Cells.Add (cell);
+ }
+ myTable.Rows.Add (row);
}
- myTable.Rows.Add (row);
-
+ } catch (Exception exc) {
+ ShowError (exc);
}
}
@@ -82,8 +115,27 @@
<title>Some DB testing</title>
</head>
<body>
+<span runat="server" visible="false" id="noDBLine">
+<h3>Database Error</h3>
+Sorry, a database error has occurred.
+<p>
+You should set up a database for user <i>'monotest'</i>,
+password <i>'monotest'</i> and dbname <i>'monotest'</i>.
+<p>
+Then modify the variables DBProviderAssembly, DBConnectionType and
+DBConnectionString in server.exe.config file to fit your needs.
+<p>
+The database should have a table called customers created with the following command (or similar):
+<pre>
+CREATE TABLE "test" (
+ "person" character varying(256) NOT NULL,
+ "email" character varying(256) NOT NULL
+);
+
+</pre>
+</span>
+<form id="theForm" runat="server">
Choose the SQL filters and click 'Submit'.
-<form runat="server">
<asp:Label Text="Person Filter: " />
<asp:TextBox id="PersonFilter" Text="" TextMode="singleLine" OnTextChanged="Filter_Changed" runat="server" maxlength=40 />
<p>
View
117 test/dbpage2.aspx
@@ -1,6 +1,7 @@
<%@ language="C#" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
+<%@ import namespace="System.Reflection" %>
<%@ Register TagPrefix="Mono" Namespace="Mono.Controls" assembly="tabcontrol2" %>
<html>
<!-- You must compile tabcontrol2.cs and copy the dll to the output/ directory -->
@@ -10,26 +11,68 @@
-->
<script runat=server>
- SqlConnection cnc;
- void Page_Init (object sender, EventArgs e)
+ static Type cncType = null;
+
+ void GetConnectionData (out string providerAssembly, out string cncTypeName, out string cncString)
{
- if (cnc == null) {
- cnc = new SqlConnection ();
- string connectionString = "hostaddr=127.0.0.1;" +
- "user=monotest;" +
- "password=monotest;" +
- "dbname=monotest";
-
- cnc.ConnectionString = connectionString;
- try {
- cnc.Open ();
- } catch (Exception e2) {
- noDBLine.InnerHtml += "<p><b>The error was:</b>\n<pre> " + e2.Message + "</pre><p>";
- cnc = null;
- theForm.Visible = false;
- noDBLine.Visible = true;
+ providerAssembly = null;
+ cncTypeName = null;
+ cncString = null;
+ NameValueCollection config = ConfigurationSettings.AppSettings as NameValueCollection;
+ if (config != null) {
+ foreach (string s in config.Keys) {
+ if (0 == String.Compare ("DBProviderAssembly", s, true)) {
+ providerAssembly = config [s];
+ } else if (0 == String.Compare ("DBConnectionType", s, true)) {
+ cncTypeName = config [s];
+ } else if (0 == String.Compare ("DBConnectionString", s, true)) {
+ cncString = config [s];
+ }
}
}
+
+ if (providerAssembly == null || providerAssembly == "")
+ providerAssembly = "Mono.Data.PostgreSqlClient";
+
+ if (cncTypeName == null || cncTypeName == "")
+ cncTypeName = "Mono.Data.PostgreSqlClient.PgSqlConnection";
+
+ if (cncString == null || cncString == "")
+ cncString = "hostaddr=127.0.0.1;user=monotest;password=monotest;dbname=monotest";
+ }
+
+ void ShowError (Exception exc)
+ {
+ noDBLine.InnerHtml += "<p><b>The error was:</b>\n<pre> " + exc + "</pre><p>";
+ theForm.Visible = false;
+ noDBLine.Visible = true;
+ }
+
+ IDbConnection cnc;
+ void Page_Init (object sender, EventArgs e)
+ {
+ string connectionTypeName;
+ string providerAssemblyName;
+ string cncString;
+
+ GetConnectionData (out providerAssemblyName, out connectionTypeName, out cncString);
+ if (cncType == null) {
+ Assembly dbAssembly = Assembly.Load (providerAssemblyName);
+ cncType = dbAssembly.GetType (connectionTypeName, true);
+ if (!typeof (IDbConnection).IsAssignableFrom (cncType))
+ throw new ApplicationException ("The type '" + cncType +
+ "' does not implement IDbConnection.\n" +
+ "Check 'DbConnectionType' in server.exe.config.");
+ }
+
+ cnc = (IDbConnection) Activator.CreateInstance (cncType);
+ cnc.ConnectionString = cncString;
+ try {
+ cnc.Open ();
+ } catch (Exception exc) {
+ ShowError (exc);
+ cnc = null;
+ }
}
void Page_PreRender (object sender, EventArgs e)
@@ -60,21 +103,25 @@
IDbCommand selectCommand = cnc.CreateCommand();
selectCommand.CommandText = selectCmd;
- IDataReader reader = selectCommand.ExecuteReader ();
- table.Rows.Clear ();
- while (reader.Read ()) {
- TableRow row = new TableRow ();
- for (int i = 0; i < reader.FieldCount; i++) {
- TableCell cell = new TableCell ();
- object data = reader.GetValue (i);
- if (data == null)
- data = "(null)";
- cell.Controls.Add (new LiteralControl (data.ToString ()));
- row.Cells.Add (cell);
+ try {
+ IDataReader reader = selectCommand.ExecuteReader ();
+ table.Rows.Clear ();
+ while (reader.Read ()) {
+ TableRow row = new TableRow ();
+ for (int i = 0; i < reader.FieldCount; i++) {
+ TableCell cell = new TableCell ();
+ object data = reader.GetValue (i);
+ if (data == null)
+ data = "(null)";
+ cell.Controls.Add (new LiteralControl (data.ToString ()));
+ row.Cells.Add (cell);
+ }
+ table.Rows.Add (row);
}
- table.Rows.Add (row);
+ reader.Close ();
+ } catch (Exception exc) {
+ ShowError (exc);
}
- reader.Close ();
}
private void DoInsert (uint dbid, string dbname, string dbaddress)
@@ -240,12 +287,14 @@
</head>
<body>
<span runat="server" visible="false" id="noDBLine">
-<h3>Error connecting to database</h3>
+<h3>Database Error</h3>
Sorry, could not connect to a database.
<p>
-You should set up a PostgreSQL database for user <i>'monotest'</i>,
-password <i>'monotest'</i> and dbname <i>'monotest'</i> accesible
-through the loopback interface.
+You should set up a database for user <i>'monotest'</i>,
+password <i>'monotest'</i> and dbname <i>'monotest'</i>
+<p>
+Then modify the variables DBProviderAssembly, DBConnectionType and
+DBConnectionString in server.exe.config file to fit your needs.
<p>
The database should have a table called customers created with the following command:
<pre>

0 comments on commit d20b09d

Please sign in to comment.