Thursday, March 5, 2009

ODBC on 64 bit machine (Windows/VISTA)?

As we all database developers know that a ODBC DSN is an entry that we created through "ODBC Data Source Administrator" that we reached from Start/Control Panel/AdministrativeTools or typing "odbcad32" from Start/Run .
On a 64bit machine when you run "ODBC Data Source Administrator" and created an ODBC DSN, actually you are creating an ODBC DSN which can be reachable by 64 bit applications only(You will not see most of drivers).

To run your 32bit application on a 64 bit machine, You need to run the 32bit version of "odbcad32.exe" by running "c:\Windows\SysWOW64\odbcad32.exe" from Start/Run menu and create your ODBC DSN with this tool.

The key point here to remember is a 64bit machine should be considered as a "64 bit Windows + 32 bit Windows" at the sametime. That's why the folder that our developers put the 32bit "odbcad32.exe" under C:\Windows\SysWOW64\ . The WOW abbreviation here means "Windows on Windows"

Look at the C:\Windows\SysWOW64\ for the EXE files at least to know what sort of other EXEs/DLLs are also compiled as 32bit and shipped within your 64bit OS.

For example cscript.exe and wscript.exe are ther for your WSH (Windows Scripting Host) scripts which needs to be run as 32bit. Think about a scenario like that. Only the 32 bit version of the OLE DB Provider or ODBC Driver exists for the data source that you need to get the data from your VBScript code. If it's the case, you'll need create the ODBC DSN (assuming that you're going to use ADO + ODBC in your VBScript code) from C:\Windows\SysWOW64\odbcad32.exe and also you should call your script as
"C:\Windows\SysWOW64\wscript.exe C:\scripts\myscript.vbs".
Doubleclicking the myscript.vbs from the Windows Explorer will result your script to be executed as
"C:\Windows\System32\wscript.exe C:\scripts\myscript.vbs".

Sunday, March 1, 2009

LINQ to SQL and multiple result sets in Stored Procedures

When you want to use stored procedure return multiple results you can't depend on auto generated class. You must do it manually, following steps below:
1. Create Entities for returned types:
example: we will create 2 entities ACCOUNT, ACCOUNT_USERS to LINQ to SQL Data Context
2. Create Partail class inherited from LINQ to SQL Data Context and add your method as below:
example for stored procedure will be test.

public partial class DataClasses1DataContext
{
[Function(Name = "dbo.Test")]
[ResultType(typeof(ACCOUNT))]
[ResultType(typeof(ACCOUNT_USER))]
public IMultipleResults Test([Parameter(DbType = "Int")] System.Nullable tmp)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), tmp);
return ((IMultipleResults)(result.ReturnValue));
}
}


3. Now you can use this method as below

DataClasses1DataContext c = new DataClasses1DataContext();
var r = c.Test(null); ;
ACCOUNT a = r.GetResult();
c.Dispose();