Trying to access a SQL Server database to display a single row in c# -
i'm trying access sql server database , display single row using c#. i'm having trouble figuring out how this. please help. thank you.
public form1() { initializecomponent(); string connectionstring = "data source=localhost \\sqlexpress;initial catalog=mmabooks;" + "integrated security=true"; sqlconnection connection = new sqlconnection(connectionstring); string selectstatement = "select productcode " + "from products " + "where productcode = @productcode"; sqlcommand selectcommand = new sqlcommand(selectstatement, connection); selectcommand.parameters.add("@productcode"); connection.open(); sqldatareader reader = selectcommand.executereader(commandbehavior.singlerow); reader.read(); txtdisplay.text = reader["productcode"].tostring(); }
looks passing parameter productcode query , returning same result. please make scenario clear. if need pass product code , same table, can :
public form1() { initializecomponent(); string connectionstring = "data source=localhost \\sqlexpress;initial catalog=mmabooks;" + "integrated security=true"; object returnvalue; sqlconnection connection = new sqlconnection(connectionstring); string selectstatement = "select top 1 productcode " + "from products " + "where productcode = @productcode"; sqlcommand selectcommand = new sqlcommand(selectstatement, connection); selectcommand.commandtype = commandtype.text; connection.open(); returnvalue = selectcommand.executescalar(); connection.close(); txtdisplay.text = returnvalue.tostring(); }
Comments
Post a Comment