2010/03/25

C# week9_1

22/03/2010

 
 

ADO.NET

 
 

//Insert, Update, Select SQL command

 
 

namespace ConsoleApplication

{

class Program

{

static SqlConnection conn = null;

static void Main(string[] args)

{

SqlDataReader rdr;

try

{

conn = new SqlConnection("Data Source = Server ; Initial Catalog = DBname; Integrated Security = SSPI");

 
 

conn.Open();

SqlCommand cmd = new SqlCommand("INSERT INTO CATEGORY VALUES (12, 'Orange')", conn);

cmd.ExecuteNonQuery();

 
 

SqlCommand cmd1 = new SqlCommand("UPDATE CATEGORY SET CategoryName='Other' WHERE CategoryID=9", conn);

cmd1.ExecuteNonQuery();

 
 

 
 

SqlCommand cmd2 = new SqlCommand("SELECT * FROM CATEGORY", conn);

rdr = cmd2.ExecuteReader();

while (rdr.Read())

{

int ID = (int)rdr[0];

string Name = (string)rdr[1];

Console.WriteLine(ID + Name);

}

 
 

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

finally

{

conn.Close();

}

}

}

}

 
 

0 件のコメント:

コメントを投稿

UA-9417263-1