Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestSqlException.cs « Test « System.Data « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 294a1ddfba3a47e47f62bfc5be6cf254aec10ca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// TestSqlInsert.cs
//
// To Test SqlConnection and SqlCommand by connecting
// to a PostgreSQL database 
// and then executing an INSERT SQL statement
//
// To use:
//   change strings to your database, userid, tables, etc...:
//        connectionString
//        insertStatement
//
// To test:
//   mcs TestSqlInsert.cs -r System.Data
//   mint TestSqlInsert.exe
//
// Author:
//   Rodrigo Moya (rodrigo@ximian.com)
//   Daniel Morgan (danmorg@sc.rr.com)
//
// (C) Ximian, Inc 2002
//

using System;
using System.Data;
using System.Data.SqlClient;

namespace TestSystemDataSqlClient
{
	class TestSqlInsert
	{
		[STAThread]
		static void Main(string[] args) {
			SqlConnection conn;
			SqlCommand cmd;
			SqlTransaction trans;

			int rowsAffected;

			String connectionString;
			String insertStatement;
			String deleteStatement;
	
			connectionString = 
				"host=localhost;" +
				"dbname=test;" +
				"user=postgres";

			insertStatement = 
				"insert into NoSuchTable " +
				"(tid, tdesc) " +
				"values ('beer', 'Beer for All!') ";

			deleteStatement = 
				"delete from sometable " +
				"where tid = 'beer' ";

			try {
				// Connect to a PostgreSQL database
				Console.WriteLine ("Connect to database...");
				conn = new SqlConnection(connectionString);
				conn.Open();
			
				// begin transaction
				Console.WriteLine ("Begin Transaction...");
				trans = conn.BeginTransaction();

				// create SQL DELETE command
				Console.WriteLine ("Create Command initializing " +
					"with an DELETE statement...");
				cmd = new SqlCommand (deleteStatement, conn);

				// execute the DELETE SQL command
				Console.WriteLine ("Execute DELETE SQL Command...");
				rowsAffected = cmd.ExecuteNonQuery();
				Console.WriteLine ("Rows Affected: " + rowsAffected);

				// change the SQL command to an SQL INSERT Command
				Console.WriteLine ("Now use INSERT SQL Command...");
				cmd.CommandText = insertStatement;

				// execute the INSERT SQL command
				Console.WriteLine ("Execute INSERT SQL Command...");
				rowsAffected = cmd.ExecuteNonQuery();
				Console.WriteLine ("Rows Affected: " + rowsAffected);

				// if successfull at INSERT, commit the transaction,
				// otherwise, do a rollback the transaction using
				// trans.Rollback();
				Console.WriteLine ("Commit transaction...");
				trans.Commit();

				// Close connection to database
				Console.WriteLine ("Close database connection...");
				conn.Close();

				Console.WriteLine ("Assuming everything " +
					"was successful.");
				Console.WriteLine ("Verify data in database to " +
					"see if row is there.");
			}
			catch(SqlException e) {
				// Display the SQL Errors and Rollback the database
				Console.WriteLine("SqlException caught: " +
					e.ToString());
				if(trans != null) {
					trans.Rollback();
					Console.WriteLine("Database has been Rolled back!");
				}
			}
			finally {
				if(conn != null)
					if(conn.State == ConnectionState.Open)
						conn.Close();
			}
		}
	}
}