C#資料庫語法新增與刪除

本篇文章中小編要介紹如何使用C#程式碼來與資料庫進行連結,並使用 C#資料庫語法新增與刪除 來將新增資料或刪除資料庫中的檔案,以下就跟著小編來看看這次程式碼的講解範例吧。

 

該如何與資料庫進行連結呢?

 

  C#資料庫程式範例教學 

新增欄位使用   INSERT  語法
刪除欄位使用  DELETE  語法

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
/* 資料庫刪除 編號99 的資料 */
public void RTM_Info_Delete()
{
    string constr = "Data source = 172.20.0.1; initial catalog = SIP_RTM; user id = Webb; password = Gt820000;";
    string sqlStr = "DELETE FROM Temp_5111_Status WHERE [No.]=99";
    try
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStr))
            {
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    catch (Exception)
    { }
}
/* SQL 新增 */
public void RTM_Info_Insert()
{
            _No = 99;
            _TotoalDeviceCount = 0;
            _EnableSiteCount = 0;
            _AccOutput1 = 0;
            _Site0Bank0Count = 0;
            _Site1Bank0Count = 0;
            _Kit_UseCount = 0;
            _Kit_LimitCount = 0;
            _HandlerID = "";
            _RealorDummyRun = "";
            _OperatoeLevel = "";
            _EngineerID = "";
            _OEE = "";
            _Site0Bank0Yields = "";
            _Site1Bank0Yields = "";
            _Site0Bank0Enable = "";
            _Site1Bank0Enable = "";
            _KitID = "";
            DateTime Time = NowTime;
            /* 資料庫指令 */
            string constr = "Data source = 172.20.0.1; initial catalog = SIP_RTM; user id = Webb; password = Gt820000;";
            string sqlStr = "INSERT INTO Temp_5111_Status(Time, [No.], [Serial No.], [Loader Count], [Total Output], UPH, [Machine Model], [Software Version], [Current Work File], [Status of Handler], [Schedular No], [Device Name], [OP ID], [Handler ID], [Real or Dummy Run], [Operatoe Level], Station, [Engineer ID], [Totoal Device Count], OEE,[Enable Site Count], Insertion, [Product Status], [Auto1 Output Count], [Auto2 Output Count], AccOutput1, Site0Bank0Yields, Site1Bank0Yields, Site0Bank0Count, Site1Bank0Count, Site0Bank0Enable, Site1Bank0Enable, [Kit ID], Kit_UseCount, Kit_LimitCount)" +
            " VALUES (@Time, @No, @SerialNo, @LoaderCount, @TotalOutput, @UPH, @MachineModel, @SoftwareVersion ,@CurrentWorkFile, @StatusofHandler, @SchedularNo, @DeviceName, @OPID, @HandlerID, @RealorDummyRun, @OperatoeLevel, @Station, @EngineerID, @TotoalDeviceCount, @OEE, @EnableSiteCount, @Insertion, @ProductStatus, @Auto1OutputCount, @Auto2OutputCount, @AccOutput1, @Site0Bank0Yields, @Site1Bank0Yields, @Site0Bank0Count, @Site1Bank0Count, @Site0Bank0Enable, @Site1Bank0Enable, @KitID, @Kit_UseCount, @Kit_LimitCount)";
            try
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(sqlStr))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Time"              ,Time);
                        cmd.Parameters.AddWithValue("@No"                ,_No);
                        cmd.Parameters.AddWithValue("@SerialNo"          ,SerialNo);
                        cmd.Parameters.AddWithValue("@LoaderCount"       ,LoaderCount);
                        cmd.Parameters.AddWithValue("@TotalOutput"       ,TotalOutput);
                        cmd.Parameters.AddWithValue("@UPH"               ,UPH);
                        cmd.Parameters.AddWithValue("@MachineModel"      ,MachineModel);
                        cmd.Parameters.AddWithValue("@SoftwareVersion"   ,SoftwareVersion);
                        cmd.Parameters.AddWithValue("@CurrentWorkFile"   ,CurrentWorkFile);
                        cmd.Parameters.AddWithValue("@StatusofHandler"   ,StatusofHandler);
                        cmd.Parameters.AddWithValue("@SchedularNo"       ,SchedularNo);
                        cmd.Parameters.AddWithValue("@DeviceName"        ,DeviceName);
                        cmd.Parameters.AddWithValue("@OPID"              ,OPID);
                        cmd.Parameters.AddWithValue("@HandlerID"         ,_HandlerID);
                        cmd.Parameters.AddWithValue("@RealorDummyRun"    ,_RealorDummyRun);
                        cmd.Parameters.AddWithValue("@OperatoeLevel"     ,_OperatoeLevel);
                        cmd.Parameters.AddWithValue("@Station"           ,Station);
                        cmd.Parameters.AddWithValue("@EngineerID"        ,_EngineerID);
                        cmd.Parameters.AddWithValue("@TotoalDeviceCount" ,_TotoalDeviceCount);
                        cmd.Parameters.AddWithValue("@OEE"               ,_OEE);
                        cmd.Parameters.AddWithValue("@EnableSiteCount"   ,_EnableSiteCount);
                        cmd.Parameters.AddWithValue("@Insertion"         ,Insertion);
                        cmd.Parameters.AddWithValue("@ProductStatus"     ,ProductStatus);
                        cmd.Parameters.AddWithValue("@Auto1OutputCount"  ,Auto1OutputCount);
                        cmd.Parameters.AddWithValue("@Auto2OutputCount"  ,Auto2OutputCount);
                        cmd.Parameters.AddWithValue("@AccOutput1"        ,_AccOutput1);
                        cmd.Parameters.AddWithValue("@Site0Bank0Yields"  ,_Site0Bank0Yields);
                        cmd.Parameters.AddWithValue("@Site1Bank0Yields"  ,_Site1Bank0Yields);
                        cmd.Parameters.AddWithValue("@Site0Bank0Count"   ,_Site0Bank0Count);
                        cmd.Parameters.AddWithValue("@Site1Bank0Count"   ,_Site1Bank0Count);
                        cmd.Parameters.AddWithValue("@Site0Bank0Enable"  ,_Site0Bank0Enable);
                        cmd.Parameters.AddWithValue("@Site1Bank0Enable"  ,_Site1Bank0Enable);
                        cmd.Parameters.AddWithValue("@KitID"             ,_KitID);
                        cmd.Parameters.AddWithValue("@Kit_UseCount"      ,_Kit_UseCount);
                        cmd.Parameters.AddWithValue("@Kit_LimitCount"    ,_Kit_LimitCount);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
    catch (Exception)
    { }
}

 
 

延伸閱讀:

C#取得資料夾內的檔案名稱

 

C#判斷資料夾是否存在

C# TextBox Multiline自動刪除第一行

C# Application.DoEvents強制跳脫迴圈

C#常用的特殊字元

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *