.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

แก้ไขข้อมูลในดาต้าเบส c#.net Options · View
buffkoy
Posted: Sunday, August 03, 2008 3:22:23 PM
Rank: มืออาชีพ
Groups: Member

Joined: 7/7/2008
Posts: 30

คือผมอยากจะแก้ไข ข้อมูลอะคับ ไม่ทราบว่า ช่วยเขียนโค๊ดให้ดูหน่อยได้ไหมคับ เคยเรียนแต่ insert กับ delete ไม่รู้ทำไม

เช่น ผมต้องการแก้ไข ชื่อ ในดาต้าเบสอะ ครับ

แนะนำโค๊ดที่ update เลยนะครับ ส่วนอื่นๆ ที่ติดต่อดาต้าเบส ผมเข้าใจแล้วคับ

paedotnet
Posted: Tuesday, August 05, 2008 10:39:39 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

ก็มีหลายวิธีนะครับขึ้นอยู่กับการใช้งาน ในตัวอย่างนี้ผมจะแสดงแบบใช้ parameter นะครับ

รูปก่อน update

รูปหลัง update

ตัวอย่างโค้ด

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int id=4,age = 18; //ในตัวอย่างนี้ผมเขียนค่าใหม่ที่จะ update ไปตรงๆ ตรงนี้อาจจะเขียนให้รับค่ามาจากที่อื่นก็ได้
            string name = "jimmy";
            SqlParameter name_param, age_param, id_param;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Server=.;Database=testupdate;Integrated Security=SSPI";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "UPDATE table1 SET name = @name , age = @age WHERE id=@id";
            name_param = new SqlParameter();
            name_param.ParameterName = "@name";
            name_param.SqlDbType = SqlDbType.NVarChar;//ชนิดของข้อมูลของค่า parameter name
            name_param.Value = name; //นำค่าตัวแปร name เก็บไว้ใน parameter
            age_param = new SqlParameter();
            age_param.ParameterName = "@age";
            age_param.SqlDbType = SqlDbType.Int;//ชนิดของข้อมูลของค่า parameter age
            age_param.Value = age;//นำค่าตัวแปร age เก็บไว้ใน parameter
            id_param = new SqlParameter();
            id_param.ParameterName = "@id";
            id_param.SqlDbType = SqlDbType.Int;//ชนิดของข้อมูลของค่า parameter id
            id_param.Value = id;//นำค่าตัวแปร id เก็บไว้ใน parameter
            SqlParameter[] param = { name_param,id_param,age_param};
            cmd.Parameters.AddRange(param); //เพิ่มคำสั่ง parameter ทั้งหมดให้กับ cmd
            con.Open();
            cmd.ExecuteNonQuery(); //update ข้อมูล
            con.Close();
        }
    }
}

 

 แต่วิธีนี้จะเป็นแบบ Fix Connection String
(Hardcode) นะครับทางที่ดีควรเก็บค่า ของ connection string ไว้ที่ App.config หรือ web.config จะดีกว่า
ดังนี้

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <connectionStrings>
  <add name="constr" connectionString="Server=.;Database=testupdate;Integrated Security=SSPI"/>
 </connectionStrings>
</configuration>


[With great power comes great responsibility]
buffkoy
Posted: Tuesday, August 05, 2008 5:27:42 PM
Rank: มืออาชีพ
Groups: Member

Joined: 7/7/2008
Posts: 30

ขอบคุนคับ โค๊ดยากจัง แต่ก็ขอบคุนคับ

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by