对象数据源麻烦
我申请的公司给了我一个小项目。他们想要一个使用 asp.net GridView
、FormView
和带有 DataSet (xsd) 文件的 ObjectDataSource
的小型应用程序。我一直在做 3 层应用程序,但以不同的方式,我将我的 DataAccess 层作为 ac Sharp 文件,其中包含接受参数和返回数据表等的方法。我的业务层是另一个类,其中的静态方法返回业务对象和集合,利用DAL 类。现在这个 ObjectDataSource
是我不太喜欢的东西,它生成了一些我什至看不到的代码?
我可以使应用程序工作到某个点(90%)。另外10%就是我的问题。我需要按名称功能进行搜索。有两个 ObjectDataSource 1 和 2。ObjectDatasource1
仅在第一次加载时从表中获取每条记录。当单击搜索按钮时,我将 gridview 的数据源设置为第二个 ObjectDataSource
,它有一个名为 GetDataByNameSearch
的方法,该方法应该接受参数(全部由 wizzzardz 定义)和参数源是控件(TextBox.Text
)。虽然我的 FormView
在从 QueryString
获取参数时工作正常,但此搜索不会返回任何内容。搜索语句如下:
SELECT Birthday, CreatedAt, ID, Name, Surname
FROM Users
WHERE (Name LIKE '%@name%') OR
(Surname LIKE '%@name%')
有关如何使用这些 ObjectDataSources 的任何想法,并使生活更轻松(!)
I have been given a small project by the company I have applied for. They want a small application using asp.net GridView
, FormView
and an ObjectDataSource
with a DataSet (xsd) file. I have been doing 3-tier applications but in a different way, I have my DataAccess layer as a c sharp file with methods accepting params and returning datatables etc. My business layer is another class with static methods returning business objects and collections making use of the DAL class. Now this ObjectDataSource
is sth i did not really like, its generating some code that i can't even see where?
I could make the application work upto some point(90%). The other 10% is what my question about. I need to make a search by name functionality. There are two ObjectDataSources 1 and 2. ObjectDatasource1
just gets every record from the table on the first load. When search button cliked I set the datasource of gridview to the second ObjectDataSource
which has a method called GetDataByNameSearch
that is supposed to accept a parameter (all defined by wizzzardz) and parameter source is control (TextBox.Text
). While my FormView
works fine where it gets its parameter from QueryString
, this search returns nothing. Search statement is as follows:
SELECT Birthday, CreatedAt, ID, Name, Surname
FROM Users
WHERE (Name LIKE '%@name%') OR
(Surname LIKE '%@name%')
Any idea about how these ObjectDataSources are supposed to be used, and make life easier(!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有代码示例,很难说清楚,但我确实注意到您对 SQL 参数的使用有点不寻常。
您有:
我不确定 SQL '@' 参数在周围有语音标记时是否有效。我认为上面的示例只会导致查询中使用文字字符串“%@name%”,这就是您可能得不到结果的原因。
SQL 参数通常这样使用:
... 但是当然你会丢失“%”通配符。您可以在将它们传递给查询之前将它们直接添加到参数字符串中。如果这不可能,也许尝试这个版本:
Without code samples its hard to tell, but I did notice that your use of SQL parameters is a bit unusual.
You have:
I'm not sure if SQL '@' parameters will work when there are speechmarks around them. I think the above example will just result in the literal string '%@name%' being used in the query, which is why you might be getting no results.
SQL Parameters are usually used like this:
... but of course then you will lose the '%' wildcards. You might be able to add them directly into the parameter strings before they are passed to the query. If that is not possible, maybe try this version: