TestSqlConnection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //#define IncludeSybaseAndTdsClient
  24. using System;
  25. using System.Data;
  26. using System.Data.SqlClient;
  27. #if IncludeSybaseAndTdsClient
  28. using Mono.Data.TdsClient;
  29. using Mono.Data.SybaseClient;
  30. #endif // IncludeSybaseAndTdsClient
  31. public class TestSqlConnection
  32. {
  33. public static void Main(string[] args)
  34. {
  35. Console.WriteLine("Start TestSqlConnection.");
  36. if (args.Length != 6 && args.Length != 7) {
  37. Console.WriteLine(
  38. "\nUsage: mono TestSqlConnection.exe Client Table Column Server Database UserID [Password]\n\n" +
  39. #if IncludeSybaseAndTdsClient
  40. "\tClient is one of the following: SqlClient, TdsClient, or SybaseClient\n" +
  41. #else
  42. "\tClient is: SqlClient. No support for TdsClient nor SybaseClient\n" +
  43. #endif // IncludeSybaseAndTdsClient
  44. "\tTable is the name of the database table to select from\n" +
  45. "\tColumn is the name of the column in the Table to select from\n" +
  46. "\tServer is the SQL Server to connect. Use one of the following forms:\n" +
  47. "\t\tHOSTNAME Ex: MYHOST\n" +
  48. "\t\tHOSTNAME,port Ex: MYHOST,1433\n" +
  49. "\t\tHOSTNAME\\\\instance Ex: MYHOST\\\\NETSDK Note: only works with SqlClient\n" +
  50. "\tDatabase is the name of the database to use\n" +
  51. "\tUser ID is the user's User ID\n" +
  52. "\tPassword is the user's Password Note: if ommitted, a blank password is used\n" +
  53. "Exampes:\n" +
  54. "\tEx 1: SqlClient employee lname MYHOST pubs myuserid mypassword\n" +
  55. "\tEx 3: SqlClient employee lname MYHOST,1443 pubs myuserid mypassword\n" +
  56. "\tEx 2: SqlClient Products ProductName MYHOST\\\\NETSDK myuserid mypassword\n" +
  57. "\tEx 4: SqlClient employee lname MYHOST pubs myuserid\n" +
  58. "\tEx 5: TdsClient sometable somecolumn MYHOST test myuserid mypassword\n" +
  59. "\tEx 6: SybaseClient sometable somecolumn MYHOST test myuserid mypassword\n");
  60. return;
  61. }
  62. string client = args[0];
  63. string tableName = args[1];
  64. string columnName = args[2];
  65. string server = args[3];
  66. string database = args[4];
  67. string userid = args[5];
  68. string password = "";
  69. if (args.Length == 7)
  70. password = args[6];
  71. string constr;
  72. string sql;
  73. Console.WriteLine("\nClient: " + client);
  74. Console.WriteLine("Table Name: " + tableName);
  75. Console.WriteLine("Column Name: " + columnName);
  76. Console.WriteLine("Server: " + server);
  77. Console.WriteLine("Database: " + database);
  78. Console.WriteLine("User ID: " + userid);
  79. Console.WriteLine("Password: " + password);
  80. sql = "SELECT " + columnName + " FROM " + tableName;
  81. constr =
  82. "Server=" + server + ";" +
  83. "Database=" + database + ";" +
  84. "User ID=" + userid + ";" +
  85. "Password=" + password + ";";
  86. Console.WriteLine("\nConnectionString: " + constr);
  87. Console.WriteLine("SQL: " + sql);
  88. Console.WriteLine("\nCreating Connection...");
  89. IDbConnection con = null;
  90. switch (client.ToUpper()) {
  91. case "SQLCLIENT":
  92. con = new SqlConnection();
  93. break;
  94. #if IncludeSybaseAndTdsClient
  95. case "TDSCLIENT":
  96. con = new TdsConnection();
  97. break;
  98. case "SYBASECLIENT":
  99. con = new SybaseConnection();
  100. break;
  101. default:
  102. Console.WriteLine("Invalid client: " + client + "\nUse SqlClient, TdsClient, or SybaseClient");
  103. return;
  104. #else
  105. default:
  106. Console.WriteLine("Invalid client: " + client + "\nUse SqlClient. No support for TdsClient nor SybaseClient.");
  107. return;
  108. #endif
  109. }
  110. Console.WriteLine("set connection string...");
  111. con.ConnectionString = constr;
  112. Console.WriteLine("open connection...");
  113. try {
  114. con.Open();
  115. }
  116. catch(SqlException se) {
  117. Console.WriteLine("SqlException caught");
  118. Console.WriteLine("Message: " + se.Message);
  119. Console.WriteLine("Procedure: " + se.Procedure);
  120. Console.WriteLine("Class: " + se.Class);
  121. Console.WriteLine("Number: " + se.Number);
  122. Console.WriteLine("Source: " + se.Source);
  123. Console.WriteLine("State: " + se.State);
  124. Console.WriteLine("Errors:");
  125. foreach(SqlError error in se.Errors) {
  126. Console.WriteLine(" SqlError:");
  127. Console.WriteLine(" Message: " + se.Message);
  128. Console.WriteLine(" Line Number: " + se.LineNumber);
  129. Console.WriteLine(" Procedure: " + se.Procedure);
  130. Console.WriteLine(" Class: " + se.Class);
  131. Console.WriteLine(" Number: " + se.Number);
  132. Console.WriteLine(" Server: " + se.Server);
  133. Console.WriteLine(" Source: " + se.Source);
  134. Console.WriteLine(" State: " + se.State);
  135. }
  136. Console.WriteLine("StackTrace: " + se.StackTrace);
  137. Console.WriteLine("TargetSite: " + se.TargetSite);
  138. Exception ie = se.InnerException;
  139. if(ie != null) {
  140. Console.WriteLine("InnerException:");
  141. Console.WriteLine(" Message: " + se.Message);
  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(" StackTrace: " + se.StackTrace);
  147. Console.WriteLine(" TargetSite: " + se.TargetSite);
  148. }
  149. return;
  150. }
  151. Console.WriteLine("Creating command...");
  152. IDbCommand cmd = con.CreateCommand();
  153. Console.WriteLine("set SQL...");
  154. cmd.CommandText = sql;
  155. Console.WriteLine("execute reader...");
  156. IDataReader reader = cmd.ExecuteReader();
  157. Console.WriteLine("read first row...");
  158. if(reader.Read()) {
  159. Console.WriteLine(" Value: " + reader[columnName].ToString());
  160. }
  161. else {
  162. Console.WriteLine(" No data returned. Or either, no permission to read data.");
  163. }
  164. Console.WriteLine("Clean up...");
  165. // clean up
  166. reader.Close();
  167. reader = null;
  168. cmd.Dispose();
  169. cmd = null;
  170. con.Close();
  171. con = null;
  172. Console.WriteLine("Done.");
  173. }
  174. }