RSS

Tag Archives: .NET

A workaround to use SQL IN clause in TableAdapters

This article is a simple implementation on how to use parameterized SQL query with IN clause or unknown number of parameters in TableAdapter.

When modifying those ancient database access codes, SQL query strings appear in the form of concatenated string very often. These codes are hard to read, maintain and prone to SQL Injection. Using DataSet Designer can easily improve it by accessing database with parameterized commands. But DataSet Designer does not support automatically generating commands with parameterized IN clause.

The idea of this article is originated from Extending TableAdapters for Dynamic SQL on The Code Project. The article demonstrate a way to change the command text programmatically by extending the TableAdapter which is defined as a partial class from the DataSet Designer. What’s not covered is how to parameterized the command.
Read the rest of this entry »

Advertisements
 
Leave a comment

Posted by on May 22, 2012 in .NET, ADO.NET, Oracle, SQL, VB

 

Tags: , , , , , ,

Dependency Injection

I’d like to refactor a simple code sample to demonstrate what dependency injection is. Here I have a MessageSender class to send messages. To send a TextMessage it needs a TextMessage instance.

public class MessageSender
{
    public void SendMessage()
    {
        TextMessage myTextMessage = new TextMessage();
        myTextMessage.Send();
    }
}

public class TextMessage
{
    public void Send()
    {
        // Implements Send()
    }
}

This way the MessageSender depends completely on TextMessage. What if there’s another type named ImageMessage to be sent? I’ll have to modify the SendMessage() method like this:

Read the rest of this entry »

 
Leave a comment

Posted by on April 17, 2012 in .NET, C#, Design Pattern

 

Tags: , , , ,

Why is DataGridView slow

I have an about 100 rows DataGridView which drops performance significantly since I wrote some code modifying its cells value programmatically. After looking up the MSDN post, the property AutoSizeColumnsMode is the reason why my DataGridView being slow. The property was set to AllCells, turns out it’ll auto resize cells when the value changes and that takes a lot of resource.

By 100 rows my code will run for about 1500 milliseconds. Changing the property to DisplayedCells can reduce it to 30 milliseconds and changing to ColumnHeader can reduce it to less than 8 milliseconds. It’ll cost no time if I commented the value changing code.

I guess every single cell value change will always auto resize according to the value of the AutoSize property, even when the property value is ColumnHeader.

 
Leave a comment

Posted by on March 13, 2012 in .NET, Winform

 

Tags: , ,