SQL Command

June 30, 2009 at 4:42 pm (ADO.Net) ()

Example of SqlCommand…
Is necessary to inform the connection and the command that you want to execute.

::[Visual Basic]
Public Function ReadFromDB(ByVal connectionString As String) as SqlDataReader
Dim commandText As String = “SELECT * FROM dbo.Table”
Dim connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(commandText, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
connection.Close()
return reader
End Function

::[C#]
private static SqlDataReader ReadFromDB(string connectionString)
{
string commandText = “SELECT * FROM dbo.Table”;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(commandText, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
connection.Close();
return reader;
}

Permalink Leave a Comment

SQL Connection

June 24, 2009 at 7:09 pm (ADO.Net)

SQL Connection

The SqlConnection (.NET Framework) object provides connectivity to Microsoft SQL Server version 7.0 or later and supports a connection string format that is similar, but is not identical, to the OLE DB (ADO) connection string format.

The following code example demonstrates how to create and open a connection to a SQL Server (version 7.0 or later) database.

::[Visual Basic]

Dim Conn As SqlConnection = New SqlConnection(“Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind”)
Conn.Open()

::[C#]
SqlConnection Conn = new SqlConnection(“Data Source=localhost; Integrated Security=SSPI;Initial Catalog=northwind”);
Conn.Open();

It is nice that you always close the Connection after using it. This can be done with the Close or Dispose methods of the Connection object.

Permalink Leave a Comment

Everything has a beginning …

June 22, 2009 at 4:44 pm (General)

What’s up!

Starting my blog …

I’ll include some .Net tips, basically the victories of the bizarre problems that we developers spend …

Permalink Leave a Comment