How do I pass a custom SQL string or parameters to a tableadapter or somesuch in C# / Visual Studio 2010?
What I want to do is create 开发者_运维技巧a SQL statement dynamically OR create the WHERE clauses dynamically and pass them to some kind of method which fills a dataset.
In the Visual Studio 2010 'add query' wizard all you can do is explicitly define a hard-coded query that the call will execute.
What I want is something like tableName.get(String sql query). Something like that.
How do I define that, how do I execute my own SQL queries on the datasource?
I'm really lost with this, please ask if you need more information.
Many thanks, Thomas
xxx
i'm not very sure i completely understand your request, but maybe you can use the "dynamic" keyword instead of original definition. for example:
public dynamic mySqlDataAdapter ...; (you can use mySqlDataAdapter with all SqlDataAdapter functions. The syntax isn't verified at design-time.)
public dynamic myDataTable ...;
and you can call them:
mySqlDataAdapter.Update(myDataTable);
So you can use any DataAdapter from your DataSet with any DataTable.
For example , I used it in the following situation: I have e base form with methods for saving data, "save_data()" . In one of these methods I have to update the data, so I use:
mySqlDataAdapter.Update(myDataTable);
I use this form as base for other forms for updating data in application.
Further, when I make a form, based on the first form, I call "save_data()" methos, but every form has it's own strongly typed dataadapter and datatable. But with "dynamic keyword" the situation is solved.
HTH
Here's a really good VB.NET (shudder) example.
http://www.java2s.com/Code/VB/Database-ADO.net/UseParametersinyoursqlcommand.htm
OK, its for an insert statement, but swap that for your select statment.
Plenty of possible arguments that could arise after a good code review, but the point is, it shows how to open a sql connection, create a command on it, set parameters on that command and execute it, with the added bonus of some error handling.
Edit Here's a c# example populating a (shudder) dataset.
click me
精彩评论