sql server - c# Local Database insert into doesn't work -


i tried many times insertion doesn't work! please me..

codes:

sqlconnection conn = new sqlconnection(@"data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\database1.mdf;integrated security=true"); conn.open(); sqlcommand cmd = new sqlcommand("insert mytbl(id, nav) values('yek','du')", conn); //yek , du examples //following command doesn't work, //"insert mytbl(id, nav) values('"+tb.text+"','"+tb2.text+"')" cmd.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. victorydatabase)

  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=victorydatabase;integrated security=true 

    and else exactly same before...

also see aaron bertrand's excellent blog post bad habits kick: using attachdbfilename more background info.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -