'분류 전체보기'에 해당되는 글 131건

  1. 2009.01.24 DB Data Select(Oracle)
  2. 2009.01.24 DataSet을 이용한 Data 추가 예제

DB Data Select(Oracle)

|

using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.OracleClient;


namespace Tran_01
{
    public class OleDbParamTest
    {
        public static void Main()
        {
            string conStr = "Data Source = XE;" +
                "User id = ;" +
                "password = ";
            string Query = "Select * from address where addr = :addr1 or addr = :addr2";
            try
            {
                OracleConnection Oconn = new OracleConnection(conStr);
                Oconn.Open();
                Console.WriteLine("DB 열림");
                OracleCommand cmd = new OracleCommand(Query, Oconn);
                cmd.Parameters.Add("addr1", OracleType.VarChar);
                cmd.Parameters.Add("addr2", OracleType.VarChar);
                cmd.Parameters["addr1"].Value = "seuol";
                cmd.Parameters["addr2"].Value = "Ameria";
                OracleDataReader Odr = cmd.ExecuteReader();
                while (Odr.Read())
                {
                    Console.Write(Odr.GetFloat(0) + "t");
                    Console.Write(Odr.GetString(1) + "t");
                    Console.Write(Odr.GetString(2) + "t");
                    Console.WriteLine();
                }
                Odr.Close();
                Oconn.Close();
            }
            catch (OracleException ErrMsg)
            {
                Console.WriteLine(ErrMsg.Message);
                Console.WriteLine("에러");
            }
        }
    }
}

 

/*

역으로 집어넣는것은 Command.ExecuteNonQuery() 함수 사용 

*/
Trackback 0 And Comment 0

DataSet을 이용한 Data 추가 예제

|

/*

DataBase

Address Table

id number,

name varchar 20

addr varchar 40

*/ 

 

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;;;


namespace Tran_01
{
    public class OleDbParamTest
    {
        public static void Main()
        {
            OracleConnection conn = new OracleConnection();
            try
            {
                conn.ConnectionString =
                              "Data Source=XE;" +
                              "User id=;" +
                              "Password=;";
                conn.Open();
                Console.WriteLine(conn.State);
                Console.WriteLine(conn.ServerVersion);
                Console.WriteLine(conn.Site);
                Console.WriteLine("nn");
                OracleDataAdapter Oadt = new OracleDataAdapter("select * from Address", conn);
                DataSet ds = new DataSet("MyAddress");
                Oadt.Fill(ds, "Address");
                conn.Close();
                DataTable table = ds.Tables[0];
                Console.WriteLine("Row Count : " + table.Columns.Count);
                Print(table);
                DataRow row = table.NewRow();
                row["id"] = "8";
                row["name"] = "Uncle";
                row["addr"] = "Ameria";
                table.Rows.Add(row);

                conn.Open();
                OracleCommand Ocmd = new OracleCommand();
                string strSql = "Insert into address(id,name,addr) values(:pid,:pname,:paddr)";
                Oadt.InsertCommand = new OracleCommand(strSql, conn);
                Oadt.InsertCommand.Parameters.Add("pid", OracleType.Number, 0, "id");
                Oadt.InsertCommand.Parameters.Add("pname", OracleType.VarChar, 20, "name");
                Oadt.InsertCommand.Parameters.Add("paddr", OracleType.VarChar, 40, "addr");

                Oadt.Update(ds, "Address");
                conn.Close();
                Console.WriteLine("Insert that ..");
                Print(table);
            }
            catch (OracleException ErrMssg)
            {
                Console.WriteLine(ErrMssg.Message);
                Console.WriteLine("에러 확인");
            }
            finally
            {
                conn.Close();
            }
        }
        public static void Print(DataTable table)
        {
            DataRowCollection rows = table.Rows;
            foreach (DataRow dr in rows)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                    Console.Write(dr[i] + "t");
                Console.WriteLine();
            }
        }
    }
}

Trackback 0 And Comment 0
prev | 1 | ··· | 41 | 42 | 43 | 44 | next