Beispiel #1
0
	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: "******"SELECT " + columnName + " FROM " + tableName;
		
		constr = 
			"Server=" + server + ";" + 
			"Database=" + database + ";" +
			"User ID=" + userid + ";" +
			"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.");
	}
Beispiel #2
0
		static void Main(string[] args) 
		{
			string connectionString = "";
						
			if(args.Length == 3 || args.Length == 4) {
				if(args.Length == 3) {
					connectionString = String.Format(
						"Server={0};" + 
						"Database={1};" +
						"User ID={2};",
						args[0], args[1], args[2]);
				}
				else if(args.Length == 4) {
					connectionString = String.Format(
						"Server={0};" + 
						"Database={1};" +
						"User ID={2};" +
						"Password={3}",
						args[0], args[1], args[2], args[3]);
				}
			}
			else {
				Console.WriteLine("Usage: mono SybaseTest.exe sql_server database user_id password");
				return;
			}

			SybaseConnection cnc = new SybaseConnection ();
			cnc.ConnectionString =  connectionString;

			cnc.Open();
			DoSybaseTest(cnc);
			cnc.Close();
		}