this some code that i got from expert F# chapter 15.
let dataAdapter = new SqlDataAdapter()
let buildDataSet conn queryString =
dataAdapter.SelectCommand <- new SqlCommand(queryString,conn)
let dataSet = new DataSet()
let _ = new SqlCommandBuilder(dataAdapter)
dataAdapter.Fill(dataSet) |> ignore
dataSet
let dataSet =
buildDataSet conn "SELECT * FROM Employees"
if i want to delete data or insert data should i make a new dataSet?
The idea behind using
DataSetfor working with data is that theDataSetrepresents a local in-memory copy of some part of the database. To modify the database, you would modify the data stored in theDataSet(in memory) and then submit the changes to the database using theSqlDataAdapter.The snippet you posted uses the
Fillmethod to copy data from database toDataSet. Submitting changes from memory to database (the other direction) is done using theUpdatemethod.You can find some C# examples in this MSDN article. It shouldn’t be difficult to translate them to F#.
If you don’t need to keep data in memory, it may be easier to use
SqlCommanddirectly. Using this type, you can create SQL command and immediately execute it on the SQL database (without copying any data to memory). You can find some examples in my recent blog post. The blog shows how to read data usingExecuteReadermethod, but you can useExecuteNonQuerymethod to just run command (without reading any results from the SQL server).