SafeMapping

类别:.NET开发 点击:0 评论:0 推荐:
CREATE TABLE empInfo (
empno NUMBER(4) PRIMARY KEY,
empName VARCHAR2(20) NOT NULL,
hiredate DATE,
salary NUMBER(7,2),
jobDescription Clob,
byteCodes BLOB
);
===================================
// C#
// The following example shows how to use the SafeMapping property to fill the dataset
public static void UseSafeMapping(
string connStr)
{

//In this SELECT statement, EMPNO, HIREDATE and SALARY must be
//preserved using safe type mapping.
string cmdStr = "SELECT EMPNO, EMPNAME, HIREDATE, SALARY FROM EMPINFO";

//create the adapter with the selectCommand txt and the connection string
OracleDataAdapter adapter = new OracleDataAdapter(cmdStr, connStr);

//get the connection from the adapter
OracleConnection connection = adapter.SelectCommand.Connection;

//create the safe type mapping for the adapter
//which can safely map column data to byte arrays, where
// applicable. By executing the following statement, EMPNO, HIREDATE AND
//SALARY columns will be mapped to byte[]
adapter.SafeMapping.Add("*", typeof(byte[]));

//Map HIREDATE to a string
//If the column name in the EMPINFO table is case-sensitive,
//the safe type mapping column name must be case-sensitive.
adapter.SafeMapping.Add("HIREDATE", typeof(string));

//Map EMPNO to a string
//If the column name in the EMPINFO table is case-sensitive,
//the safe type mapping column name must also be case-sensitive.
adapter.SafeMapping.Add("EMPNO", typeof(string));

//Create and fill the DataSet using the EMPINFO
DataSet dataset = new DataSet();
adapter.Fill(dataset, "EMPINFO");

//Get the EMPINFO table from the dataset
DataTable table = dataset.Tables["EMPINFO"];

//Get the first row from the EMPINFO table
DataRow row0 = table.Rows[0];

//Print out the row info
Console.WriteLine("EMPNO Column: type = " + row0["EMPNO"].GetType() +
"; value = " + row0["EMPNO"]);
Console.WriteLine("EMPNAME Column: type = " + row0["EMPNAME"].GetType() +
"; value = " + row0["EMPNAME"]);
Console.WriteLine("HIREDATE Column: type = " + row0["HIREDATE"].GetType()+
"; value = " + row0["HIREDATE"]);
Console.WriteLine("SALARY Column: type = " + row0["SALARY"].GetType() +
"; value = " + row0["SALARY"]);
}

Output:
EMPNO Column: type = System.String; value = 1
EMPNAME Column: type = System.String; value = KING
HIREDATE Column: type = System.String; value = 01-MAY-81
SALARY Column: type = System.Byte[]; value = System.Byte[]

本文地址:http://com.8s8s.com/it/it42775.htm