本文演示了如何在C#中连接Oracle数据库。
string sqlSql = "SELECT * FROM A";
//从配置文件中获取连接字符串信息
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
//数据库读取对象
OracleDataReader reader = null;
try
{
//打开数据库连接
conn.Open();
OracleCommand comd = new OracleCommand(sqlSql, conn);
if (!string.IsNullOrEmpty("FIELD_A"))
{
sqlSql += string.Format(" AND FIELD_A =:FIELD_A");
//通过AddWithValue方法将参数设置到SQL语句中
comd.Parameters.AddWithValue("FIELD_A", "ABC");
}
comd.CommandText = sqlSql;
reader = comd.ExecuteReader();
while (reader.Read())
{
A.abc=reader.GetString(0);
}
catch (Exception e)
{
throw e;
}
finally
{
if (conn.State == ConnectionState.Open)
// 关闭连接
conn.Close();
}
}