2016년 12월 29일 목요일

[닷넷교육,C#교육,실무교육학원추천_탑크리에듀][C#4.0 ADO.NET강좌]ADO.NET데이터조작, 명령객체, OleDbCommand 예제

[C#4.0 ADO.NET강좌]ADO.NET데이터조작, 명령객체, OleDbCommand 예제
 
아래의 예제는 ExecuteScalar를 이용하여 테이블 행의 개수를 가져오는 예제 입니다.
테이블 안의 행들의 개수를 알아 내는 것으로 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;

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();
                }
            }
        }
    }
}

[결과]
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;

namespace ConsoleApplication4
{
    using System;
    using System.Data;
    using System.Data.OleDb;
    class CommandExam
    {
        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=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();
        }
        public static void Output()
        {
            string sql = "SELECT empno id, ename name FROM emp";
            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}",
                    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);
                int rowsadded = cmdAdd.ExecuteNonQuery();
                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";
                OleDbCommand cmdupdate = new OleDbCommand(sqlmodify, cn);
                int rows = cmdupdate.ExecuteNonQuery();
                Console.WriteLine("Number of rows modified: " + rows);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        public static void Deleting()
        {
            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());
            }
        }
        public static void Closing()
        {
            cn.Close();
        }
    }
}
 

댓글 없음:

댓글 쓰기