sql - How to use DataAdapter to call a stored procedure in C# with variable parameters -
i calling following code in c# fill dataadapter given stored procedure "sp1_name". problem want call different stored procedures different parameters. (all sp's select) let's suppose stored procedure name "sp_somesp", works fine.
let's suppose stored procedure name "sp_somesp @month= 10, @year = 2010", doesn't work. throws exception cannot find stored procedure.
any solutions?
thanks!
//first connection - sp1 using (sqlconnection con = new sqlconnection(constr)) { using (sqlcommand cmd = new sqlcommand(sp1_name, con)) //sp1_name = name + parameters { cmd.commandtimeout = 3600; cmd.commandtype = commandtype.storedprocedure; using (sqldataadapter dataadapter = new sqldataadapter(cmd)) { dataadapter.fill(results2); } } }
first issue:
parameters in stored procedure shouldn't included along name
second issue:
having space in names of stored procedure isn't practice.
, code behind
using(sqlconnection con = new sqlconnection("your connection string here")) { sqlcommand cmd = new sqlcommand("sp_somename", con); cmd.commandtype = commandtype.storedprocedure; //the 2 codes after comment assign value parameters //have on stored procedure sql cmd.parameters.add("@month", sqldbtype.varchar).value = "somevalue"; cmd.parameters.add("@year", sqldbtype.varchar).value = "someyear"; sqldataadapter da = new sqldataadapter(cmd); sqldataset ds = new sqldataset(); da.fill(ds); //this put values select command //dataset named ds, reason fetch value db code behind foreach(datarow dr in ds.tables[0].rows) // run through dataset , values want it. { sometextbox.text = dr["month"].tostring(); //you should know code } }
Comments
Post a Comment