.net - How use WHERE in SqlDataAdapter in C# -


how use where in sqldataadapter in c#?

i want name in textbox , use @ query wont work .

sqlconnection sqlconnection = new sqlconnection("server=behnam\\accounting;initial catalog=accounting;integrated security=true");  datatable dt = new datatable(); string _search_name = txt_search.text;  sqldataadapter sda = new sqldataadapter("select dbo.tbl_user.field1,dbo.tbl_user.field2 tbl_user dbo.tbl_user.name=_search_name ", sqlconnection);  sda.fill(dt);  datagridview1.datasource = dt; 

prepare command text , use parameter value of search. use command text initialize new sqlcommand. fill parameter value addwithvalue , pass sqlcommand constructor of sqldataadapter.

string cmdtext = "select dbo.tbl_user.field1,dbo.tbl_user.field2 " +                    "from tbl_user dbo.tbl_user.name=@search_name" sqlcommand cmd = new sqlcommand(cmdtext, sqlconnection); cmd.parameters.addwithvalue("@search_name", _search_name); sqldataadapter sda = new sqldataadapter(cmd); 

the sqldataadapter store command selectcommand property , use passed in sqlcommand execute query retrieve records database.

keep in mind addwithvalue shortcut drawbacks. example pass string nvarchar parameter size equal actual lenght of variable. reduces performance of sql server optimizer.

this enlightening article on issue


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -