public void UsingRawAdoWithDF() {
string dsKeyName = "NorthwindIBEntityManager";
//Extract EdmKey
EdmKey edmKey = GetEdmKey(dsKeyName);
//Create raw ADO.Net Db connection using EdmKey
System.Data.IDbConnection aConnection = CreateDbConnection(edmKey);
//Execute some Sql commands
ExecuteSqlCommand(aConnection);
}
private EdmKey GetEdmKey(string dsKeyName) {
var mgr = new NorthwindIBEntityManager();
EdmKey testEdmKey = EdmKey.LocateEdmKey(dsKeyName);
var dataSourceKey = mgr.DataSourceResolver.GetDataSourceKey(dsKeyName);
//If this is done on the client, it will return a ClientEdmKey
//If this is done on the server, it will return an EdmKey
//Ideally, you want to do this on the server
ClientEdmKey clientEdmKey = (ClientEdmKey)dataSourceKey;
EdmKey edmKey = new EdmKey(clientEdmKey);
return edmKey;
}
private System.Data.Common.DbConnection CreateDbConnection(EdmKey edmKey) {
var providerName = edmKey.DbProviderName;
var connectionString = edmKey.DbConnectionString;
var providerFactory = System.Data.Common.DbProviderFactories.GetFactory(providerName);
System.Data.Common.DbConnection connection = providerFactory.CreateConnection();
connection.ConnectionString = connectionString;
return connection;
}
private void ExecuteSqlCommand(System.Data.IDbConnection aConnection) {
const String sqlSelect = "select UserName from dbo.[User] where FirstName = 'Admin'";
const String sqlUpdate = "update dbo.[User] set UserName={0} where FirstName = 'Admin'";
aConnection.Open();
using (aConnection) {
System.Data.IDbCommand aCommand = CreateDbCommand(aConnection);
aCommand.CommandText = sqlSelect;
System.Data.IDataReader aDataReader = aCommand.ExecuteReader();
if (!aDataReader.Read()) {
throw new IdeaBladeException("Unable to locate record");
}
Object tmp = aDataReader.GetValue(0);
aDataReader.Close();
//Execute sqlUpdate
aCommand.CommandText = String.Format(sqlUpdate, "'adminxxx'");
int numOfRowsAffected = aCommand.ExecuteNonQuery();
}
}
private System.Data.Common.DbCommand CreateDbCommand(System.Data.IDbConnection connection) {
if (connection.State != System.Data.ConnectionState.Open) connection.Open();
System.Data.IDbCommand command = connection.CreateCommand();
try {
return (System.Data.Common.DbCommand)command;
} catch {
throw new IdeaBladeException("Unable to cast a DbCommand object from an IDbCommand for current connection");
}
}