瀏覽代碼

2005-01-16 Daniel Morgan <[email protected]>

	* ChangeLog: added file for oracle tests
	
	* testblob.cs: test loading a binary file into
	an Oracle BLOB column and then reading a BLOB column
	to a new binary file
	
	* testclob.cs: test loading a text file into
	an Oracle CLOB column and then reading a CLOB column
	to a new text file

svn path=/trunk/mcs/; revision=39001
Daniel Morgan 21 年之前
父節點
當前提交
bf27550aca

+ 13 - 0
mcs/class/System.Data.OracleClient/Test/ChangeLog

@@ -0,0 +1,13 @@
+2005-01-16  Daniel Morgan <[email protected]>
+
+	* ChangeLog: added file for oracle tests
+	
+	* testblob.cs: test loading a binary file into
+	an Oracle BLOB column and then reading a BLOB column
+	to a new binary file
+	
+	* testclob.cs: test loading a text file into
+	an Oracle CLOB column and then reading a CLOB column
+	to a new text file
+	
+	

+ 139 - 0
mcs/class/System.Data.OracleClient/Test/testblob.cs

@@ -0,0 +1,139 @@
+// testblob.cs - tests loading a binary file into an oracle blob and vice-versa
+using System;
+using System.Data;
+using System.Data.OracleClient;
+using System.Text;
+using System.IO;
+
+class TestBlob 
+{
+	static string infilename = @"mono-win32-setup-dark.bmp";
+	static string outfilename = @"mono-win32-setup-dark2.bmp";
+	static string connectionString = "Data Source=palis;User ID=scott;Password=tiger";
+
+	public static void Main (string[] args) 
+	{
+		OracleConnection con = new OracleConnection();
+		con.ConnectionString = connectionString;
+		con.Open();
+
+		BLOBTest (con);
+		ReadBlob (con);
+		
+		con.Close();
+		con = null;
+	}
+
+	// read the BLOB into file "cs-parser2.cs"
+	public static void ReadBlob (OracleConnection connection) 
+	{
+		if (File.Exists(outfilename) == true) {
+			Console.WriteLine("Filename already exists: " + outfilename);
+			return;
+		}
+
+		OracleCommand rcmd = connection.CreateCommand ();
+		rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
+		OracleDataReader reader2 = rcmd.ExecuteReader ();
+		if (!reader2.Read ())
+			Console.WriteLine ("ERROR: RECORD NOT FOUND");
+
+		Console.WriteLine ("  TESTING OracleLob OBJECT 2...");
+		OracleLob lob2 = reader2.GetOracleLob (0);
+		Console.WriteLine ("  LENGTH: {0}", lob2.Length);
+		Console.WriteLine ("  CHUNK SIZE: {0}", lob2.ChunkSize);
+
+		byte[] lobvalue = (byte[]) lob2.Value;
+
+		FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
+		BinaryWriter w = new BinaryWriter(fs);
+		w.Write(lobvalue);
+		w.Close();
+		fs.Close();
+
+		lob2.Close ();
+		reader2.Close ();
+	}
+
+	public static void BLOBTest (OracleConnection connection) 
+	{		
+		Console.WriteLine ("  BEGIN TRANSACTION ...");
+
+		OracleTransaction transaction = connection.BeginTransaction ();
+
+		Console.WriteLine ("  Drop table BLOBTEST ...");
+		try {
+			OracleCommand cmd2 = connection.CreateCommand ();
+			cmd2.Transaction = transaction;
+			cmd2.CommandText = "DROP TABLE BLOBTEST";
+			cmd2.ExecuteNonQuery ();
+		}
+		catch (OracleException oe1) {
+			// ignore if table already exists
+		}
+
+		Console.WriteLine ("  CREATE TABLE ...");
+
+		OracleCommand create = connection.CreateCommand ();
+		create.Transaction = transaction;
+		create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
+		create.ExecuteNonQuery ();
+
+		Console.WriteLine ("  INSERT RECORD ...");
+
+		OracleCommand insert = connection.CreateCommand ();
+		insert.Transaction = transaction;
+		insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
+		insert.ExecuteNonQuery ();
+
+		OracleCommand select = connection.CreateCommand ();
+		select.Transaction = transaction;
+		select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
+		Console.WriteLine ("  SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
+
+		OracleDataReader reader = select.ExecuteReader ();
+		if (!reader.Read ())
+			Console.WriteLine ("ERROR: RECORD NOT FOUND");
+
+		Console.WriteLine ("  TESTING OracleLob OBJECT ...");
+		OracleLob lob = reader.GetOracleLob (0);
+		Console.WriteLine ("  LENGTH: {0}", lob.Length);
+		Console.WriteLine ("  CHUNK SIZE: {0}", lob.ChunkSize);
+
+		try {
+			if (File.Exists(infilename) == false) {
+				Console.WriteLine("Filename does not exist: " + infilename);
+				return;
+			}
+
+			FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
+			BinaryReader r = new BinaryReader(fs);
+			
+			byte[] bytes = null;
+			int bufferLen = 8192;
+			bytes = r.ReadBytes (bufferLen);
+
+			while(bytes.Length > 0) {
+				Console.WriteLine("byte count: " + bytes.Length.ToString());
+				lob.Write (bytes, 0, bytes.Length);
+				if (bytes.Length < bufferLen)
+					break;
+				bytes = r.ReadBytes (bufferLen);
+			}
+
+			r.Close();
+			fs.Close ();	
+		}
+		catch (Exception e) {
+			Console.WriteLine("The file could not be read:");
+			Console.WriteLine(e.Message);
+		}
+
+		lob.Close ();
+
+		Console.WriteLine ("  CLOSING READER...");
+			
+		reader.Close ();
+		transaction.Commit ();
+	}
+}

+ 118 - 0
mcs/class/System.Data.OracleClient/Test/testclob.cs

@@ -0,0 +1,118 @@
+// testclob.cs - tests loading a text file into an oracle clob and vice-versa
+using System;
+using System.Data;
+using System.Data.OracleClient;
+using System.Text;
+using System.IO;
+
+class TestClob 
+{
+	static string infilename = @"cs-parser.cs";
+	static string outfilename = @"cs-parser2.cs";
+	static string connectionString = "data source=palis;user id=scott;password=tiger"
+
+	public static void Main (string[] args) 
+	{
+		OracleConnection con = new OracleConnection();
+		con.ConnectionString = connectionString;
+		con.Open();
+		
+		CLOBTest (con);
+		ReadClob (con);
+		
+		con.Close();
+		con = null;
+	}
+
+	// read the CLOB into file "cs-parser2.cs"
+	public static void ReadClob (OracleConnection connection) 
+	{
+		OracleCommand rcmd = connection.CreateCommand ();
+		rcmd.CommandText = "SELECT CLOB_COLUMN FROM CLOBTEST";
+		OracleDataReader reader2 = rcmd.ExecuteReader ();
+		if (!reader2.Read ())
+			Console.WriteLine ("ERROR: RECORD NOT FOUND");
+
+		Console.WriteLine ("  TESTING OracleLob OBJECT 2...");
+		OracleLob lob2 = reader2.GetOracleLob (0);
+		Console.WriteLine ("  LENGTH: {0}", lob2.Length);
+		Console.WriteLine ("  CHUNK SIZE: {0}", lob2.ChunkSize);
+
+		string lobvalue = (string) lob2.Value;
+		
+		using (StreamWriter sw = new StreamWriter(outfilename)) {
+			sw.Write(lobvalue);
+		}
+
+		lob2.Close ();
+		reader2.Close ();
+
+	}
+
+	public static void CLOBTest (OracleConnection connection) 
+	{		
+		Console.WriteLine ("  BEGIN TRANSACTION ...");
+
+		OracleTransaction transaction = connection.BeginTransaction ();
+
+		Console.WriteLine ("  Drop table CLOBTEST ...");
+		try {
+			OracleCommand cmd2 = connection.CreateCommand ();
+			cmd2.Transaction = transaction;
+			cmd2.CommandText = "DROP TABLE CLOBTEST";
+			cmd2.ExecuteNonQuery ();
+		}
+		catch (OracleException oe1) {
+			// ignore if table already exists
+		}
+
+		Console.WriteLine ("  CREATE TABLE ...");
+
+		OracleCommand create = connection.CreateCommand ();
+		create.Transaction = transaction;
+		create.CommandText = "CREATE TABLE CLOBTEST (CLOB_COLUMN CLOB)";
+		create.ExecuteNonQuery ();
+
+		Console.WriteLine ("  INSERT RECORD ...");
+
+		OracleCommand insert = connection.CreateCommand ();
+		insert.Transaction = transaction;
+		insert.CommandText = "INSERT INTO CLOBTEST VALUES (EMPTY_CLOB())";
+		insert.ExecuteNonQuery ();
+
+		OracleCommand select = connection.CreateCommand ();
+		select.Transaction = transaction;
+		select.CommandText = "SELECT CLOB_COLUMN FROM CLOBTEST FOR UPDATE";
+		Console.WriteLine ("  SELECTING A CLOB (CHARACTER) VALUE FROM CLOBTEST");
+
+		OracleDataReader reader = select.ExecuteReader ();
+		if (!reader.Read ())
+			Console.WriteLine ("ERROR: RECORD NOT FOUND");
+
+		Console.WriteLine ("  TESTING OracleLob OBJECT ...");
+		OracleLob lob = reader.GetOracleLob (0);
+		Console.WriteLine ("  LENGTH: {0}", lob.Length);
+		Console.WriteLine ("  CHUNK SIZE: {0}", lob.ChunkSize);
+
+		UnicodeEncoding encoding = new UnicodeEncoding ();
+
+		try {
+			// read file "cs-parser.cs" into the oracle clob
+			using (StreamReader sr = new StreamReader(infilename)) {
+				string sbuff = sr.ReadToEnd ();
+				byte[] evalue = encoding.GetBytes (sbuff);
+				lob.Write (evalue, 0, evalue.Length);
+			}
+		}
+		catch (Exception e) {
+			Console.WriteLine("The file could not be read:");
+			Console.WriteLine(e.Message);
+		}
+		lob.Close ();
+
+		Console.WriteLine ("  CLOSING READER...");
+			
+		reader.Close ();
+		transaction.Commit ();
+	}
+}