Cannot Insert data from C# into SQL Server -


i want insert data .mdf sql server file, when want see data insert. wasn't happen. use local database work.

private sqlconnection connection; private sqlcommand command; private string sql;  private void frmnewemp_load(object sender, eventargs e) {     string constr = @"server=(localdb)\v11.0;attachdbfilename=|datadirectory|\payrolldatabase.mdf;";      connection = new sqlconnection(constr);      if (connection.state == connectionstate.closed)     {        connection.open();     } }  private void insertdata() {     sql = @"insert employee(eid, ename, eadd, essid, etel, edatesub, esalary) values(@id, @name, @add, @ssid, @tel, @date, @sal)";     command = new sqlcommand(sql, connection);      command.parameters.clear();     command.commandtext = sql;      addparamtervalues();      messagebox.show("add complete"); } 

you not executing command anywhere. (assuming have correctly attached parameters)

you need

command.executenonquery(); 

and need open connection database.

just noticed opening connection on load , not closing it. not best way db stuff, open database connection late possible , close possible should rule.

consider enclosing connection object in usingstatement (like did command object) , use connection command


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -