c# - SqlCeDataReader Read() returning false -
i working on card game uses database store , retrieve cards. unfortunately having trouble retrieving data database.
program.cs
using system; using system.collections.generic; using system.linq; using system.text; namespace cardgame { class program { static void main(string[] args) { card c = new card(); card d; c.name = "testcard"; c.pack = "pack"; c.value = 5; c.color = colors.green; c.description = "describing stuff"; //database.createcardtable(); (already created) database.insertcard(c); d = database.retrievecard("testcard"); console.writeline(d.name); console.readline(); } } }
database.cs
using system; using system.collections.generic; using system.linq; using system.text; using system.data.sqlserverce; using system.data; using system.data.sqlclient; namespace cardgame { class database { public static void createcardtable() { string sqlstr; sqlceconnection cardconn = new sqlceconnection(properties.settings.default.cardsconnectionstring); sqlstr = "create table data " + "(name nvarchar(50), " + "pack nvarchar(50), " + "value int, " + "color int," + "description nvarchar(200))"; sqlcecommand cardcomm = new sqlcecommand(sqlstr, cardconn); try { cardconn.open(); cardcomm.executenonquery(); console.writeline("tables created successfully!"); } catch (system.exception ex) { console.writeline(ex.tostring()); console.writeline("error creating tables."); } { if (cardconn.state == connectionstate.open) { cardconn.close(); } } } public static void insertcard(card c) { sqlceconnection cardconn = new sqlceconnection(properties.settings.default.cardsconnectionstring); try { cardconn.open(); sqlcecommand insertcommand = new sqlcecommand("insert data values(@name, @pack, @value, @color, @description)", cardconn); insertcommand.parameters.add("name", c.name); insertcommand.parameters.add("pack", c.pack); insertcommand.parameters.add("value", c.value); insertcommand.parameters.add("color", c.color); insertcommand.parameters.add("description", c.description); console.writeline("card inserted successfully"); } catch { console.writeline("some kind of error."); } { if (cardconn.state == connectionstate.open) { cardconn.close(); } } } public static card retrievecard(string name) { card c = new card(); sqlceconnection cardconn = new sqlceconnection(properties.settings.default.cardsconnectionstring); try { cardconn.open(); sqlcecommand retrievecommand = new sqlcecommand("select * data name=@name", cardconn); retrievecommand.parameters.add("name", name); sqlcedatareader reader = retrievecommand.executereader(); while (reader.read()) { console.writeline("reader: " + reader["name"].tostring()); c.name = (string)reader["name"]; //read rest } } catch (exception e) { console.writeline(e.tostring()); } return c; } public void createaccountdb(string dbname) { } } }
when run insert card success message (at end of try statement). when database.retrievecard("testcard");
reaches line while (reader.read())
program not reach code inside of while statement. how can fix code can read cards database?
i built command insert card database never executed it. nothing ever inserted when go retrieve card return nothing because there nothing in database!
i changed
try { cardconn.open(); sqlcecommand insertcommand = new sqlcecommand("insert data values(@name, @pack, @value, @color, @description)", cardconn); insertcommand.parameters.add("name", c.name); insertcommand.parameters.add("pack", c.pack); insertcommand.parameters.add("value", c.value); insertcommand.parameters.add("color", c.color); insertcommand.parameters.add("description", c.description); console.writeline("card inserted successfully"); }
to
try { cardconn.open(); sqlcecommand insertcommand = new sqlcecommand("insert data values(@name, @pack, @value, @color, @description)", cardconn); insertcommand.parameters.add("name", c.name); insertcommand.parameters.add("pack", c.pack); insertcommand.parameters.add("value", (int)c.value); insertcommand.parameters.add("color", (int)c.color); insertcommand.parameters.add("description", c.description); insertcommand.executenonquery(); console.writeline("card inserted successfully"); }
Comments
Post a Comment