TestSqlConnection.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // TestSqlConnection.cs - tests connection via ServerName:
  3. // "Server=hostname"
  4. // "Server=hostname\\instance"
  5. // "Server=hostname,port"
  6. //
  7. // Test Connections for SqlClient, SybaseClient, and TdsClient
  8. //
  9. // Author:
  10. // Daniel Morgan <[email protected]>
  11. //
  12. // Copyright (C) Daniel Morgan, 2003
  13. //
  14. // To build this test on Linux:
  15. // mcs TestSqlConnection.cs -r System.Data.dll \
  16. // -r Mono.Data.SybaseClient.dll -r Mono.Data.TdsClient.dll
  17. //
  18. // To build this test on Windows via Cygwin:
  19. // mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe TestSqlConnection.cs \
  20. // -lib:C:/cygwin/home/MyHome/mono/install/lib -r System.Data.dll \
  21. // -r Mono.Data.SybaseClient.dll -r Mono.Data.TdsClient.dll
  22. //
  23. //
  24. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  25. //
  26. // Permission is hereby granted, free of charge, to any person obtaining
  27. // a copy of this software and associated documentation files (the
  28. // "Software"), to deal in the Software without restriction, including
  29. // without limitation the rights to use, copy, modify, merge, publish,
  30. // distribute, sublicense, and/or sell copies of the Software, and to
  31. // permit persons to whom the Software is furnished to do so, subject to
  32. // the following conditions:
  33. //
  34. // The above copyright notice and this permission notice shall be
  35. // included in all copies or substantial portions of the Software.
  36. //
  37. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  38. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  40. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  41. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  42. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  43. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  44. //
  45. //#define IncludeSybaseAndTdsClient
  46. using System;
  47. using System.Data;
  48. using System.Data.SqlClient;
  49. #if IncludeSybaseAndTdsClient
  50. using Mono.Data.TdsClient;
  51. using Mono.Data.SybaseClient;
  52. #endif // IncludeSybaseAndTdsClient
  53. public class TestSqlConnection
  54. {
  55. public static void Main(string[] args)
  56. {
  57. Console.WriteLine("Start TestSqlConnection.");
  58. if (args.Length != 6 && args.Length != 7) {
  59. Console.WriteLine(
  60. "\nUsage: mono TestSqlConnection.exe Client Table Column Server Database UserID [Password]\n\n" +
  61. #if IncludeSybaseAndTdsClient
  62. "\tClient is one of the following: SqlClient, TdsClient, or SybaseClient\n" +
  63. #else
  64. "\tClient is: SqlClient. No support for TdsClient nor SybaseClient\n" +
  65. #endif // IncludeSybaseAndTdsClient
  66. "\tTable is the name of the database table to select from\n" +
  67. "\tColumn is the name of the column in the Table to select from\n" +
  68. "\tServer is the SQL Server to connect. Use one of the following forms:\n" +
  69. "\t\tHOSTNAME Ex: MYHOST\n" +
  70. "\t\tHOSTNAME,port Ex: MYHOST,1433\n" +
  71. "\t\tHOSTNAME\\\\instance Ex: MYHOST\\\\NETSDK Note: only works with SqlClient\n" +
  72. "\tDatabase is the name of the database to use\n" +
  73. "\tUser ID is the user's User ID\n" +
  74. "\tPassword is the user's Password Note: if ommitted, a blank password is used\n" +
  75. "Exampes:\n" +
  76. "\tEx 1: SqlClient employee lname MYHOST pubs myuserid mypassword\n" +
  77. "\tEx 3: SqlClient employee lname MYHOST,1443 pubs myuserid mypassword\n" +
  78. "\tEx 2: SqlClient Products ProductName MYHOST\\\\NETSDK myuserid mypassword\n" +
  79. "\tEx 4: SqlClient employee lname MYHOST pubs myuserid\n" +
  80. "\tEx 5: TdsClient sometable somecolumn MYHOST test myuserid mypassword\n" +
  81. "\tEx 6: SybaseClient sometable somecolumn MYHOST test myuserid mypassword\n");
  82. return;
  83. }
  84. string client = args[0];
  85. string tableName = args[1];
  86. string columnName = args[2];
  87. string server = args[3];
  88. string database = args[4];
  89. string userid = args[5];
  90. string password = "";
  91. if (args.Length == 7)
  92. password = args[6];
  93. string constr;
  94. string sql;
  95. Console.WriteLine("\nClient: " + client);
  96. Console.WriteLine("Table Name: " + tableName);
  97. Console.WriteLine("Column Name: " + columnName);
  98. Console.WriteLine("Server: " + server);
  99. Console.WriteLine("Database: " + database);
  100. Console.WriteLine("User ID: " + userid);
  101. Console.WriteLine("Password: " + password);
  102. sql = "SELECT " + columnName + " FROM " + tableName;
  103. constr =
  104. "Server=" + server + ";" +
  105. "Database=" + database + ";" +
  106. "User ID=" + userid + ";" +
  107. "Password=" + password + ";";
  108. Console.WriteLine("\nConnectionString: " + constr);
  109. Console.WriteLine("SQL: " + sql);
  110. Console.WriteLine("\nCreating Connection...");
  111. IDbConnection con = null;
  112. switch (client.ToUpper()) {
  113. case "SQLCLIENT":
  114. con = new SqlConnection();
  115. break;
  116. #if IncludeSybaseAndTdsClient
  117. case "TDSCLIENT":
  118. con = new TdsConnection();
  119. break;
  120. case "SYBASECLIENT":
  121. con = new SybaseConnection();
  122. break;
  123. default:
  124. Console.WriteLine("Invalid client: " + client + "\nUse SqlClient, TdsClient, or SybaseClient");
  125. return;
  126. #else
  127. default:
  128. Console.WriteLine("Invalid client: " + client + "\nUse SqlClient. No support for TdsClient nor SybaseClient.");
  129. return;
  130. #endif
  131. }
  132. Console.WriteLine("set connection string...");
  133. con.ConnectionString = constr;
  134. Console.WriteLine("open connection...");
  135. try {
  136. con.Open();
  137. }
  138. catch(SqlException se) {
  139. Console.WriteLine("SqlException caught");
  140. Console.WriteLine("Message: " + se.Message);
  141. Console.WriteLine("Procedure: " + se.Procedure);
  142. Console.WriteLine("Class: " + se.Class);
  143. Console.WriteLine("Number: " + se.Number);
  144. Console.WriteLine("Source: " + se.Source);
  145. Console.WriteLine("State: " + se.State);
  146. Console.WriteLine("Errors:");
  147. foreach(SqlError error in se.Errors) {
  148. Console.WriteLine(" SqlError:");
  149. Console.WriteLine(" Message: " + se.Message);
  150. Console.WriteLine(" Line Number: " + se.LineNumber);
  151. Console.WriteLine(" Procedure: " + se.Procedure);
  152. Console.WriteLine(" Class: " + se.Class);
  153. Console.WriteLine(" Number: " + se.Number);
  154. Console.WriteLine(" Server: " + se.Server);
  155. Console.WriteLine(" Source: " + se.Source);
  156. Console.WriteLine(" State: " + se.State);
  157. }
  158. Console.WriteLine("StackTrace: " + se.StackTrace);
  159. Console.WriteLine("TargetSite: " + se.TargetSite);
  160. Exception ie = se.InnerException;
  161. if(ie != null) {
  162. Console.WriteLine("InnerException:");
  163. Console.WriteLine(" Message: " + se.Message);
  164. Console.WriteLine(" Class: " + se.Class);
  165. Console.WriteLine(" Number: " + se.Number);
  166. Console.WriteLine(" Source: " + se.Source);
  167. Console.WriteLine(" State: " + se.State);
  168. Console.WriteLine(" StackTrace: " + se.StackTrace);
  169. Console.WriteLine(" TargetSite: " + se.TargetSite);
  170. }
  171. return;
  172. }
  173. Console.WriteLine("Creating command...");
  174. IDbCommand cmd = con.CreateCommand();
  175. Console.WriteLine("set SQL...");
  176. cmd.CommandText = sql;
  177. Console.WriteLine("execute reader...");
  178. IDataReader reader = cmd.ExecuteReader();
  179. Console.WriteLine("read first row...");
  180. if(reader.Read()) {
  181. Console.WriteLine(" Value: " + reader[columnName].ToString());
  182. }
  183. else {
  184. Console.WriteLine(" No data returned. Or either, no permission to read data.");
  185. }
  186. Console.WriteLine("Clean up...");
  187. // clean up
  188. reader.Close();
  189. reader = null;
  190. cmd.Dispose();
  191. cmd = null;
  192. con.Close();
  193. con = null;
  194. Console.WriteLine("Done.");
  195. }
  196. }