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
install sql server express (and you've done anyway)
install sql server management studio express
create database in ssms express, give logical name (e.g.
mydatabase
)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
Post a Comment