SQL Query depending on a Winform control. C#, VS2010
I new to C#, and trying to make a winform app, i am having problem to search a Database depending upon the text entered in a textbox.
I have added the gridview, when adding a new query, what should be the WHERE clause like to be able to use data from Winform controls?
Also do i need to change the fill table call ?
And is there any Good books that may cover a good amount of simila开发者_运维知识库r topic,i.e,working with databases in c# winforms?
Thank you all, in advance..
You should look into making a stored procedure for your query and then you supply the value from your textbox.Text field into your stored procedure.
This other question might help you with the C# syntax: How to execute a stored procedure within C# program
And assuming you're trying out SQL Server Express, this should help you setup the query as a stored procedure: http://msdn.microsoft.com/en-us/library/ms345415.aspx
Otherwise if you're going for basic SQL in the winForm, you likely want to run the query based on the user pressing a button rather than the "TextChanged" event on the TextBox:
String queryStr = "SELECT * "+
"FROM my_table "+
String.Format("WHERE my_value = '{0}'", TextBox1.Text);
It may be worth looking into linqto sql or linq to entities. Then you could do it somewhat like
var q = from s in recs.Record
where s == txtBox.Text
select s;
There are plenty of online resources for linq and it sort of creates a data access layer for you.
精彩评论