[C#4.0 ADO.NET강좌]ADO.NET데이터조작, 명령객체, OleDbCommand 예제
아래의 예제는 ExecuteScalar를 이용하여 테이블 행의 개수를 가져오는 예제 입니다.
테이블 안의 행들의 개수를 알아 내는 것으로 ExecuteScalar()를 통해 select count(*) 질의를 수행 하는 것이 효율적 입니다.
테이블 안의 행들의 개수를 알아 내는 것으로 ExecuteScalar()를 통해 select count(*) 질의를 수행 하는 것이 효율적 입니다.
[예제1]
Oracle scott 계정의 EMP 테이블 레코드 건수 리턴 …
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
OleDbConnection Cn = null;
try
{
string conStr = "Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
Cn = new OleDbConnection(conStr);
OleDbCommand cmdSelect = new OleDbCommand("select count(*) from emp", Cn);
Cn.Open();
object count = cmdSelect.ExecuteScalar();
Console.WriteLine("Count of Emp = {0}", count);
Cn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (Cn.State == ConnectionState.Open)
{
Cn.Close();
}
}
OleDbCommand cmdSelect = new OleDbCommand("select count(*) from emp", Cn);
Cn.Open();
object count = cmdSelect.ExecuteScalar();
Console.WriteLine("Count of Emp = {0}", count);
Cn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (Cn.State == ConnectionState.Open)
{
Cn.Close();
}
}
}
}
}
}
}
[결과]
Count of Emp = 15
아래 예제는 EMP TABLE에 대한 입력, 수정, 삭제 예제 입니다.
[예제2]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace ConsoleApplication4
{
using System;
using System.Data;
using System.Data.OleDb;
class CommandExam
{
static OleDbConnection cn;
{
static OleDbConnection cn;
public static void Main()
{
OleCn();
{
OleCn();
Openning();
Console.WriteLine("The Original Table");
Output();
Output();
Console.WriteLine("Added Table");
Adding();
Output();
Adding();
Output();
Console.WriteLine("Modified Table");
Modifying();
Output();
Modifying();
Output();
Console.WriteLine("Deleted Table");
Deleting();
Output();
Deleting();
Output();
Closing();
}
}
public static void OleCn()
{
//string OleCnString = @"Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
string OleCnString = ("Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger");
cn = new OleDbConnection(OleCnString);
}
{
//string OleCnString = @"Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
string OleCnString = ("Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger");
cn = new OleDbConnection(OleCnString);
}
public static void Openning()
{
cn.Open();
}
{
cn.Open();
}
public static void Output()
{
string sql = "SELECT empno id, ename name FROM emp";
{
string sql = "SELECT empno id, ename name FROM emp";
OleDbCommand cmd;
OleDbDataReader dr;
OleDbDataReader dr;
cmd = new OleDbCommand(sql, cn);
dr = cmd.ExecuteReader();
Console.Write("\n");
while (dr.Read())
{
Console.WriteLine("{0, -10}\t{1, -10}",
dr[0].ToString().Trim(),
dr[1].ToString().Trim());
}
Console.Write("\n");
while (dr.Read())
{
Console.WriteLine("{0, -10}\t{1, -10}",
dr[0].ToString().Trim(),
dr[1].ToString().Trim());
}
Console.Write("\n");
dr.Close();
}
}
public static void Adding()
{
try
{
string sqladd = "INSERT INTO emp (empno, ename) VALUES (888, '오?A라?o클¡þ자U바?U')";
OleDbCommand cmdAdd = new OleDbCommand(sqladd, cn);
{
try
{
string sqladd = "INSERT INTO emp (empno, ename) VALUES (888, '오?A라?o클¡þ자U바?U')";
OleDbCommand cmdAdd = new OleDbCommand(sqladd, cn);
int rowsadded = cmdAdd.ExecuteNonQuery();
Console.WriteLine("Number of rows added: " + rowsadded);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine("Number of rows added: " + rowsadded);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Modifying()
{
try
{
string sqlmodify = "UPDATE emp SET ename = '오?A엔?¡?제|이I' WHERE empno = 888";
{
try
{
string sqlmodify = "UPDATE emp SET ename = '오?A엔?¡?제|이I' WHERE empno = 888";
OleDbCommand cmdupdate = new OleDbCommand(sqlmodify, cn);
int rows = cmdupdate.ExecuteNonQuery();
Console.WriteLine("Number of rows modified: " + rows);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Deleting()
{
try
{
string sqldelete = "DELETE FROM emp WHERE empno = 888 ";
{
try
{
string sqldelete = "DELETE FROM emp WHERE empno = 888 ";
OleDbCommand cmddelete = new OleDbCommand(sqldelete, cn);
int rows = cmddelete.ExecuteNonQuery();
Console.WriteLine("Number of rows deleted: " + rows);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Closing()
{
cn.Close();
}
{
cn.Close();
}
}
}
[진짜실무교육&환급100%]SQL/자바/스프링/안드로이드/닷넷C#/웹… | 12-27 | 2362 | ||
[채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1710 | ||
53 | [평일전액환급실무교육]Spring,자바&JSP,안드로이드,웹퍼블리싱,… | 03-15 | 1524 | |
52 | [주말]C#,ASP.NET마스터 | 01-31 | 1632 | |
51 | [기업100%환급,평일주간]SQL기초에서스키마오브젝트,PLSQL,힌트… | 01-31 | 2099 | |
50 | [평일주간야간,주말]C기본&자료구조,알고리즘 | 01-31 | 1285 | |
49 | [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 | 01-19 | 1592 | |
48 | [평일야간,주말]안드로이드개발자과정(Android기초실무) | 01-11 | 1425 | |
47 | [평일야간,주말주간,주말야간]JAVA,Network&JSP&Spring,MyBatis,… | 01-03 | 1962 | |
46 | [진짜실무교육&환급100%]SQL/자바/스프링/안드로이드/닷넷C#/웹… | 12-27 | 2362 | |
45 | [기업100%환급]자바웹개발기초과정(JAVA,JDBC,JSP,Servlet,Aajx,… | 12-19 | 1692 | |
44 | [평일주간야간, 주말]웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX… | 12-14 | 1667 | |
43 | [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1710 | |
42 | [기업100%환급]웹퍼블리싱마스터(HTML5,CSS3,JavaScript,jQUERY) | 12-09 | 1358 | |
41 | [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 | 12-01 | 1583 | |
40 | [기업100%환급]오라클&자바웹스프링신입과정3주(SQL,JAVA,JSP,Se… | 12-01 | 1761 | |
39 | [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) | 12-01 | 1237 |
댓글 없음:
댓글 쓰기