Configure Enterprise Library in code - configuration

Is it possible to configure the Data Application Block in Enterprise Library entirely in code?
Instead of having the big messy config file.

Ok, after a bit of googeling and some trial and error I've come up with this solution that works pretty good. It uses the System.Data.SQLClient provider.
Just supply a connectionstring:
Dim databaseSettings As Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings
Dim connStringSection As System.Configuration.ConnectionStringsSection = New System.Configuration.ConnectionStringsSection()
Dim dictDataSource As Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource
Dim dbProvider As Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping
Dim dbFactory As DatabaseProviderFactory
Dim database As Microsoft.Practices.EnterpriseLibrary.Data.Database
databaseSettings = New Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings()
connStringSection = New System.Configuration.ConnectionStringsSection()
dictDataSource = New Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource()
dbProvider = New Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping.DefaultSqlProviderName, GetType(Sql.SqlDatabase))
connStringSection.ConnectionStrings.Add(New System.Configuration.ConnectionStringSettings("DBConnectionString", connectionString, "System.Data.SqlClient"))
databaseSettings.ProviderMappings.Add(dbProvider)
databaseSettings.DefaultDatabase = "DBConnectionString"
'Add Database Settings to Dictionary
dictDataSource.Add(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings.SectionName, databaseSettings)
'Add Connection String to Dictionary
dictDataSource.Add("connectionStrings", connStringSection)
dbFactory = New DatabaseProviderFactory(dictDataSource)
database = dbFactory.Create("DBConnectionString")
database.CreateConnection()
Return database

short answer: yes.
Long answer: Why would you want to do that? The whole idea of the "big messy" file is that you write somewhat generic code that uses interfaces and then configure the specifics. Want to switch from ORacle to SQL Server? I've done it just by updating a config variable. Yes it's a bear porting stored procs, but it works.

Related

LotusScript - Is there a way to send an attachment(file) using ODBC to MySQL?

I am using some legacy project and I need to export some files from my Lotus Notes database to MySQL DB using ODBC connection.
I have a ~94000 documents in lotus database with some small attachments (30-40kb).
As always, for this tasks I was always using some kind of this:
Dim mysqlConnection As New ODBCConnection
Dim sqlQuery As New ODBCQuery
Dim result As New ODBCResultSet
Dim notesSession As New NotesSession
Set ntsDatabase = notesSession.CurrentDatabase
Call mysqlConnection.ConnectTo("DSN_NAME","NAME","PASS")
And I was not having problems with sending/parsing some data with queries like this:
Set sqlQuery.Connection = mysqlConnection
Set result.Query = sqlQuery
sqlQuery.SQL = some query e.t.c.
Everything is working fine. But now I am trying to find a way to send files to MySQL database and having some real problems to find the solution.
Can you please give some small example with sending a small blob file to MySQL or some kind of advise to solve this?
Thanks!
I don't think an example like that could be considered "small".
You're going to have to extract the attachment to a file, read the file into NotesStream, convert the bytes in the NotesStream into a Base64 string, and send that string value in a SQL command.

Classic ASP global.asa SQL Server 2008 connection string

I have been given a web application written in Classic ASP to port from Windows 2003 Server (SQL Server 2000 and IIS 6) to Windows 2008 Server (SQL Server 2008 and IIS 7.5).
The site uses a GLOBAL.ASA file to define global variables, one of which is the connection string (cnn) to connect to SQL Server.
Below is the (old) connection string from GLOBAL.ASA:
Sub Application_OnStart
Dim cnnDem, cnnString
Set cnnDem = Server.CreateObject("ADODB.Connection")
cnnDem.CommandTimeout = 60
cnnDem.Mode = admodeshareexclusive
cnnString = "Provider=SQLOLEDB; Data Source=192.xxx.x.xx; User Id=xxxx; Password=xxxxx; default catalog=xxxxxxx;"
Application("conString")=cnnString
Call cnnDem.Open(cnnString)
Application("cnn") = cnnDem
End Sub
The .ASP pages then use the cnn value like this:
strSQL = "Select * From tblUtilities order by companyname"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, Application("cnn"), adOpenKeyset
However I could not get the connection string to connect – I whittled it down to a “Failed to Login” error message (no matter what Login ID I tried).
I edited the GLOBAL.ASA file as follows and it works.
Sub Application_OnStart
Dim cnnDem, cnnString
Set cnnDem = Server.CreateObject("ADODB.Connection")
cnnDem.CommandTimeout = 60
cnnString = "Provider=SQLNCLI10.1;User Id=xxxx; Password=xxxxx;Initial Catalog=xxxxxxx;Data Source=xxxxxx\SQLEXPRESS;"
Application("conString")=cnnString
Application("cnn")=cnnString
Call cnnDem.Open(cnnString)
End Sub
The main difference is that cnn now contains the connection string, where as previously cnn was an object referring to ADOBD.Connection.
The question I have is what impact (if any) will this have on the application. I have done some basic (local) testing and everything looks ok at the moment. But I am wondering if there might be multi-user issues (or something of that nature) that might arise when this site is deployed again.
One of the best and easiest way to connect to create a Database Connection String is to crease a new ASP file in the root directory or elsewhere and include the Connection string into it:
//Global.asp //
<%
Dim connectionString
connectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=YourSQLServer;UID=sa;PWD=*******;DATABASE=YourDataBase"
%>
Then create an include statement in each file that you would like to call this connection.
<!-- #include virtual="global.asp" -->
Then, where you need to setup your connection call, simply use your code to connect to the Database:
<%
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open = ConnectionString
Set rsReports = Server.CreateObject("ADODB.Recordset")
strSQL = "Select * From Customers"
rsReports.Open strSQL, adoCon
%>
I keep the Connection String in Global.asa but create the connection in a separate function loaded as needed. An Application connection object may not be aware of temporary network issues that may close that connection, and then future attempts to use the connection will not be successful.
Hope this makes sense.

How to stream resultset with MySQL .net connector for large dataset

I need to retrieve a large dataset from a MySQL server for processing.
I am using the MySQL .net connector for this, as the application that is consuming the data is written in F#. The dataset is much too large to fit into memory, so I want avoid holding the raw data in memory altogether and operate on it as it streams into the application from the database server.
I've read that this can be done by using the ResultSet properties in the JDBC API a la Streaming large result sets with MySQL and http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html, but I haven't found something similar for the .Net API.
So far my search has been primarily through the MySQL documentation at http://dev.mysql.com/doc/connector-net/en/connector-net-programming.html, but this hasn't turned up anything (at least, nothing obvious to me).
How might I accomplish what I'm looking to do?
Update:
Some details about my limitations.
I only have read access to the data source, as it is managed by a third party, so making any changes server-side is a no go.
Here's what the query I'm pushing to the server looks like:
SELECT
SID.dwsi_store AS 'store'
,SID.dwsi_transaction_date AS 'transactionDate'
,SID.dwsi_transaction AS 'transaction'
,SID.dwsi_item AS 'sku'
,C.dwcl_class AS 'clss'
,D.dwde_department AS 'department'
FROM
dw_sls_item_dtl SID
JOIN
dw_item I
JOIN
dw_class C
JOIN
dw_department D
ON
SID.dwsi_store = I.dwin_store
AND SID.dwsi_item = I.dwin_item_number
AND I.dwin_store = C.dwcl_store
AND I.dwin_class = C.dwcl_class
AND I.dwin_store = D.dwde_store_number
AND I.dwin_department = D.dwde_department
WHERE
SID.dwsi_transaction_date >= '2007-03-01'
AND SID.dwsi_store BETWEEN '2' AND '8'
AND NOT C.dwcl_class = ''
AND NOT C.dwcl_class_name LIKE('%CCL''d%')
AND D.dwde_department BETWEEN '10' AND '92'
While I could manually page through the data by parameterizing the where clause and retrieving the data one date at a time, I would prefer not to do this if the MySQL connector has a more elegant solution available.
Update
Here's the code that is calling the database server. It is using the ExecuteReader method which returns a DbDataReader. Now that I think of it, the problem is probably that I'm reading everything into a sequence without operating on it. It appears this issue is related to how I'm implementing the reader, not on the reader itself.
use cn = new MySqlConnection(cs)
use cmd = new MySqlCommand(sql,cn)
cmd.CommandType = CommandType.Text |> ignore
cn.Open()
use reader = cmd.ExecuteReader()
while reader.Read() do
yield { store = unbox (reader.["store"])
transactionDate = unbox (reader.["transactionDate"])
transaction = unbox (reader.["transaction"])
sku = unbox (reader.["sku"])
clss = unbox(reader.["clss"])
department = unbox(reader.["department"])} }
I think you are looking for a derived class of DbDataReader; likely OdbcDataReader or OleDbDataReader. These classes give forward-only access to a result set. See the links below for more information:
http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx ("The DataReader is a good choice when retrieving large amounts of data because the data is not cached in memory.")
http://msdn.microsoft.com/en-us/library/system.data.common.dbdatareader(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcdatareader(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader(v=vs.110).aspx

Why Doesn't The EntityConnection Object Contain the Login Password?

I've got an app that uses EF CTP5.
In this particular situation, i need to degrade to some classic ADO.NET (in order to read multiple result sets in a stored procedure, which EF does not support).
As such, im trying to use the existing connection string from the EntityConnection object, like this:
var ctx = (this as IObjectContextAdapter).ObjectContext;
var efCon = ((EntityConnection) (ctx.Connection)).StoreConnection;
var con = new SqlConnection(efCon.ConnectionString);
con.Open(); // exception thrown
When i debug, i see that the ConnectionString does not contain the password, only the data source, username, database, etc.
Is this a security thing why they've removed it? Does EF hide the password somewhere and only use it when it executes stored procedures itself?
The EF connection string is not like classic ADO.NET connection strings, as it has metadata info.
So it looks like im going to have to strip out the part of the connection string that i need, put that in the web.config and pass it through to the Repository.
Surely there must be a better way!
Try adding "Persist Security Info=True;" to the context connection string. This worked for me.

Trouble resetting connection string in Access 2010

New to Access 2010. The following VBA code when run doesn't reset the connection string as expected. I'm pretty sure this used to work. What's wrong?
CurrentDb.TableDefs("AccountNumber").Connect = "ODBC;Description=NativeClient;DRIVER=SQL Native Client;SERVER=server1;DATABASE=Expense;Trusted_Connection=Yes"
CurrentDb.TableDefs("AccountNumber").RefreshLink
I am not sure about that connection string. Which version of SQL Server are you using? You might like to try a connection string from http://www.connectionstrings.com/sql-server-2008#p3, for example, this works for me.
scn = "Driver={SQL Server Native Client 10.0};" & _
"Server=ServerName; Database=test;Trusted_Connection=yes;"
With CurrentDb
.TableDefs(sLocalName).Connect = scn
.TableDefs(sLocalName).RefreshLink
End with