ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# ADO.NET 접속 Console접속 SELECT ,UPDATE
    Writer/WarmingUp Code 2008. 11. 12. 09:51
    반응형
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    namespace ConLess
    {
    class Program
    {
    static void Main(string[] args)
    {
    // 커넥션
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = @"data source = (local); initial catalog=northwind; user id=sa ; password=";
    // 데이터세을 쿼리
    string selqry = "SELECT categoryID, categoryName FROM categories";
    // 실제 작업쿼리
    string updqry = "UPDATE categories SET categoryName = @categoryName" + " WHERE categoryID=@categoryID";

    SqlDataAdapter adapter = null;
    SqlCommand selCmd = null;
    SqlCommand updCmd = null;
    DataSet ds = null;

    int icategoryId = 8;
    string strCategoryName = "Seafood";

    try
    {
    selCmd = new SqlCommand(selqry, conn); // 셀렉트 커맨드
    updCmd = new SqlCommand(updqry, conn); //업데이트커맨드
    updCmd.Parameters.Add("@categoryName", SqlDbType.NVarChar, 15).Value = strCategoryName;
    updCmd.Parameters.Add("@categoryID", SqlDbType.Int).Value = icategor
    yId;
    adapter = new SqlDataAdapter();
    adapter.SelectCommand = selCmd;
    adapter.UpdateCommand = updCmd;

    ds = new DataSet();
    adapter.Fill(ds, "categories");

    DataTable table = ds.Tables["categories"];
    DataRow[] row = table.Select("categoryId=" + icategoryId.ToString());
    if (row.Length >0)
    {
    row[0]["categoryName"] = strCategoryName;
    adapter.Update(ds.Tables["categories"]);
    }
    Console.WriteLine("수정완료");
    }
    catch (DataException de)
    {
    Console.WriteLine("{0}, 에러발생",de.ToString());
    }

    }
    }
    }

    반응형

    'Writer > WarmingUp Code' 카테고리의 다른 글

    워밍업 코드 클래스 2  (0) 2008.12.23
    워밍업 코드 클래스  (0) 2008.12.23
    ADO.NET 접속  (0) 2008.10.29
    메모장 만들기(MDI)포함  (0) 2008.10.29
    C# Thread 스레드  (0) 2008.10.29
    C# 객체생성과제  (0) 2008.10.20
    C# 과제  (0) 2008.10.17
    abstract interface  (0) 2008.10.15

    댓글

Designed by Tistory.