我尝试过: 我试过这个 *What i Prefer:- keep DBChild - C_ID to be PK (AutoNumber) - I really needs this on my project)- Keep P_ID = Relation / Reflection From P_ID of DBParent - Another Important Thing- Using Conditional "WHERE" as my project needs this to compare the value with UniqeID that i mentioned below.- God help me to find the answer, or at least Chuck Norris...What I have tried:I have tried thisTrydim UniqeID = 6 'Let's say this number as uniqe for nowDB.OpenConnection() 'Open ConnectionDB.SQLQuery = "INSERT INTO DBChild (C_Name,C_Code) VALUES (@Name,@Code) " & "SELECT * FROM DBParent WHERE P_ID = " & UniqeID & ";"DB.Command = New OleDbCommand(DB.SQLQuery, DB.Connection)With DB.Command .Parameters.AddWithValue("@Name", Txt_Snippet_Title.Text) .Parameters.AddWithValue("@Code", Txt_Editor_Tag.Text) .ExecuteNonQuery()End WithMsgBox("Record Saved!.", MsgBoxStyle.Information) 'Never Reach this Line...Catch ex As ExceptionMsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error " & GetCurrentMethod().Name & " !")DB.CloseConnection()FinallyDB.CloseConnection()End Try 但错误说:在SQL结束时缺少SemiColon(;)声明 当我尝试通过VB直接编辑/更改为DBChild(C_Name或C_Code)时错误说: 你无法添加或更改记录,因为表DBParent中需要相关记录 这几天就被困住了(很多虽然在网上冲浪,但仍然无法找到答案... 提前致谢。 J But Error Say : Missing SemiColon (;) at end of SQL Statementand When i try Edit/Change directly to DBChild (Both C_Name or C_Code) via VB then Error Say : "You Cannot Add or Change a record because related record is required in table DBParent"just Stuck by this over days (a lot surfing on internet, though) , but still can't found the answers...Thanks in advance.J推荐答案 查看您的查询: Take a look at your query:"INSERT INTO DBChild (C_Name,C_Code) VALUES (@Name,@Code) " & "SELECT * FROM DBParent WHERE P_ID = " & UniqeID & ";" 您将2个查询连接在一起。你不能这样做,因为 .ExecuteNonQuery()只接受 INSERT |更新|删除语句。 您必须在一个语句中使用 INSERT 并且 SELECT 在另一个( ExecuteReader )。 请参阅:如何:创建并执行返回行的SQL语句 [ ^ ] BTW:永远不要使用连接字符串作为查询! You did concatenate 2 queries in one. You can't do this , becuase .ExecuteNonQuery() accepts only INSERT | UPDATE | DELETE statements.You have to use INSERT in one statement and SELECT in the other (ExecuteReader).See: How to: Create and Execute an SQL Statement that Returns Rows[^] BTW: Never use concatenate strings as a query!"SELECT * FROM DBParent WHERE P_ID = " & UniqeID & "; 这是最大的原因 SQL注入 [ ^ ]! 好吧最后我想出了我自己!!,就像我以前想的那样,这个问题应该用一种简单的方式解决了。 对于那些谁在这里寻找相同的答案是我的工作解决方案 Ok Finally I Figure it Out Myself!!, just like i thought before, this problem should be resolved with an simple way somehow :)For those who looking the same answers here's my Working SolutionsTrydim UniqeID = 6 'Let's say this number as uniqe for nowDB.OpenConnection() 'Open ConnectionDB.SQLQuery = "INSERT INTO DBChild (P_ID, C_Name, C_Code) VALUES (@PID, @Name, @Code)" DB.Command = New OleDbCommand(DB.SQLQuery, DB.Connection)With DB.Command .Parameters.AddWithValue("@PID", UniqeID) 'This is The Key! .Parameters.AddWithValue("@Name", Txt_Snippet_Title.Text) .Parameters.AddWithValue("@Code", Txt_Editor_Tag.Text) .ExecuteNonQuery()End WithMsgBox("Record Saved!.", MsgBoxStyle.Information) 'Finally! :)Catch ex As ExceptionMsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error " & GetCurrentMethod().Name & " !")DB.CloseConnection()FinallyDB.CloseConnection()End Try 因此,在将新记录添加到DBChild时,只需确保P_ID值在DBParent中存在,并且只是使用INSERT INTO只有我们可以把它包起来,就是全部! 感谢 Maciej Los 为我照亮我的方式和 CPallini 指明方向! \ m / 干杯, J So, just make sure the "P_ID" value is Exist in DBParent when adding the new Record into DBChild, and yes just using "INSERT INTO" only we can wrap this up, that's All!Thanks before to Maciej Los for brighten my way and CPallini for pointing the way! \ m /Cheers,J 这篇关于救命! ,Vb OLEDB INSERT记录与表关系!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 09:30