 |
|
เราอยากให้กดปุ่มเดียว แล้ว ทำการ query 2 คำสั่ง เช่น update กับ delete ไปพร้อม ๆ กัน ชี้แนะด้วยครับ |
|
 |
|
|
 |
 |
|
คุณก็เขียน 2 Query แค่นั้นแหละครับ
Code (C#)
System.Data.OleDb.OleDbConnection objConn;
System.Data.OleDb.OleDbCommand objCmd;
String strConnString, strSQL;
OleDbTransaction Trans;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb") + ";Jet OLEDB:Database Password=;";
objConn = new System.Data.OleDb.OleDbConnection(strConnString);
objConn.Open();
//*** Query 1 ***//
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')";
objCmd = new System.Data.OleDb.OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
//*** Query 2 ***//
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')";
objCmd = new System.Data.OleDb.OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
objCmd = null;
objConn.Close();
objConn = null;
|
 |
 |
 |
 |
Date :
2012-09-05 06:05:58 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมขอ code vb 2010 ด้วยได้ไหมค้าบ ชี้แนะด้วยค้าบ
|
 |
 |
 |
 |
Date :
2012-09-05 09:04:28 |
By :
billspy66 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ทราบว่ามันถูกหรือเปล่า แต่มันติดที่ผมทำตำหนิไว้ครับ
Code (C#)
String strConnString, strSQL;
strConnString = "Data Source=MaGGy-Notebook;Initial Catalog=Project Train;User ID=poorivat;Password=poorivat";
SqlConnection Conn = new SqlConnection(strConnString);
//*** Query 1 ***//
strSQL = "INSERT INTO Ticket VALUES('" + Label1.Text + "','" + Label2.Text + "','" + DDLType.Text + "','" + Firsttxt.Text + "','" + LastTxt.Text + "','" + Ticktotal.Text + "','" + Stock.Text + "','" + DDLEmp.Text + "','" + lblText1.Text + "','" + lblcal.Text + "')";
Conn.Open();
SqlCommand Cmd = new SqlCommand(strSQL, Conn);
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.ExecuteNonQuery();
//*** Query 2 ***//
strSQL = "UPDATE TotalTicket set TicketLeft = TicketLeft - " + Ticktotal.Text + " where TrainID=" + LblTrainID.Text + "";
Conn.Open();
SqlCommand Cmd = new SqlCommand(strSQL, Conn);
Cmd.Connection = Conn;
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.ExecuteNonQuery();
Cmd = null;
Conn.Close();
Conn = null;
|
 |
 |
 |
 |
Date :
2012-09-05 10:18:29 |
By :
Chunate |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ติดที่ บรรทัดนี้ ครับ
Code (C#)
SqlCommand Cmd = new SqlCommand(strSQL, Conn);
ของ //*** Query 2 ***//
|
 |
 |
 |
 |
Date :
2012-09-05 10:23:55 |
By :
Chunate |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนแบบนี้ได้มั้ยครับ แต่มันยังติดเส้นใต้สีแดง ตรงที่ Cmd ของ //*** Query 2 ***// ทุกบรรทัดเลยครับ แนะนำด้วยครับผม
Code (C#)
{
String strConnString, strSQL;
strConnString = "Data Source=MaGGy-Notebook;Initial Catalog=Project Train;User ID=poorivat;Password=poorivat";
SqlConnection Conn = new SqlConnection(strConnString);
//*** Query 1 ***//
strSQL = "INSERT INTO Ticket VALUES('" + Label1.Text + "','" + Label2.Text + "','" + DDLType.Text + "','" + Firsttxt.Text + "','" + LastTxt.Text + "','" + Ticktotal.Text + "','" + Stock.Text + "','" + DDLEmp.Text + "','" + lblText1.Text + "','" + lblcal.Text + "')";
Conn.Open();
SqlCommand Cmd = new SqlCommand(strSQL, Conn);
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.ExecuteNonQuery();
//*** Query 2 ***//
strSQL = "UPDATE TotalTicket set TicketLeft = TicketLeft - " + Ticktotal.Text + " where TrainID=" + LblTrainID.Text + "";
Conn.Open();
SqlCommand Cmd = new SqlCommand(strSQL, Conn);
Cmd.Connection = Conn;
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.ExecuteNonQuery();
Cmd = null;
Conn.Close();
Conn = null;
}
|
 |
 |
 |
 |
Date :
2012-09-06 12:48:04 |
By :
Chunate |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (ASP)
{
String strConnString, strSQL;
strConnString = "Data Source=MaGGy-Notebook;Initial Catalog=Project Train;User ID=poorivat;Password=poorivat";
SqlConnection Conn = new SqlConnection(strConnString);
//*** Query 1 ***//
strSQL = "INSERT INTO Ticket VALUES('" + Label1.Text + "','" + Label2.Text + "','" + DDLType.Text + "','" + Firsttxt.Text + "','" + LastTxt.Text + "','" + Ticktotal.Text + "','" + Stock.Text + "','" + DDLEmp.Text + "','" + lblText1.Text + "','" + lblcal.Text + "')";
Conn.Open();
SqlCommand Cmd = new SqlCommand(strSQL, Conn);
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.ExecuteNonQuery();
//*** Query 2 ***//
strSQL = "UPDATE TotalTicket set TicketLeft = TicketLeft - " + Ticktotal.Text + " where TrainID=" + LblTrainID.Text + "";
Cmd.Connection = Conn
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.ExecuteNonQuery();
Cmd = null;
Conn.Close();
Conn = null;
}
|
 |
 |
 |
 |
Date :
2012-09-06 15:30:51 |
By :
hwang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
for VB.NET
Code (VB.NET)
Dim objConn As System.Data.OleDb.OleDbConnection
Dim objCmd As System.Data.OleDb.OleDbCommand
Dim strConnString As [String], strSQL As [String]
Dim Trans As OleDbTransaction
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";Jet OLEDB:Database Password=;"
objConn = New System.Data.OleDb.OleDbConnection(strConnString)
objConn.Open()
'*** Query 1 ***//
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & "VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')"
objCmd = New System.Data.OleDb.OleDbCommand()
objCmd.Connection = objConn
objCmd.CommandType = CommandType.Text
objCmd.CommandText = strSQL
objCmd.ExecuteNonQuery()
'*** Query 2 ***//
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & "VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')"
objCmd = New System.Data.OleDb.OleDbCommand()
objCmd.Connection = objConn
objCmd.CommandType = CommandType.Text
objCmd.CommandText = strSQL
objCmd.ExecuteNonQuery()
objCmd = Nothing
objConn.Close()
objConn = Nothing
|
 |
 |
 |
 |
Date :
2012-09-06 16:21:18 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วถ้าอันแรกบันทึกข้อมูลผ่าน อันที่ 2 บันทึกข้อมูลไม่ผ่านละ ใช้ transection เหอะ
---- connectin string ---
ตัวแปรนี้จะเรียกใช้งาน connection string db จาก web.config
dim strConnString as stringf = configurationmanager.appsettings("ConectStringDb")
----- web.config ----
<add key="ConectStringDb" value="Data SourceMaGGy-Notebook;Initial Catalog=Project Train;User ID=poorivat;Password=poorivat;" />
--- Code ในปุ่ม บันทึก -----
try
if Adddata then
response.write("บันทึกข้อมูลแล้ว !")
else
response.write("เกิดข้อผิดพลาด ไม่สามารถบันทึกข้อมูลได้ !")
end if
catch ex
end try
---- code Function ---
Function Adddata() as boolean
dim Sqlconn as SqlConnection= new SqlConnection(strConnString)
dim TranS as sqlTransection
try
Sqlconn.open
Trans = Sqlconn.BeginTransection
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES (@CustomerID,@Name,@Email,@CountryCode,@Budget,@Used)"
dim Fm(5) as sqlclient.sqlparameter
Fm(0) = new sqlclient.sqlparameter("@CustomerID",'C005')
Fm(1) = new sqlclient.sqlparameter("@Name",'Weerachai Nukitram')
Fm(2) = new sqlclient.sqlparameter("@Email",'[email protected]')
Fm(3) = new sqlclient.sqlparameter("@CountryCode",'TH')
Fm(4) = new sqlclient.sqlparameter("@Budget",'2000000')
Fm(5) = new sqlclient.sqlparameter("@Used",'1000000')
sqlhelper.ExecuteNonQuery(TranS,Commantype.Text,strSQL ,fm )
'---------- เห็นข้อมูลที่คุณ บันทึก ข้อมูลเดียปวกัน ผมเลยใช้ query ตัวเดียวนะ
sqlhelper.ExecuteNonQuery(TranS,Commantype.Text,strSQL ,fm )
Trans.Commit()
Sqlconn.close
return true
catch ex
Trans.Rollback()
Sqlconn.close
return false
end try
end Function
|
 |
 |
 |
 |
Date :
2013-06-19 09:45:47 |
By :
เมาทั้งวัน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อาจจะมีคำผิดไปนิดนึง เพราะเขียนสดในนี้ ต้องขออภัยมา ณ ที่นี้ด้วย ^^
|
 |
 |
 |
 |
Date :
2013-06-19 09:47:06 |
By :
เมาทั้งวัน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อีกแนวทาง คุณเจ้าของกระทู้อาจจะสร้าง Store Procedure ที่มีคำสั่งทั้ง Insert Update ไว้ใน SQL Server และเรียกใช้ผ่านโปรแกรมครั้งเดียวก็ได้นะครับ
|
 |
 |
 |
 |
Date :
2013-06-19 10:11:38 |
By :
01000010 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฺเกริ่นนำ หลักการ Advance Store Procedure
Code
WITH cte
AS (
SELECT *
,ROW_NUMBER() OVER (
ORDER BY InputType DESC
,StringNum ASC
) AS rn
,(
(
(
(
ROW_NUMBER() OVER (
PARTITION BY InputType ORDER BY InputType DESC
,StringNum ASC
) + 4
) / 5
) - 1
) % 5
) + 1 AS rnGroup
FROM (
SELECT InputType
,StringNum
,LimitPrice1
,LimitPrice2
,Y.Price1
,Y.Price2
,ISNULL(LimitPrice1, 0) + ISNULL(Y.Price1, 0) AS OverP1
,ISNULL(LimitPrice2, 0) + ISNULL(Y.Price2, 0) AS OverP2
FROM GoodLuckConfigSpread AS Z
CROSS APPLY (
SELECT SUM(Price1) AS Price1
,SUM(Price2) AS Price2
FROM GoodLuck a
WHERE a.InputType = Z.InputType
AND a.StringNum = Z.StringNum
) Y
) X
WHERE X.OverP1 > 0
OR X.OverP2 > 0
)
,cte0
AS (
SELECT InputType
,MIN(rn) AS rn
FROM cte
GROUP BY InputType
)
,cte1
AS (
SELECT cte.InputType, StringNum, OverP1, OverP2, cte.rn
FROM cte
INNER JOIN cte0 ON cte.InputType = cte0.InputType
AND (cte.rn - cte0.rn) % 5 = 0
)
SELECT cte1.InputType
,cte1.StringNum AS StringNum_1
,cte1.OverP1 AS OverP1_1
,cte1.OverP2 AS OverP2_1
,cte2.StringNum AS StringNum_2
,cte2.OverP1 AS OverP1_2
,cte2.OverP2 AS OverP2_2
,cte3.StringNum AS StringNum_3
,cte3.OverP1 AS OverP1_3
,cte3.OverP2 AS OverP2_3
,cte4.StringNum AS StringNum_4
,cte4.OverP1 AS OverP1_4
,cte4.OverP2 AS OverP2_4
,cte5.StringNum AS StringNum_5
,cte5.OverP1 AS OverP1_5
,cte5.OverP2 AS OverP2_5
FROM cte1
LEFT JOIN cte AS cte2 ON cte1.InputType = cte2.InputType
AND cte2.rn - cte1.rn = 1
LEFT JOIN cte AS cte3 ON cte1.InputType = cte3.InputType
AND cte3.rn - cte1.rn = 2
LEFT JOIN cte AS cte4 ON cte1.InputType = cte4.InputType
AND cte4.rn - cte1.rn = 3
LEFT JOIN cte AS cte5 ON cte1.InputType = cte5.InputType
AND cte5.rn - cte1.rn = 4
ORDER BY InputType DESC
|
 |
 |
 |
 |
Date :
2013-06-19 10:53:00 |
By :
ผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฺเกริ่นนำ หลักการ Advance Store Procedure 001
#14 ทำงานได้ถูกต้อง 100% ไม่มีผิดพลาด ไม่ว่ากรณีใดฯ
แต่ทำงานได้ชักช้า (อายุแค่ 13 แต่ ท้องคนที่ 14)
|
 |
 |
 |
 |
Date :
2013-06-19 11:13:33 |
By :
ผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
#15, 16 ก็ต้องกลับไปดูข้อที่ #17 - 001
|
 |
 |
 |
 |
Date :
2013-06-19 11:17:54 |
By :
ผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
E = MC ^ 2
ผมพยายามสังเกตุดูและไม่เคย เห็น ว่ามีวงเล็บต่อท้าย
[x] บางคน อาจจะ เห็น เป็นอย่างอื่นก็ได้ เช่น
เห็น ช้าง
เห็น นก
เห็น ผึ้ง
เห็น ... กินน้ำผึ้ง
ก็อาจเป็นไปได้เหมือนกันครับ + 55555
|
 |
 |
 |
 |
Date :
2013-06-21 11:27:27 |
By :
ผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|