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 />

}<br />

}<br />

finally<br />

{<br />

// Clean up<br />

if (rsMyRS != null)<br />

{<br />

rsMyRS.Close();<br />

}<br />

}<br />

if (cnMyConn != null)<br />

{<br />

cnMyConn.Close();<br />

}<br />

Executing Commands with No Data Set<br />

652<br />

using System;<br />

using System.Data.SqlClient;<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

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

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

// Integrated Security - next line should be uncommented to use<br />

string strConnect = “Data Source=(local);Initial Catalog=master;Integrated<br />

Security=SSPI”;<br />

// <strong>SQL</strong> Security - next line should be uncommented to use<br />

//string strConnect = “Data Source=(local);Initial Catalog=master;User<br />

Id=sa;Password=MyPass”;<br />

string strCommand = “CREATE TABLE Foo(Column1 INT NOT NULL PRIMARY KEY)”;<br />

string strCommand2 = “DROP TABLE Foo”;<br />

SqlConnection cnMyConn = new SqlConnection(strConnect);<br />

try<br />

{<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 />

SqlCommand sqlMyCommand = new SqlCommand(strCommand, cnMyConn);<br />

// Execute the command<br />

sqlMyCommand.ExecuteNonQuery();<br />

Console.WriteLine(“Table Created”);<br />

Console.WriteLine(“Press enter to continue (you can go check to make<br />

sure that it’s there first) “);

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

Saved successfully!

Ooh no, something went wrong!