Sunday, September 12, 2010

Database, Using Stored Procedures


Problem

You need to use a stored procedure in your database, and you're not sure how to specify values for its input and output parameters.

Solution

Use the command object's Parameters collection to set and retrieve stored procedure argument values.

Discussion

Here's a simple SQL Server stored procedure that does nothing more than retrieve a field from a table given its ID value:
CREATE PROCEDURE GetRecordName
    @PriKey int,
    @NameResult varchar(50) OUT
 AS
 BEGIN
    -- Given an ID value, return the RecordName field.
    SET @NameResult =
       (SELECT RecordName FROM Table1 WHERE ID = @PriKey);
 END

Monday, September 6, 2010

Format the DataGridView

Formatting the .NET 1.x DataGrid ranges from awkward to nearly impossible. However, thanks to its multi-layered model, formatting the DataGridView is far easier. This model builds on a single class, the DataGridViewCellStyle, which encapsulates key formatting properties. You can assign different DataGridViewCellStyle objects to separate rows, columns, or even distinct cells.


Note: By using a few simple style properties, you can configure the appearance of the entire grid, individual columns, or rows with important data.



How do I do that?

The DataGridView already looks better than the DataGrid in its default state. For example, you'll notice that the column headers have a modern, flat look and become highlighted when the user moves the mouse over them. However, there's much more you can do with the help of the DataGridViewCellStyle class.
The DataGridViewCellStyle collects all the formatting properties of the DataGridView. It defines appearance-related settings (e.g., color, font), and data formatting (e.g., currency, date formats). All in all, the DataGridViewCellStyle provides the following key properties:

Thursday, September 11, 2008

Issuing SQL Commands

Problem

Now that you've established a connection to a database through a provider, you're ready to issue SQL commands. But how?

Solution
Use a Command object to issue SQL commands directly to your database through the provider connection.

Discussion
The following code updates a SQL Server table named Table1, changing every Column2 field to 25 whenever Column1 has a value of 0:

' ----- Connect to the database.
Dim connectionString As String = _
"Data Source=MySystem\SQLEXPRESS;" & _
"Initial Catalog=MyDatabase;Integrated Security=true"
Dim theDatabase As New SqlClient.SqlConnection(connectionString)
theDatabase.Open()

' ----- Prepare the SQL statement for use.
Dim sqlStatement As New SqlClient.SqlCommand( _
"UPDATE Table1 SET Column2 = 25 WHERE Column1 = 0", _
theDatabase)
sqlStatement.ExecuteNonQuery()

' ----- Clean up.
theDatabase.Close()
theDatabase.Dispose()


Connecting to a Data Provider

Problem
You are writing an application that interacts with a database, and you need to connect to it to run some queries.

Solution
Use a Connection object and a "connection string" to establish the connection you will use for queries and updates.

Discussion
The following set of statements establishes a connection to a SQL Server Express database named MyDatabase running on the system named MySystem, using the active Microsoft Windows login account for its security access:

Dim theDatabase As System.Data.SqlClient.SqlConnection
Dim connectionString As String = _
"Data Source=MySystem\SQLEXPRESS;" & _
"Initial Catalog=MyDatabase;Integrated Security=true"

theDatabase = New SqlClient.SqlConnection(connectionString)
theDatabase.Open( )
' ---- Perform database processing here, then…
theDatabase.Close( )
theDatabase.Dispose( )


Thursday, September 4, 2008

andLinux, run Linux applications in Windows

For those who like to run Linux applications but still use a PC that had Windows OS, now do not need to go back and forth reboot to run a dual boot or run a virtualization program such a Virtual PC or Virtual Box. With andLinux now we can run linux applications in windows as if the applications are Windows applications.
andLinux uses the Ubuntu Linux distribution that runs in a seamless environment in Windows 2000 (2000, XP, 2003, Vista, 32-bit version only). and Linux using coLinux as corenya. coLinux kernel is ported into Windows.This technology is mentioned more than a virtual machine because Windows and coLinux kernel merge and do not emulate a PC, making it more efficient.