 |
|
[.NET]
พยายามจะบันทึกค่าเข้าไปสองตาราง ตารางแรกเข้าได้แต่ตารางที่สองเข้าไม่ได้ค่ะ จะใช้ค่าหนึ่งตรงกัน |
|
 |
|
|
 |
 |
|
หลังจากที่ค้นหาและลองทำหลาย ๆ วิธีในนี้(และจาก stackoverflow) แล้ว ทั้ง scope_identity,ident_current,select max(id)
ก็ยังทำไม่ได้เลยค่ะ
เริ่มต้นก็คือ จะเพิ่มข้อมูลเข้าไปที่ตารางแรก โดย PK ของตารางนี้เป็น int และ ai ค่ะ
Code (VB.NET)
01. Try
02. gsql = "insert into member (MemIn,MemName,MemAge,MemTypeId,MemStatusId,MemLevelId,MemTel,Addno,SubdistrictNo,SubdistrictName,District,Province,JoinDate,Gender,Subdistrict,IncomeStatusId,Mempic) values ("
03. gsql += "'" & MemIn.Text & "',"
04. gsql += "'" & MemName.Text & "',"
05. gsql += "'" & MemAge.Text & "',"
06. gsql += "'" & MemType.SelectedValue & "',"
07. gsql += "'" & MemStatus.SelectedValue & "',"
08. gsql += "'" & MemLevel.SelectedValue & "',"
09. gsql += "'" & MemTel.Text & "',"
10. gsql += "'" & AddNo.Text & "',"
11. gsql += "'" & SubdistrictNo.Text & "',"
12. gsql += "'" & SubdistrictName.SelectedItem & "',"
13. gsql += "'" & District.SelectedItem & "',"
14. gsql += "'" & Province.SelectedItem & "',"
15. gsql += "'" & DateTimePicker1.Value.ToString( "s" ) & "',"
16. gsql += "'" & gender & "',"
17. gsql += "'" & Subdistrict.SelectedItem & "',"
18. gsql += "'" & IncomeStatus.SelectedValue & "',"
19. gsql += "'" & MemPic.Text & "')"
20. With cmd
21. .CommandType = CommandType.Text
22. .CommandText = gsql
23. .Connection = con
24. .ExecuteNonQuery()
25. End With
ตรงนี้ เพิ่มผ่าน ไม่มีปัญหาอะไรค่ะ แต่เมื่อจะเพิ่มตารางที่สอง โดย ค่าที่จะเพิ่มเข้าไปในตาราง คือ ค่า MemId ที่เพิ่มเข้าไปด้านบน
โดยลองทั้ง 3 วิธีแล้ว แต่เกิดปัญหานี้ขึ้นค่ะ

ตามโค๊ดด้านล่าง
Code (VB.NET)
01. Try
02. gsql = "insert into beneficiary (MemId,B1In,B1Name,B1Tel,B1Re,B2In,B2Name,B2Tel,B2Re,B3In,B3Name,B3Tel,B3Re) values ("
03. gsql += "'" & "select max(MemId) from member" & "',"
04. gsql += "'" & B1In.Text & "',"
05. gsql += "'" & B1Name.Text & "',"
06. gsql += "'" & B1Tel.Text & "',"
07. gsql += "'" & B1Re.SelectedItem & "',"
08. gsql += "'" & B2In.Text & "',"
09. gsql += "'" & B2Name.Text & "',"
10. gsql += "'" & B2Tel.Text & "',"
11. gsql += "'" & B2Re.SelectedItem & "',"
12. gsql += "'" & B3In.Text & "',"
13. gsql += "'" & B3Name.Text & "',"
14. gsql += "'" & B3Tel.Text & "',"
15. gsql += "'" & B3Re.SelectedItem & "')"
16. With cmd
17. .CommandType = CommandType.Text
18. .CommandText = gsql
19. .Connection = con
20. .ExecuteNonQuery()
21. End With
22. Catch ex As System.Data.SqlClient.SqlException
23. MsgBox(ex.Message, , "Sql Exception" )
24. End Try
เราต้องแก้ไขตรงไหนคะ โดยหลักก็คือ เราจะเพิ่มข้อมูลเข้าไปที่ตาราง member ก่อน แล้วเพิ่มข้อมูลเข้าตาราง beneficiary
โดยที่ค่า MemId ตรงกัน พยายามมาหลายวิธีแล้วรบกวนด้วยค่ะ ขอบคุณค่ะ
Tag : .NET, Ms SQL Server 2008, VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2015-09-02 07:47:46 |
By :
newbies |
View :
895 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code
"insert into member (MemIn,MemName,MemAge,MemTypeId,MemStatusId,MemLevelId,MemTel,Addno,SubdistrictNo,SubdistrictName,District,Province,JoinDate,Gender,Subdistrict,IncomeStatusId,Mempic) values ("
select max(MemId) from member
จากข้อมูลข้างต้น member ไม่มี MemId นี่ครับ
|
 |
 |
 |
 |
Date :
2015-09-02 08:17:48 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
gsql += "'" & "select max(MemId) from member" & "'," ตรงบรรทัดนี้ครับ เอา ' ' ออกครับ จะได้เป็น
gsql += "" & "select max(MemId) from member" & ","
เหตุผลเพราะว่า Column เป็น Int ไม่ควรจะเอา '' ใส่ให้คับ ใช้เฉพาะ string เท่านั้น และ Column นี้ต้องไม่กำหนดเป็น AI ด้วยนะครับ เฉพาะตารางนี้นะ ตาราง member ไม่เกี่ยว
|
 |
 |
 |
 |
Date :
2015-09-02 08:55:58 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ยากครับ วิธีหา Error ตัวนี้ เดาง่ายนิดเดียง เกิดจากคำสั่่ง SQL เขียนผิดครับ ให้ลองทำตามขั้นตอนนี้ครับ
Debug โปรแกรม แล้ว Copy คำสั่ง gsql ไปรันใน SQL ดูครับ หรือ ถ้าไม่ได้จริงๆ Copy คำสั่งทั้งหมดที่ DEbug มาแปะครับ เด๋วจะดูให้
|
 |
 |
 |
 |
Date :
2015-09-02 16:04:55 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วค่ะ ลองใช้วิธีนี้แทน
Code (VB.NET)
01. gsql = "select max(MemId) from member"
02. cmd = New System.Data.SqlClient.SqlCommand
03. cmd.Connection = con
04. cmd.CommandType = CommandType.Text
05. cmd.CommandText = gsql
06. myscalar = cmd.ExecuteScalar
07. MemId.Text = myscalar.ToString
08.
09. gsql = "insert into beneficiary values ("
10. gsql += "'" & MemId.Text & "',"
11. gsql += "'" & B1In.Text & "',"
12. gsql += "'" & B1Name.Text & "',"
13. gsql += "'" & B1Tel.Text & "',"
14. gsql += "'" & B1Re.Text & "',"
15. gsql += "'" & B2In.Text & "',"
16. gsql += "'" & B2Name.Text & "',"
17. gsql += "'" & B2Tel.Text & "',"
18. gsql += "'" & B2Re.Text & "',"
19. gsql += "'" & B3In.Text & "',"
20. gsql += "'" & B3Name.Text & "',"
21. gsql += "'" & B3Tel.Text & "',"
22. gsql += "'" & B3Re.Text & "')"
23. With cmd
24. .CommandType = CommandType.Text
25. .CommandText = gsql
26. .Connection = con
27. .ExecuteNonQuery()
28. End With
ขอบคุณ คุณ Freedom และคุณ TOR_CHEMISTRY นะคะ
|
 |
 |
 |
 |
Date :
2015-09-02 16:27:18 |
By :
newbies |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|