17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix B: Very Simple Connectivity Examples<br />

End Sub<br />

End Module<br />

Dim rsMyRS As SqlClient.SqlDataReader<br />

Dim cnMyConn As New SqlClient.SqlConnection(strConnect)<br />

‘ “Open” the connection (this is the first time it actually<br />

‘ contacts the database server)<br />

cnMyConn.Open()<br />

‘ Create the command object now<br />

Dim sqlMyCommand As New SqlClient.SqlCommand(strCommand, cnMyConn)<br />

‘ Create the result set<br />

rsMyRS = sqlMyCommand.ExecuteReader()<br />

‘Output what we got<br />

Do While rsMyRS.Read<br />

‘ Write out the first (ordinal numbers here)<br />

‘ column. We can also refer to the column by name<br />

Console.WriteLine(rsMyRS(“Name”))<br />

Loop<br />

Console.WriteLine()<br />

Console.WriteLine(“Press any key to continue...”)<br />

Console.ReadKey()<br />

‘ Clean up<br />

rsMyRS.Close()<br />

cnMyConn.Close()<br />

Executing Commands with No Data Set<br />

654<br />

Imports System<br />

Imports System.Data<br />

Imports System.Data.SqlClient<br />

Module Program<br />

Sub Main()<br />

‘Create some base strings so you can look at these<br />

‘separately from the commands they run in<br />

‘ Integrated Security - next 2 lines should be uncommented to use<br />

Dim strConnect As String = _<br />

“Data Source=(local);Initial Catalog=master;Integrated Security=SSPI”<br />

‘ <strong>SQL</strong> Security - next 2 lines should be uncommented to use<br />

‘Dim strConnect As String = _<br />

‘ “Data Source=(local);Initial Catalog=master;User Id=sa;Password=MyPass”<br />

Dim strCommand As String = “CREATE TABLE Foo(Column1 INT NOT NULL PRIMARY KEY)”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!