using System; using System.Data; using System.Data.Common; using System.Data.SqlClient; namespace STMes.DataProvider { /// /// Implements access to the Data Provider for SQL Server. /// /// /// See the method to find an example. /// /// AddDataManager Method public class SqlDataProvider: IDataProvider { /// /// Creates the database connection object. /// /// /// See the method to find an example. /// /// AddDataManager Method /// The database connection object. IDbConnection IDataProvider.CreateConnectionObject() { return new SqlConnection(); } IDbConnection IDataProvider.CreateConnectionObject(string connectionString ) { SqlConnection conn = new SqlConnection(connectionString); return conn; } /// /// Creates the data adapter object. /// /// /// See the method to find an example. /// /// AddDataManager Method /// A data adapter object. DbDataAdapter IDataProvider.CreateDataAdapterObject() { return new SqlDataAdapter(); } /// /// Populates the specified IDbCommand object's Parameters collection with /// parameter information for the stored procedure specified in the IDbCommand. /// /// /// See the method to find an example. /// /// AddDataManager Method /// The IDbCommand referencing the stored procedure for which the parameter information is to be derived. The derived parameters will be populated into the Parameters of this command. void IDataProvider.DeriveParameters(IDbCommand command) { SqlCommandBuilder.DeriveParameters((SqlCommand)command); } /// /// Returns connection type. /// /// /// See the method to find an example. /// /// AddDataManager Method /// An instance of the class. public Type ConnectionType { get { return typeof(SqlConnection); } } /// /// Returns the data provider name. /// /// /// See the method to find an example. /// /// AddDataProvider Method /// Data provider name. public string Name { get { return "Sql"; } } /// /// /// /// public IDbDataParameter CreateDataParameter() { // TODO: Ìí¼Ó SqlDataProvider.CreateDataParameter ʵÏÖ return new SqlParameter(); } /// /// /// /// /// public Object CreateCommandBuilder(DbDataAdapter da) { return new SqlCommandBuilder(da as SqlDataAdapter) as object; } /// /// Test Connection Info(SQL SERVER) /// /// Test Connection Object /// Test Result public bool Ping(ref IDbConnection db) { SqlDataReader read = null; try { using(SqlCommand cmd = new SqlCommand("SELECT 1 FROM DUAL",(SqlConnection)db)) { read = cmd.ExecuteReader(); } return true; } catch(Exception ex) { Console.WriteLine(ex.Message); } finally { if(read!=null) read.Close(); } return false; } public int GetSID(ref IDbConnection db) { return -1; } } }