March 2005 - Posts

Discovering MIME types

I've been working on a little application to scratch an itch and I ran across the need to tell whether a certain file was an audio file or not. Rather than having a list of extensions (*.mp3, *.wma, ...) I decided to see if I could dig out the MIME type that Windows tracks.

Turns out (not surprisingly) that this info is stored in the registry in the HKEY_CLASSES_ROOT under the file extension. So if you know the extension you can figure out the MIME type.

Interestingly some extensions only have the Content Type value while others have both Content Type and PerceivedType.

Here is the routine I use to determine if a file is an audio type:

public bool IsAudio(FileInfo file)
{
	using (RegistryKey k = Registry.ClassesRoot.OpenSubKey(file.Extension))
	{
		if (k != null)
		{
			string value = (string)k.GetValue("PerceivedType");
			if (value != null)
			{
				return (value == "audio");
			}

			value = (string)k.GetValue("Content Type");
			if (value != null)
			{
				return (value.Split('/')[0] == "audio");
			}
		}
		return false;
	}
}

 

Posted by Wayne Allen | with no comments
Filed under:

Danger Quicksand - Have A Nice Day

David St Lawrence has sent his book "Danger Quicksand - Have A Nice Day" to the printers. You can pre-order the book here.

If your career is losing ground under the pressures and uncertainties of today's corporate employment, you need to read DANGER QUICKSAND - HAVE A NICE DAY.

This unconventional guide to surviving corporate employment is must reading for anyone who is waiting for the axe to fall, or who feels they are not getting the straight story from management.

This is the first career book written to level the playing field for employees who are trying to deal with the current realities of 21st century employment. You will laugh and curse as you see your current employment situation explained in all of its painful detail. Once you read this book, you will never drink the corporate Koolaid again.

Some of this will be uncomfortable reading for managers and top executives, because it is based on actual events, many of which are corroborated by numerous reader comments. Wherever you are in the organization, this book has something of value for you.

Danger Quicksand

 

Posted by Wayne Allen | 1 comment(s)
Filed under:

SqlConnection Connection String Parameter Keywords & Values

I can never find this info, so I'm filing it away.

Name

Default

Description

Application Name

 

The name of the application, or '.Net SqlClient Data Provider' if no application name is provided.

AttachDBFilename
-or-
extended properties
-or-
Initial File Name

 

The name of the primary file, including the full path name, of an attachable database. If this setting is specified, the Initial Catalog setting must also be specified.

The database name must be specified with the keyword 'database'.

Example:
Data Source=.; Initial Catalog=; Integrated Security=true;AttachDBFileName=c:\MyFolder\MyDb.mdf 

Update:

You can use the following syntax to attach a database that lives in your "Data" directory:

AttachDBFileName=|DataDirectory|MyDb.mdf 

Connect Timeout
-or-
Connection Timeout

15

The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.

Current Language

 

The SQL Server Language record name.

Data Source
-or-
Server
-or-
Address
-or-
Addr
-or-
Network Address

 

The name or network address of the instance of SQL Server to which to connect.

Encrypt

'false'

When true, SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed. Recognized values are true, false, yes, and no.

Initial Catalog
-or-
Database

 

The name of the database.

Integrated Security
-or-
Trusted_Connection

'false'

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.

Network Library
-or-
Net

'dbmssocn'

The network library used to establish a connection to an instance of SQL Server. Supported values include dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (Apple Talk), dbmsgnet (VIA), dbmslpcn (Shared Memory) and dbmsspxn (IPX/SPX), and dbmssocn (TCP/IP).

The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.

Packet Size

8192

Size in bytes of the network packets used to communicate with an instance of SQL Server.

Password
-or-
Pwd

 

The password for the SQL Server account logging on (Not recommended. To maintain a high level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).

Persist Security Info

'false'

When set to false or no (strongly recommended), security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password. Recognized values are true, false, yes, and no.

User ID

 

The SQL Server login account (Not recommended. To maintain a high level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).

Workstation ID

the local computer name

The name of the workstation connecting to SQL Server.

Connection Lifetime

0

When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online.

A value of zero (0) causes pooled connections to have the maximum connection timeout.

Connection Reset

'true'

Determines whether the database connection is reset when being drawn from the pool. For Microsoft SQL Server version 7.0, setting to false avoids making an additional server round trip when obtaining a connection, but you must be aware that the connection state, such as database context, is not being reset.

Enlist

'true'

When true, the pooler automatically enlists the connection in the creation thread's current transaction context. Recognized values are true, false, yes, and no.

Max Pool Size

100

The maximum number of connections allowed in the pool.

Min Pool Size

0

The minimum number of connections allowed in the pool.

Pooling

'true'

When true, the SQLConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. Recognized values are true, false, yes, and no.

Posted by Wayne Allen | 24 comment(s)
Filed under:

VS.NET Bug Followup - Or is True == 1

Allan Bogh left a comment on my previous post about the VS.NET debugger and quickwatch windows giving erroneous values:

Ahh, you miss the underlying meaning of 1 in o1 = 1. When you tell a C++/javascript/php/perl/every other high-level language that a variable (o1) is 1, you are only telling the system that it's true, not that it actually equals 1. When you tell two variables that they are "true" this, in no way, means that o1 == o2. If you figure that when you tell a variable that it ="a string", you are only giving the string an alias. Two different aliases with the same string are equal, but those two aliases by themself are not equal.

At the time he left the comment it was not clear that I was talking about C#, I've updated the post since then to reflect the specifics.

However, it is interesting to point out that in C# true and 1 are in fact not the same thing unlike it's ancestors. From the docs:

In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types.

In fact the following expression will not compile in C#:

if (true == 1) {}

I actually like this feature because to me true is different than non-zero.

Posted by Wayne Allen | 1 comment(s)
Filed under:

VS.NET Bug

One of my coworkers discovered the hard way today that in C# == is not the same as .Equals(). She had something like this:

object o1 = 1; object o2 = 1; Assert.IsTrue(o1 == o2);

This fails. The really nasty thing is that the Command Window and QuickWatch show that the expression evaluates to true!

So she changed it to:

object o1 = 1; object o2 = 1; Assert.IsTrue(o1.Equals(o2));

This succeeds! Why? From the docs:

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

So that means strings are special, so the following should work:

object o1 = "1"; object o2 = "1"; Assert.IsTrue(o1 == o2);

And it does.

Posted by Wayne Allen | 2 comment(s)
Filed under:
More Posts