本文介绍了SQL:编写更简洁的paramaterized SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试使用参数化SQL。我已经开始工作, 但是现在我的一些查询看起来不必要了。例如: strSQL =" IF NOT EXISTS(SELECT * FROM We_Link_SiteMenus_To_DirectoryContacts WHERE contactID =?AND pageID =?)INSERT INTO We_Link_SiteMenus_To_DirectoryContacts(contactID,pageID)VALUES(?,?)" objCommand.Parameters.Add(" @ contactID", System.Data.OleDb.OleDbType.Numeric).Value = Request(" hid_contactIDadd_"& contactsCount) objCommand.Parameters.Add(" @ pageID" , System.Data.OleDb.OleDbType.Numeric).Value = Request.QueryString(" pageID") objCommand.Parameters.Add(" @ contactID" , System.Data.OleDb.OleDbType.Numeric).Value = Request(" hid_contactIDadd_"& contactsCount) objCommand。 Parameters.Add(" @ pageID", System.Data.OleDb.OleDbType.Numeric).Value = Request.QueryString(" pageID") 如你所见,我是代表两次吃这两个值。是否有更简洁的 方式传递这些参数? -DarrelI''m trying to get the hang of using parameterized SQL. I''ve gotten to work,but now some of my queries seem unecessarily long. For instance:strSQL = "IF NOT EXISTS(SELECT * FROM We_Link_SiteMenus_To_DirectoryContactsWHERE contactID = ? AND pageID = ?) INSERT INTOWe_Link_SiteMenus_To_DirectoryContacts (contactID, pageID) VALUES (?, ?)"objCommand.Parameters.Add("@contactID",System.Data.OleDb.OleDbType.Numeric).Value = Request("hid_contactIDadd_" &contactsCount)objCommand.Parameters.Add("@pageID",System.Data.OleDb.OleDbType.Numeric).Value = Request.QueryString("pageID")objCommand.Parameters.Add("@contactID",System.Data.OleDb.OleDbType.Numeric).Value = Request("hid_contactIDadd_" &contactsCount)objCommand.Parameters.Add("@pageID",System.Data.OleDb.OleDbType.Numeric).Value = Request.QueryString("pageID")As you can see, I''m repeating the two values twice. Is there a less verboseway to pass those parameters?-Darrel推荐答案 strSQL =" IF NOT EXISTS(SELECT * FROM e_Link_SiteMenus_To_DirectoryContacts WHERE contactID = @contactID AND pageID = @pageID)INSERT INTO We_Link_SiteMenus_To_DirectoryContacts(contactID,pageID)VALUES (@ contactID,@ pageID)"strSQL = "IF NOT EXISTS(SELECT * FROM e_Link_SiteMenus_To_DirectoryContactsWHERE contactID = @contactID AND pageID = @pageID) INSERT INTOWe_Link_SiteMenus_To_DirectoryContacts (contactID, pageID) VALUES(@contactID, @pageID)" 我一直看到这个用过在示例中,但我从来没有能够使用 '''''语法来使用MSSql。事实上,人们已经说'''''是我需要使用的价格。 我会试一试,虽然; o) -DarrelI''ve always seen this used in examples, but I''ve never been able to get the''@'' syntax to work with MSSql. In fact, folks have said that the ''?'' is whatI need to use.I''ll give it a shot, though ;o)-Darrel 我一直看到这个在例子中使用,但我从来没能得到 '@''语法与MSSql一起使用。事实上,人们已经说'''''是我需要使用的。 我会试一试; o) -Darrel I''ve always seen this used in examples, but I''ve never been able to get the ''@'' syntax to work with MSSql. In fact, folks have said that the ''?'' is what I need to use. I''ll give it a shot, though ;o) -Darrel 这篇关于SQL:编写更简洁的paramaterized SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 08:56