using System;
using System.Data;
using System.Data.OleDb;
class CommandExample {
static OleDbConnection cn;
public static void Main()
{
OleCn(); Openning();
Console.WriteLine("The Original Table"); Output();
Console.WriteLine("Added Table"); Adding(); Output();
Console.WriteLine("Modified Table");Modifying();Output();
Console.WriteLine("Deleted Table"); Deleting(); Output();
Closing();
}
public static void OleCn() {
string OleCnString=("Provider=MSDAORA;data source=bitcamp;User ID=scott;Password=tiger");
cn = new OleDbConnection(OleCnString);
}
public static void Openning(){
cn.Open();
}
public static void Output() {
string sql = "SELECT name, tel, addr FROM addrbook";
OleDbCommand cmd; OleDbDataReader dr;
cmd = new OleDbCommand(sql, cn);
dr = cmd.ExecuteReader(); Console.Write("\n");
while(dr.Read()) {
Console.WriteLine("{0, -10}\t{1, -10}\t{2, -10}",
dr["name"].ToString().Trim(),dr["tel"].ToString().Trim(),
dr["addr"].ToString().Trim());
}
Console.Write("\n"); dr.Close();
}
public static void Adding() {
string sqladd = "INSERT INTO addrbook (name, tel, addr) ";
sqladd += " VALUES ('jclee', '01039295919','경기 수원시')";
OleDbCommand cmdAdd = new OleDbCommand(sqladd, cn);
int rowsadded = cmdAdd.ExecuteNonQuery();
Console.WriteLine("Number of rows added: " + rowsadded);
}
public static void Modifying() {
string sqlmodify = "UPDATE addrbook SET Name = 'jclee2' WHERE name = 'jclee'";
OleDbCommand cmdupdate = new OleDbCommand(sqlmodify, cn);
int rows = cmdupdate.ExecuteNonQuery();
Console.WriteLine("Number of rows modified: " + rows);
}
public static void Deleting() {
string sqldelete = "DELETE FROM addrbook WHERE name like 'jclee%'";
OleDbCommand cmddelete = new OleDbCommand(sqldelete, cn);
int rows = cmddelete.ExecuteNonQuery();
Console.WriteLine("Number of rows deleted: " + rows);
}
public static void Closing() {
cn.Close();
}
}
=============================================================
[결과]
The Original Table
이종철 333-4444 경기 고양
Added Table
Number of rows added: 1
이종철 333-4444 경기 고양
jclee 01039295919 경기 수원시
Modified Table
Number of rows modified: 1
이종철 333-4444 경기 고양
jclee2 01039295919 경기 수원시
Deleted Table
Number of rows deleted: 1
이종철 333-4444 경기 고양
Press any key to continue
댓글 없음:
댓글 쓰기