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

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -