Gotcha with OleDbConnection and SQL Server 2000 databases

I was bug hunting today and ran across some weird behaviour with OleDbConnection. Before we go on, I’d like to point out that the code I was looking at was NOT mine (Who’d connect with an OleDbConnection, when there is a perfectly good SqlConnection?). Anyway, the thing I noticed was that when you have an OleDbConnection object connecting to a SQL Server 2000 database, with the Password parameter set to something. After a call to Open(), the connection string doesn’t have the Password parameter in it anymore.

Here’s some code I wrote to confirm it:

class Class1
{
  [STAThread]
  static void Main(string[] args)
  {
    OleDbConnection connection = new OleDbConnection(
"Provider=SQLOLEDB.1;Persist Security Info=False;Password=password;"+
"User ID=user;Initial Catalog=catalog;Data Source=COMPUTERNAME;" );
    Console.WriteLine("Before {0}",connection.ConnectionString );
    connection.Open();
    Console.WriteLine("After {0}",connection.ConnectionString);
    Console.ReadLine();
  }
}

And the output:

Before: Provider=SQLOLEDB.1;Persist Security Info=False;Password=password;User ID=user
;Initial Catalog=catalog;Data Source=COMPUTERNAME;
After: Provider=SQLOLEDB.1;Persist Security Info=False;User ID=user;
Initial Catalog=catalog;Data Source=COMPUTERNAME;

The names were changed to protect the innocent. To try it out, change Data Source to a real machine name, User ID to a real user id and Initial Catalog to a real db.

Why? Well, I can only speculate, but I’m guessing that it has to do with security, so no one can steal the information once the application has connected to the db. Could be the same for SqlConnection too. Anyone wanna try it out?

Regulator 2.0

Today at work, I required a regular expressions tester. I had heard about Regulator and I actually have it on my ever-expanding “to-do” list at home. Anyway, I downloaded it again and actually used it today. What a great tool! The interface at startup is a little confusing, but it’s really great for testing regular expressions on the fly. And then it can give you code once you’ve perfected it. No wonder it’s in the top 10 tools every .NET developer should have.

Reviewing the list, I have 7 downloaded, 6 installed(now), and until today, only used 3 with any frequency: NUnit, CodeSmith and Reflector.

Got any great programming tools that you can’t do without?

Auto increment in MS Access

This took me longer to find on Google than I wanted. If you want to have an auto-incrementing primary key in MS Access (sucks), then you want something like the following in the your create table statement:

CREATE TABLE tablename (
  id COUNTER NOT NULL CONSTRAINT constraintName_pk PRIMARY KEY,
  intColumn INTEGER,
  description MEMO
)

The primary key is ‘id’ and it’s of type COUNTER. Consider the above opposed to the SQL Server 2000 CREATE statement:

CREATE TABLE tablename (
  id int identity (1, 1) NOT NULL PRIMARY KEY,
  intColumn int,
  description varchar(2048)
)

Hopefully, with SQL Server 2005 Express coming out, MS Access (sucks) might go the way of the dodo in 10 years.