日期:2011-05-04  浏览次数:20711 次

在编写有关数据库方面的C#程序时,经常需要知道数据库的表中各字段的以下信息:
  1. 用于OracleParameter(或SqlParameter,...)中的字段和属性的数据库特定的数据类型。
  2. 其对应的.NET数据类型。
  如下面的程序片断所示:


using (OracleConnection conn = new OracleConnection(Pub.ConnString))
{
conn.Open();
OracleCommand comm = new OracleCommand(
"SELECT trdate,txcode,drcrf,amount,balance,tellerno,txnote,zoneno,nodeno FROM detail "+
"WHERE accno=:accno AND currtype=:currtype ORDER BY accno,currtype,trdate,seqno", conn);
comm.Parameters.Add("accno", OracleDbType.Int64).Value = long.Parse(acc.Substring(4,13));
comm.Parameters.Add("currtype", OracleDbType.Int16).Value = curr;
using (OracleDataReader r = comm.ExecuteReader())
{
for (cnt = 0; r.Read(); cnt++)
{
DataRow dr = dt.NewRow();
dr["TrDate"] = r.GetDateTime(0);
dr["Txcode"] = r.GetInt32(1);
dr["Drcrf"] = IcbcEtc.GetDrcrfString(r.GetInt16(2));
dr["Amount"] = r.GetInt64(3) / R;
dr["Balance"] = r.GetInt64(4) / R;
dr["Tellerno"] = r.GetInt32(5);
dr["TxNote"] = r.GetString(6);
dr["Zoneno"] = r.GetInt32(7);
dr["Nodeno"] = r.GetInt32(8);
dr["Txname"] = DbTrxCode.GetNewName((int)dr["Txcode"]);
dt.Rows.Add(dr);
}
}
}

  为此,我编写了一个小工具,其应用示例如下:



  这里是源程序(ODP.NET版),需要下载“Oracle Data Provider for .NET”,其命名空间是: Oracle.DataAccess.Client。

usingSystem;
usingSystem.Data;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Drawing;
usingOracle.DataAccess.Client;

namespaceSkyiv.Util.Odpnet