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

TestSqlConnection.cs « Test « System.Data « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b1c23d698515c1931f5b10d85ae473858a8ef263 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// TestSqlConnection.cs - tests connection via ServerName:
//   "Server=hostname"
//   "Server=hostname\\instance"
//   "Server=hostname,port"
//
// Test Connections for SqlClient, SybaseClient, and TdsClient
//
// Author: 
//      Daniel Morgan <danmorg@sc.rr.com>
//
// Copyright (C) Daniel Morgan, 2003
//
// To build this test on Linux:
// mcs TestSqlConnection.cs -r System.Data.dll \
//     -r Mono.Data.SybaseClient.dll -r Mono.Data.TdsClient.dll
//
// To build this test on Windows via Cygwin:
// mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe TestSqlConnection.cs \
//      -lib:C:/cygwin/home/MyHome/mono/install/lib -r System.Data.dll \
//      -r Mono.Data.SybaseClient.dll -r Mono.Data.TdsClient.dll
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

//#define IncludeSybaseAndTdsClient

using System;
using System.Data;
using System.Data.SqlClient;
#if IncludeSybaseAndTdsClient
	using Mono.Data.TdsClient;
	using Mono.Data.SybaseClient;
#endif // IncludeSybaseAndTdsClient

public class TestSqlConnection 
{
	public static void Main(string[] args) 
	{
		Console.WriteLine("Start TestSqlConnection.");
		if (args.Length != 6 && args.Length != 7) {
			Console.WriteLine(
				"\nUsage: mono TestSqlConnection.exe Client Table Column Server Database UserID [Password]\n\n" +
#if IncludeSybaseAndTdsClient
				"\tClient is one of the following: SqlClient, TdsClient, or SybaseClient\n" +
#else
				"\tClient is: SqlClient.  No support for TdsClient nor SybaseClient\n" +
#endif // IncludeSybaseAndTdsClient
				"\tTable is the name of the database table to select from\n" +
				"\tColumn is the name of the column in the Table to select from\n" +
				"\tServer is the SQL Server to connect.  Use one of the following forms:\n" +
				"\t\tHOSTNAME            Ex: MYHOST\n" +
				"\t\tHOSTNAME,port       Ex: MYHOST,1433\n" +
				"\t\tHOSTNAME\\\\instance  Ex: MYHOST\\\\NETSDK  Note: only works with SqlClient\n" +
				"\tDatabase is the name of the database to use\n" +
				"\tUser ID is the user's User ID\n" +
				"\tPassword is the user's Password   Note: if ommitted, a blank password is used\n" +
				"Exampes:\n" +
				"\tEx 1: SqlClient employee lname MYHOST pubs myuserid mypassword\n" +
				"\tEx 3: SqlClient employee lname MYHOST,1443 pubs myuserid mypassword\n" +
				"\tEx 2: SqlClient Products ProductName MYHOST\\\\NETSDK myuserid mypassword\n" +
				"\tEx 4: SqlClient employee lname MYHOST pubs myuserid\n" +
				"\tEx 5: TdsClient sometable somecolumn MYHOST test myuserid mypassword\n" +
				"\tEx 6: SybaseClient sometable somecolumn MYHOST test myuserid mypassword\n");

			return;
		}

		string client = args[0];
		string tableName = args[1];
		string columnName = args[2];
		
		string server = args[3];
		string database = args[4];
		string userid = args[5];
		string password = "";
		if (args.Length == 7)
			password  = args[6];
		
		string constr;
		string sql;

		Console.WriteLine("\nClient: " + client);
		Console.WriteLine("Table Name: " + tableName);
		Console.WriteLine("Column Name: " + columnName);
		Console.WriteLine("Server: " + server);
		Console.WriteLine("Database: " + database);
		Console.WriteLine("User ID: " + userid);
		Console.WriteLine("Password: " + password);

		sql = "SELECT " + columnName + " FROM " + tableName;
		
		constr = 
			"Server=" + server + ";" + 
			"Database=" + database + ";" +
			"User ID=" + userid + ";" +
			"Password=" + password + ";";	

		Console.WriteLine("\nConnectionString: " + constr);
		Console.WriteLine("SQL: " + sql);
		
		Console.WriteLine("\nCreating Connection...");

		IDbConnection con = null;
		switch (client.ToUpper()) {
		case "SQLCLIENT":
			con = new SqlConnection();
			break;
#if IncludeSybaseAndTdsClient
		case "TDSCLIENT":
			con = new TdsConnection();
			break;
		case "SYBASECLIENT":
			con = new SybaseConnection();
			break;
		default:
			Console.WriteLine("Invalid client: " + client + "\nUse SqlClient, TdsClient, or SybaseClient");
			return;
#else
		default:
			Console.WriteLine("Invalid client: " + client + "\nUse SqlClient.  No support for TdsClient nor SybaseClient.");
			return;

#endif
		}
		Console.WriteLine("set connection string...");
		con.ConnectionString = constr;
		Console.WriteLine("open connection...");
		try {
			con.Open();
		}
		catch(SqlException se) {
			Console.WriteLine("SqlException caught");
			Console.WriteLine("Message: " + se.Message);
			Console.WriteLine("Procedure: " + se.Procedure);
			Console.WriteLine("Class: " + se.Class);
			Console.WriteLine("Number: " + se.Number);
			Console.WriteLine("Source: " + se.Source);
			Console.WriteLine("State: " + se.State);
			Console.WriteLine("Errors:");
			foreach(SqlError error in se.Errors) {
				Console.WriteLine("  SqlError:");
				Console.WriteLine("     Message: " + se.Message);
				Console.WriteLine("     Line Number: " + se.LineNumber);
				Console.WriteLine("     Procedure: " + se.Procedure);
				Console.WriteLine("     Class: " + se.Class);
				Console.WriteLine("     Number: " + se.Number);
				Console.WriteLine("     Server: " + se.Server);
				Console.WriteLine("     Source: " + se.Source);
				Console.WriteLine("     State: " + se.State);
			}
			Console.WriteLine("StackTrace: " + se.StackTrace);
			Console.WriteLine("TargetSite: " + se.TargetSite);
			Exception ie = se.InnerException;
			if(ie != null) {
				Console.WriteLine("InnerException:");
				Console.WriteLine("   Message: " + se.Message);
				Console.WriteLine("   Class: " + se.Class);
				Console.WriteLine("   Number: " + se.Number);
				Console.WriteLine("   Source: " + se.Source);
				Console.WriteLine("   State: " + se.State);
				Console.WriteLine("   StackTrace: " + se.StackTrace);
				Console.WriteLine("   TargetSite: " + se.TargetSite);
			}
			return;
		}
		Console.WriteLine("Creating command...");
		IDbCommand cmd = con.CreateCommand();
		Console.WriteLine("set SQL...");
		cmd.CommandText = sql;
		Console.WriteLine("execute reader...");
		IDataReader reader = cmd.ExecuteReader();
		Console.WriteLine("read first row...");
		if(reader.Read()) {
			Console.WriteLine("  Value: " + reader[columnName].ToString());
		}
		else {
			Console.WriteLine("  No data returned.  Or either, no permission to read data.");
		}

		Console.WriteLine("Clean up...");
		// clean up
		reader.Close();
		reader = null;
		cmd.Dispose();
		cmd = null;
		con.Close();
		con = null;
		Console.WriteLine("Done.");
	}
}