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
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.
victorydatabase
)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
Post a Comment