|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Add new record to databaseprogram is not adding any record to database. Hot to fix it? This is my code: OleDbDataAdapter odbAdapt; DataSet dsObject= new DataSet(); DataTable dtObject; string SQL; string ConnStr; int BrojSlogova; ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=c:/dbase/Proba.mdb"; SQL = "SELECT * FROM Names" OleDbConnection odbconn = new OleDbConnection(); odbconn.ConnectionString = ConnStr; odbconn.Open(); odbAdapt = new OleDbDataAdapter (SQL, ConnStr); dsObject = new DataSet(); odbAdapt.Fill(dsObject); dtObject = dsObject.Tables[0]; // Then add new record DataRow WorkRow; WorkRow= dtObject.NewRow(); WorkRow["FirstName"] = "George"; WorkRow["LastName"] = "Bush"; dtObject.Rows.Add(WorkRow); This doesn't work! Firstly, if you don't have an insert method set in the DataAdapter, you
won't be able to use the DataAdapter to update (meaning insert, update or delete) the db. Secondly, when you add a row to a datatable, you only add it to the datatable object which is a memory representation of relationnal datas (not specific to a platform). You have to use the Update method of the DataAdapter and passed the modified datatable in order to apply updates to the DB. Thirdly, since .Net 2.0, TableAdapters are more accurates than DataAdapters. In order to help understanding ADO.Net concepts (especially disconnect working), I suggest you to follow this tutorial which is a very good one : http://www.asp.net/learn/dataaccess/default.aspx?tabid=63 Hope that helps "Marko" <markoma***@marko.com> a écrit dans le message de news: ekk1fc$39***@sunce.iskon.hr...Show quoteHide quote >I am trying to add new record in database. There is no compile error but >program is not adding any record to database. Hot to fix it? This is my >code: > > OleDbDataAdapter odbAdapt; > DataSet dsObject= new DataSet(); > DataTable dtObject; > string SQL; > string ConnStr; > int BrojSlogova; > > ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;data > source=c:/dbase/Proba.mdb"; > SQL = "SELECT * FROM Names" > OleDbConnection odbconn = new OleDbConnection(); > odbconn.ConnectionString = ConnStr; > odbconn.Open(); > odbAdapt = new OleDbDataAdapter (SQL, ConnStr); > dsObject = new DataSet(); > odbAdapt.Fill(dsObject); > dtObject = dsObject.Tables[0]; > > // Then add new record > > DataRow WorkRow; > WorkRow= dtObject.NewRow(); > WorkRow["FirstName"] = "George"; > WorkRow["LastName"] = "Bush"; > dtObject.Rows.Add(WorkRow); > > This doesn't work! > > > > >
How do you translate your WinForms app? (outsourcing localization)
How to recognize a Command Line parameter? Attaching multiple files of any type to a mail message? Looking for a simple explanation of how to walk through a dataset in .net 2.0 Need pre-built report administration application Its Urgent: I have export database into excel file if office not installed then it gives me error Where is the VS mru list file stored? how sql xml query to get empty element tag Create a Deployment Package! How to update .NET Development Environment to import a new namespa |
|||||||||||||||||||||||