c# - Why inserting into the table not working? -


i wonder why piece of code not working?

sqlcommand comand = new sqlcommand(); //string myconnectionstring = "data source=.\sqlexpress;initial catalog=try;integrated security=true;pooling=false"; sqlconnection conn = new sqlconnection(@"server=.\sqlexpress;attachdbfilename=|datadirectory|\mydatabase.mdf;integrated security=true;user instance=true"); comand.connection = conn; conn.open();  comand.commandtext = "insert tableproduct (productid,productname) values ('1','m')";  comand.executenonquery(); conn.close(); 

the whole user instance , attachdbfilename= approach flawed - @ best! when running app in visual studio, copying around .mdf file (from app_data directory output directory - typically .\bin\debug - app runs) , most likely, insert works fine - you're looking @ wrong .mdf file in end!

if want stick approach, try putting breakpoint on myconnection.close() call - , inspect .mdf file sql server mgmt studio express - i'm data there.

the real solution in opinion

  1. install sql server express (and you've done anyway)

  2. install sql server management studio express

  3. create database in ssms express, give logical name (e.g. mydatabase)

  4. connect using logical database name (given when create on server) - , don't mess around physical database files , user instances. in case, connection string like:

    data source=.\\sqlexpress;database=mydatabase;integrated security=true 

    and else exactly same before...


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -