SqlConnection conn = new SqlConnection("Data Source=10.11.1.1;Initial Catalog=GIS;User ID=sa;Password=Pocket007");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"select std_quation_id,job_name,ORIGIN,DESTINATION,VEHICLE_TYPE_ID,SUP_PRICE,PORT_ID,CUSTOMER_NAME,YARD from MT_FLST_QT_QUOTATION where QT_ID like('%" + qt_id.Text + "%')";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.Columns.Clear();
dataGridView1.DataSource = null;
dataGridView1.DataSource = dt;
conn.Close();
ปุ่มค้นหาครับ
string sqlquery2 = ("SELECT * FROM MT_FLST_QT_QUOTATION ");
int mm;
mm = dataGridView1.Rows.Count - 1;
for (int i = 0; i < mm; i++)
{
strSQLupdate2 = "UPDATE MT_FLST_QT_QUOTATION SET job_name=@jn,QT_ID=@QT_ID2,ORIGIN=@ORIGIN,SUP_PRICE=@SUP,DESTINATION=@DEST,VEHICLE_TYPE_ID=@V_TYPE,PORT_ID=@PORT_ID,CUSTOMER_NAME=@cn,yard=@yard WHERE std_quation_id='" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "' ";
objCmdupdate2 = new SqlCommand(strSQLupdate2, objConn);
}
DECLARE @TestTable TABLE (Quot_No varchar(10), RefNo int, CustomerName varchar(50))
/*Test Data*/
INSERT INTO @TestTable (Quot_No, RefNo, CustomerName)
VALUES ('no-1', 2, 'Customer B')
/* Update/Insert/Delete */
MERGE INTO @TestTable AS TS
USING (
VALUES (
'no-1'
,1
,'Customer A'
)
,(
'no-1'
,2
,'Customer B Change update'
)
,(
'no-1'
,3
,'Customer C'
)
) AS DS(Quot_No, RefNo, CustomerName)
ON TS.Quot_No = DS.Quot_No AND TS.RefNo = DS.RefNo
WHEN MATCHED AND DS.CustomerName = N'' /*Empty Customer Name*/
THEN
DELETE
WHEN MATCHED
THEN
UPDATE
SET CustomerName = DS.CustomerName
WHEN NOT MATCHED BY TARGET
THEN
INSERT (Quot_No, RefNo, CustomerName)
VALUES (DS.Quot_No, DS.RefNo, DS.CustomerName);
/*Display Data*/
SELECT * FROM @TestTable ORDER BY 1, 2
WHEN MATCHED AND DS.CustomerName = N'' /*Empty Customer Name*/
THEN
DELETE
WHEN MATCHED AND TS.CustomerName <> DS.CustomerName
THEN
UPDATE
SET CustomerName = DS.CustomerName
WHEN MATCHED
THEN
UPDATE
SET CustomerName = '+55555'
Public Class Form1 'Windows Forms Application
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dtTemp As New DataTable()
dtTemp.Columns.AddRange(New DataColumn() {New DataColumn("Quot_No", GetType(String)),
New DataColumn("RefNo", GetType(Integer)),
New DataColumn("CustomerName", GetType(String))})
dtTemp.Rows.Add(New Object() {"no-1", 1, "Customer A"})
dtTemp.Rows.Add(New Object() {"no-1", 2, "Customer B"})
dtTemp.Rows.Add(New Object() {"no-1", 3, "Customer C"})
dtTemp.Rows.Add(New Object() {"no-1", 4, "Customer D"})
dtTemp.AcceptChanges() '*****
dtTemp.Rows(1).Item("CustomerName") = "Customer B Update" 'ทดสอบแก้ไขข้อมูล
dtTemp.Rows(3).Delete() 'ทดสอบลบข้อมูล
dtTemp.Rows.Add(New Object() {"no-1", 5, "Customer E Added"}) 'ทดสอบเพิ่มข้อมูล
Dim CheckRowsState = dtTemp.GetChanges() 'Added, Deleted, Modified
If CheckRowsState IsNot Nothing Then
For i As Integer = 0 To CheckRowsState.Rows.Count - 1
If CheckRowsState.Rows(i).RowState = DataRowState.Added Then
MsgBox(CheckRowsState.Rows(i).Item("CustomerName")) 'Customer E Added
End If
If CheckRowsState.Rows(i).RowState = DataRowState.Modified Then
MsgBox(CheckRowsState.Rows(i).Item("CustomerName")) 'Customer B Update
End If
If CheckRowsState.Rows(i).RowState = DataRowState.Deleted Then
Dim drCol0 = CheckRowsState.Rows(i)("Quot_No", DataRowVersion.Original)
Dim drCol1 = CheckRowsState.Rows(i)("RefNo", DataRowVersion.Original)
Dim drCol2 = CheckRowsState.Rows(i)("CustomerName", DataRowVersion.Original)
MsgBox(drCol0 & ", " & drCol1 & ", " & drCol2) ' no-1, 4, Customer D
End If
Next
End If
dtTemp.AcceptChanges() '*****
End Sub
End Class