 |
|
[.NET]
การ save ข้อมูลลง database การไช้คำสั่ง sql save ต่างตาราง |
|
 |
|
|
 |
 |
|
สั้นๆน้อยๆดีเน้นกระทัดรัดเรอะครับ
บ้านๆครับ
SQL Insert/Update
ร่วมกับ for
ง่ายๆสั้นๆกระทัดรัด  
|
 |
 |
 |
 |
Date :
2015-09-21 15:29:00 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค๊ด จะประมานไหนครับ
|
 |
 |
 |
 |
Date :
2015-09-21 15:36:02 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประชดครับ(โอ๊ยน๊   )
มีข้อมูล datable sql หรือโค๊ดที่ทำไปแล้วมั่งครับ
|
 |
 |
 |
 |
Date :
2015-09-21 16:03:08 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากรู้ว่า objCmd มันคือ ตัว อะไรอ่าครับ
ได้มาจาก https://www.thaicreate.com/dotnet/forum/065762.html
Code (C#)
01. Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
02.
03. strsql = "insert into OrderDetail(OrderID,ProductID,NumberOrder,UnitID,Weight,UnitWeightID,Driver,RegisDriver) values(@OrderId,@ProductID,@ProductName,@NumberOrder,@UnitID,@Weight,@UnitWeightID,@Driver,@RegisDriver)"
04. objCmd = New System.Data.SqlClient.SqlCommand(strsql, DbConn)
05. For i As Integer = 0 To DataGridOrder.RowCount - 1
06. If Not DataGridOrder.Rows(i).IsNewRow Then
07. With objCmd
08. .Parameters.Add(New SqlParameter( "@OrderId" , orderID.Text))
09. .Parameters.Add(New SqlParameter( "@ProductID" , DataGridOrder.Rows(i).Cells(0).Value))
10. .Parameters.Add(New SqlParameter( "@NumberOrder" , DataGridOrder.Rows(i).Cells(1).Value))
11. .Parameters.Add(New SqlParameter( "@UnitId" , DataGridOrder.Rows(i).Cells(2).Value))
12. .Parameters.Add(New SqlParameter( "@Weight" , DataGridOrder.Rows(i).Cells(3).Value))
13. .Parameters.Add(New SqlParameter( "@UnitWeightID" , DataGridOrder.Rows(i).Cells(4).Value))
14. .Parameters.Add(New SqlParameter( "@Driver" , Driver.Text))
15. .Parameters.Add(New SqlParameter( "@RegisDriver" , RegisDriver.Text))
16. End With
17. objCmd.ExecuteNonQuery()
18. objCmd.Parameters.Clear()
19. End If
20. Next
21.
22. MessageBox.Show( "บันทึกข้อมูลแล้ว" )
23. DbConn.Close()
24. End Sub
|
 |
 |
 |
 |
Date :
2015-09-21 16:55:26 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มัน eror ว่า Incorrect syntax near '@product_code'. ครับ ที่บรรทัด scm2.ExecuteNonQuery(); อ่าครับ ต้องทำอย่างไรดีครับ
Code (C#)
01. private void btnsave_receive_CheckedChanged( object sender, EventArgs e)
02. {
03. SqlCommand scm2 = new SqlCommand( "receive_details (product_code , product_serial, product_name, product_unit_name, product_unit_num, remark, receive_status_id) values (@product_code , @product_serial, @product_name, @product_unit_name, @product_unit_num,@remark, @receive_status_id)" , conn);
04.
05. for ( int i = 0; i <= dataGridView1.RowCount - 1; i++)
06. {
07. if (!dataGridView1.Rows[i].IsNewRow)
08.
09. {
10.
11. scm2.Parameters.AddWithValue( " @product_code " , dataGridView1.Rows[i].Cells[2].Value);
12. scm2.Parameters.AddWithValue( " @product_serial " , dataGridView1.Rows[i].Cells[3].Value);
13. scm2.Parameters.AddWithValue( " @product_name " , dataGridView1.Rows[i].Cells[4].Value);
14. scm2.Parameters.AddWithValue( " @product_unit_name " , dataGridView1.Rows[i].Cells[5].Value);
15. scm2.Parameters.AddWithValue( " @remark " , dataGridView1.Rows[i].Cells[7].Value);
16. scm2.Parameters.AddWithValue( " @receive_status_id " , dataGridView1.Rows[i].Cells[9].Value);
17. scm2.Parameters.AddWithValue( " @product_unit_num " , dataGridView1.Rows[i].Cells[10].Value);
18.
19. }
20. }
21.
22. conn.Open();
23. scm2.ExecuteNonQuery();
24. conn.Close();
25. DialogResult dialogResult = MessageBox.Show( "Save success" , "Result" , MessageBoxButtons.YesNo);
26. if (dialogResult == DialogResult.Yes)
27. {
28. load_default();
29. textboxNoEnable();
30. }
31. else if (dialogResult == DialogResult.No)
32. {
33.
34. }
|
 |
 |
 |
 |
Date :
2015-09-22 12:07:57 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
INSERT INTO หายไปไหนแล้วละ
|
 |
 |
 |
 |
Date :
2015-09-22 12:12:10 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่ม เข้าไปแล้วครับ ก็ error แบบ เดิม ครับ
|
 |
 |
 |
 |
Date :
2015-09-22 13:02:53 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
scm2.Parameters.AddWithValue(" @product_code ", dataGridView1.Rows[i].Cells[2].Value);
scm2.Parameters.AddWithValue(" @product_serial ", dataGridView1.Rows[i].Cells[3].Value);
scm2.Parameters.AddWithValue(" @product_name ", dataGridView1.Rows[i].Cells[4].Value);
scm2.Parameters.AddWithValue(" @product_unit_name ", dataGridView1.Rows[i].Cells[5].Value);
scm2.Parameters.AddWithValue(" @remark ", dataGridView1.Rows[i].Cells[7].Value);
scm2.Parameters.AddWithValue(" @receive_status_id ", dataGridView1.Rows[i].Cells[9].Value);
scm2.Parameters.AddWithValue(" @product_unit_num ", dataGridView1.Rows[i].Cells[10].Value);
พวกนี้ครับ ลองตัดช่องว่างออกให้หมดครับ ตรง " @product_code " จะเป็น "@product_code"
|
 |
 |
 |
 |
Date :
2015-09-22 13:29:35 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครั้งนี้ error นี้ครับ The variable name '@product_code' has already been declared. Variable names must be unique within a query batch or stored procedure. ที่บรรทัดเดิมครับ scm2.ExecuteNonQuery();
|
 |
 |
 |
 |
Date :
2015-09-22 13:41:07 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดู โค้ดล่าสุดหน่อยครับ
|
 |
 |
 |
 |
Date :
2015-09-22 14:01:47 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่ครับ
Code (C#)
01. SqlCommand scm2 = new SqlCommand( "INSERT INTO receive_details (product_code , product_serial, product_name, product_unit_name, product_unit_num, remark, receive_status_id) values (@product_code, @product_serial, @product_name, @product_unit_name, @product_unit_num,@remark, @receive_status_id) " , conn);
02.
03. for ( int i = 0; i <= dataGridView1.RowCount - 1; i++)
04. {
05. if (!dataGridView1.Rows[i].IsNewRow)
06.
07. {
08. scm2.Parameters.AddWithValue( "@product_code" , dataGridView1.Rows[i].Cells[2].Value);
09. scm2.Parameters.AddWithValue( "@product_serial" , dataGridView1.Rows[i].Cells[3].Value);
10. scm2.Parameters.AddWithValue( "@product_name" , dataGridView1.Rows[i].Cells[4].Value);
11. scm2.Parameters.AddWithValue( "@product_unit_name" , dataGridView1.Rows[i].Cells[5].Value);
12. scm2.Parameters.AddWithValue( "@remark" , dataGridView1.Rows[i].Cells[7].Value);
13. scm2.Parameters.AddWithValue( "@receive_status_id" , dataGridView1.Rows[i].Cells[9].Value);
14. scm2.Parameters.AddWithValue( "@product_unit_num" , dataGridView1.Rows[i].Cells[10].Value);
15.
16. }
17. }
18.
19. conn.Open();
20. scm2.ExecuteNonQuery();
21. conn.Close();
|
 |
 |
 |
 |
Date :
2015-09-22 14:03:45 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่มันขึ้นแบบนี้เพราะมันไม่มีข้อมูลครับ หรือ มันไม่เข้า For
|
 |
 |
 |
 |
Date :
2015-09-22 14:08:42 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลมีนะครับ อันนี้ผมลองดีบัคดู

error 1

error 2

|
 |
 |
 |
 |
Date :
2015-09-22 14:20:34 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ย้ายตัวนี้ scm2.ExecuteNonQuery(); เข้าไปไว้ใน If ครับ แล้ว conn.Open(); เอาไว้บน For
|
 |
 |
 |
 |
Date :
2015-09-22 14:25:41 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลืมบอกไปอย่าง
มันไม่ได้เริ่มต้น Cells จาก 1 แต่มันเริ่ม จาก 0 ครับ
อย่าลืมแก้ด้วยครับ
 
|
 |
 |
 |
 |
Date :
2015-09-22 14:31:40 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ มัน เออเร่อแบบเดิมครับ บางครั้งก็เซฟ ได้ บางครั้ง เลือก 2 ตัว แล้ว เซฟ มันเข้า ไปเซฟ ตัวเดียว ส่วนอีกตัวขึ้น error ครับ

|
 |
 |
 |
 |
Date :
2015-09-22 14:46:20 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
01. for ( int i = 0; i <= dataGridView1.RowCount - 1; i++)
02. {
03. if (!dataGridView1.Rows[i].IsNewRow)
04. {
05. SqlCommand scm2 = new SqlCommand( "INSERT INTO receive_details (product_code , product_serial, product_name, product_unit_name, product_unit_num, remark, receive_status_id) values (@product_code, @product_serial, @product_name, @product_unit_name, @product_unit_num,@remark, @receive_status_id) " , conn);
06. if (conn.State == ConnectionState.Closed)
07. conn.Open();
08.
09. scm2.Parameters.AddWithValue( "@product_code" , dataGridView1.Rows[i].Cells[2].Value);
10. scm2.Parameters.AddWithValue( "@product_serial" , dataGridView1.Rows[i].Cells[3].Value);
11. scm2.Parameters.AddWithValue( "@product_name" , dataGridView1.Rows[i].Cells[4].Value);
12. scm2.Parameters.AddWithValue( "@product_unit_name" , dataGridView1.Rows[i].Cells[5].Value);
13. scm2.Parameters.AddWithValue( "@remark" , dataGridView1.Rows[i].Cells[7].Value);
14. scm2.Parameters.AddWithValue( "@receive_status_id" , dataGridView1.Rows[i].Cells[9].Value);
15. scm2.Parameters.AddWithValue( "@product_unit_num" , dataGridView1.Rows[i].Cells[10].Value);
16. scm2.ExecuteNonQuery();
17.
18. }
19. }
20.
21.
22. conn.Close();
ตามนี้เลย
|
 |
 |
 |
 |
Date :
2015-09-22 15:01:08 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ แล้วครับ ขอบคุณ ทั้งสองท่านมากครับ ช่วย อทิบายหน่อย ได้มั้ยครับ ตรง if (conn.State == ConnectionState.Closed) อ่าครับ
ขอบคุณครับ Y^Y
|
 |
 |
 |
 |
Date :
2015-09-22 15:13:25 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
เป็นมหากาพย์เลยนะครับ
แต่ก็ดีที่ทำได้ครับ
|
 |
 |
 |
 |
Date :
2015-09-22 15:21:01 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
if (conn.State == ConnectionState.Closed)
conn.Open();
ตรวจสอบสถานะการเชื่อมต่อกับฐานข้อมูล ถ้าการเชื่อมต่อปิดอยู่ ให้เปิดการเชื่อมต่อใหม่
|
 |
 |
 |
 |
Date :
2015-09-22 15:26:30 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆ ครับ
|
 |
 |
 |
 |
Date :
2015-09-22 15:36:18 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ส่งคำขอบคุณแทนแล้วนะ ส่วนน้ำสีเหลืองมีฟองผมขอแล้วกัน 5555(ไม่เอาที่ปล่อยจากตัวนะ 555)
|
ประวัติการแก้ไข 2015-09-22 15:41:33
 |
 |
 |
 |
Date :
2015-09-22 15:40:50 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาดปายครับ
พระราม 3 ซอย 23 ครับ
  
|
 |
 |
 |
 |
Date :
2015-09-22 15:59:34 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|