usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
namespaceDataProviders
{
///
///SqlDataProvider 的摘要说明。/// internal classSqlDataProvider: IDataProvider
{
privateSystem.Data.SqlClient.SqlConnectionsqlConnection;
privateSystem.Data.SqlClient.SqlCommandsqlCommand;
private stringconnectionString;
publicSqlDataProvider() : this(null)
{
//
// TODO: 在此处添加构造函数逻辑
//}
publicSqlDataProvider(stringconnectionString)
{
if(connectionString == null|| connectionString.Trim() == string.Empty)
{
System.Configuration.AppSettingsReaderconfigurationAppSettings = newSystem.Configuration.AppSettingsReader();
this.connectionString = (string)(configurationAppSettings.GetValue("sqlConnectionString", typeof(string)));
}
else{
this.connectionString = connectionString;
}
}
///
///SQL 连接字符串/// public stringConnectionString
{
get{
return this.connectionString;
}
set{
this.connectionString = value;
}
}
///
///返回一个带有连接字符串的SQL Connection.///
/// OracleConnectionprivateSqlConnectionGetSqlConnection()
{
try{
return newSqlConnection(this.connectionString);
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return null;
}
}
///
///对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1///
/// UPDATE、INSERT 和 DELETE 语句public intExecuteNonQuery(stringsql)
{
using(sqlConnection = this.GetSqlConnection())
{
if(sqlConnection == null)
return-1;
try{
if(sqlConnection.State == System.Data.ConnectionState.Closed)
sqlConnection.Open();
sqlCommand = newSqlCommand(sql, sqlConnection);
returnsqlCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return-1;
}
}
}
///
///执行查询,并将查询返回的结果集中第一行的第一列作为 .NET Framework 数据类型返回。忽略额外的列或行。///
/// SELECT 语句
/// .NET Framework 数据类型形式的结果集第一行的第一列;如果结果集为空或结果为 REF CURSOR,则为空引用public objectExecuteScalar(stringsql)
{
using(sqlConnection = this.GetSqlConnection())
{
if(sqlConnection == null)
return null;
try{
if(sqlConnection.State == System.Data.ConnectionState.Closed)
sqlConnection.Open();
sqlCommand = newSqlCommand(sql, sqlConnection);
returnsqlCommand.ExecuteScalar();
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return null;
}
}
}
///
///执行单Sql语句查询,并将查询返回的结果作为一个数据集返回///
/// SELECT 语句
/// 数据集 DataSetpublicDataSetRetriveDataSet(stringsql)
{
if(sql == null|| sql == string.Empty)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine("sql 为空");
#endif
return null;
}
using(sqlConnection = this.GetSqlConnection())
{
if(sqlConnection == null)
return null;
using(SqlDataAdapterda = newSqlDataAdapter(sql, sqlConnection))
{
DataSetds = newDataSet();
try{
da.Fill(ds);
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif}
returnds;
}
}
}
///
///执行Sql数组语句查询,并将查询返回的结果作为一个数据集返回///
/// Select 语句数组
/// TableName
/// 数据集 DataSetpublicDataSetRetriveDataSet(string[] sql, params string[] tableName)
{
intsqlLength;
sqlLength = sql.Length;
if( sqlLength == 0)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine("sql 为空");
#endif
return null;
}
using(sqlConnection = this.GetSqlConnection())
{
if(sqlConnection == null)
return null;
DataSetds = newDataSet();
inttableNameLength = tableName.Length;
for(inti = 0; i < sqlLength; i++)
{
using(SqlDataAdapterda = newSqlDataAdapter(sql[i], sqlConnection))
{
try{
if(i < tableNameLength)
da.Fill(ds, tableName[i]);
elseda.Fill(ds, "table"+ i);
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return null;
}
}
}
returnds;
}
}
publicDataSetUpdateDataSet(stringsql, DataSethasChangesDataSet)
{
return null;
}
public voidDispose()
{
this.connectionString = null;
this.sqlCommand.Dispose();
this.sqlConnection.Dispose();
}
}
}
OdbcDataProvider.cs 提供ODBC连接访问的类
#defineDEBUG
usingSystem;
usingSystem.Data;
usingSystem.Data.Odbc;
namespaceFenceKing.DataProviders
{
///
///OdbcDataProvider 的摘要说明。/// internal classOdbcDataProvider: IDataProvider
{
privateSystem.Data.Odbc.OdbcConnectionodbcConnection;
privateSystem.Data.Odbc.OdbcCommandodbcCommand;
private stringconnectionString;
publicOdbcDataProvider() : this(null)
{
//
// TODO: 在此处添加构造函数逻辑
//}
publicOdbcDataProvider(stringconnectionString)
{
if(connectionString == null|| connectionString.Trim() == string.Empty)
{
System.Configuration.AppSettingsReaderconfigurationAppSettings = newSystem.Configuration.AppSettingsReader();
this.connectionString = (string)(configurationAppSettings.GetValue("odbcConnectionString", typeof(string)));
}
else{
this.connectionString = connectionString;
}
}
///
///Oracle 连接字符串 "User Id=southfence;Data Source=FENCEORA;Password=southfence;Persist Security Info=true;"/// public stringConnectionString
{
get{
return this.connectionString;
}
set{
this.connectionString = value;
}
}
///
///返回一个带有连接字符串的Odbc Connection.///
/// OracleConnectionprivateOdbcConnection GetOdbcConnection()
{
try{
return newOdbcConnection(this.connectionString);
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return null;
}
}
///
///对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1///
/// UPDATE、INSERT 和 DELETE 语句public intExecuteNonQuery(stringsql)
{
using(odbcConnection = this.GetOdbcConnection())
{
if(odbcConnection == null)
return-1;
try{
if(odbcConnection.State == System.Data.ConnectionState.Closed)
odbcConnection.Open();
odbcCommand = newOdbcCommand(sql, odbcConnection);
returnodbcCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return-1;
}
}
}
///
///执行查询,并将查询返回的结果集中第一行的第一列作为 .NET Framework 数据类型返回。忽略额外的列或行。///
/// SELECT 语句
/// .NET Framework 数据类型形式的结果集第一行的第一列;如果结果集为空或结果为 REF CURSOR,则为空引用
public objectExecuteScalar(stringsql)
{
using(odbcConnection = this.GetOdbcConnection())
{
if(odbcConnection == null)
return null;
try{
if(odbcConnection.State == System.Data.ConnectionState.Closed)
odbcConnection.Open();
odbcCommand = newOdbcCommand(sql, odbcConnection);
returnodbcCommand.ExecuteScalar();
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return null;
}
}
}
///
///执行单Sql语句查询,并将查询返回的结果作为一个数据集返回///
/// SELECT 语句
/// 数据集 DataSetpublicDataSetRetriveDataSet(stringsql)
{
if(sql == null|| sql == string.Empty)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine("sql 为空");
#endif
return null;
}
using(odbcConnection = this.GetOdbcConnection())
{
if(odbcConnection == null)
return null;
using( OdbcDataAdapter da = newOdbcDataAdapter(sql, odbcConnection))
{
DataSetds = newDataSet();
try{
da.Fill(ds);
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif
return null;
}
returnds;
}
}
}
///
///执行Sql数组语句查询,并将查询返回的结果作为一个数据集返回///
/// Select 语句数组
/// TableName
/// 数据集 DataSetpublicDataSetRetriveDataSet(string[] sql, params string[] tableName)
{
intsqlLength;
sqlLength = sql.Length;
if( sqlLength == 0)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine("sql 为空");
#endif
return null;
}
using(odbcConnection = this.GetOdbcConnection())
{
if(odbcConnection == null)
return null;
DataSetds = newDataSet();
inttableNameLength = tableName.Length;
for(inti = 0; i < sqlLength; i++)
{
using(OdbcDataAdapter da = newOdbcDataAdapter(sql[i], odbcConnection))
{
try{
if(i < tableNameLength)
da.Fill(ds, tableName[i]);
elseda.Fill(ds, "table"+ i);
}
catch(Exceptionex)
{
#ifDEBUG
System.Diagnostics.Debug.WriteLine(ex.ToString());
#endif}
}
}
returnds;
}
}
publicDataSetUpdateDataSet(stringsql, DataSethasChangesDataSet)
{
return null;
}
public voidDispose()
{
this.connectionString = null;
this.odbcCommand.Dispose();
this.odbcConnection.Dispose();
}
}
}