arraylist to a table - c#

I have an arraylist called backuplist.
this arraylist has structures in it.
So what i need to do is transfer this arraylist in a table and then store this table in my SQL database.
Anybody with ideas as to what i should do..??
Even if it is a different way to do this please let me know.
Thanks

If you are using VS2008 (tags), you should ideally use List<T>, not ArrayList. You can convert from a List<T> to a DataTable like so; then just use a SqlDataAdapter or SqlBulkCopy to get the data into the database.

This isn't a complete solution, but I thought I'd point you in the right direction. The problem is I don't really know your application, and your experience is limited, so it's going to be a stab in the dark.
Anyway, here are a couple of resources to get you started:
Converting Custom Collections To and From DataTable
http://blog.lozanotek.com/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx
Inserting New Records into a Database
http://msdn.microsoft.com/en-us/library/ms233812(VS.80).aspx

I'd agree on using a strongly typed list as Marc suggested. Another option to getting those into the database would be to plow through with a foreach (on either your list or array), and use the structure properties as parameters to an insert stored procedure.
We do this all the time in our app, where we might have a List coming from a business component and being tossed to the data layer, where we'll loop through, do any necesary manipulation, then run our update SP on each row.
Let me know if you need a snippet.
-Bob

I've been using an ArrayList returned by MySQL so it's populated with column names and types etc.
ArrayList list = new ArrayList();
// Add Items to list
DataTable table = new DataTable();
table.Load(list);

Related

How to handle large datatables c# and update database

Good day.
I am asking for a bit of advise on what other experience has been and pitfalls ect. I am a SQL developer but needing to write a front end using c#.
I am returning a query from a MSSQL database via a stored procedure and putting it into a Datatable. There are about 140k rows in the result set. I am using standard calls with a datareader to return the resultset. No binding.
What I would like to do is return parts of the datatable to datagrid on a form and allow a user to manipulate the data in the grid and save back to the datatable then collect the next part of the datatable and manipulate that. I don't want to to pull things into a datatable in segments as I need to update calculations on the entire datatable when a change is made.
And then finally save the changes back to the database, when done.
If anyone can point me to a the best and most efficient way it would be greatly appreciated.
Thank you in advance
Scott

Typed dataset correct usage

If I understand this right, do I have to use the table adapters to get data into my typed data set, I can't just create my strongly typed data-set and have the data autoload? (im also using a .net 3.5 project in VS2012)
for example, I have to do this to get data (If I do it this way, I get data)
var a = new EvtDataSetTableAdapters.tblFileTableAdapter();
a.GetData();
versus just doing this, (if I do it this way, I get nothing... and I could understand if its lazy loading...??)
EvtDataSet o = new EvtDataSet();
var r = o.tblFile.Select();
All DataSets (type and untyped) are database-agnostic, ie any DataTable can be filled from Oracle just as easy as from MS-Sql. The DataSet has no knowledge of schema or connection strings.
You need an Adapter to read to/from a backing store.
(And DataTable.Select() is probably from Linq-to-Datasets).
The much maligned, much misunderstood strongly typed dataset!
Typically yes you would use a TableAdapter to load the data, and to perform updates.
Using the designer you would add parameter queries to the table adapter to support the operations your program requires eg select * from customers where customerid = #customerid
Call this FillbyCustomerid.
Then you would pull the data for the selected customer using the TableAdapter by something along the lines of:
dim ta as new dscustomerstableadapters.customertableadapter
dim ds as new dsCustomers
ta.fillbycustomerid (ds.customers, ourid)
BEST usage of typed dataset: Ignore, never use. Go OTM instead. Use LINQ. Datasets - typed and untyped - where bad when they were in .NET 1.0, since then even MS has realized alternatives. Not used one in 10 years, not going to use one.
Exception: Reporting applications where the SQL is "entered externally", so you basically just Need a generic data Container.
Use EntityFramework or one of the tons of alternatives.

IS there a short way to insert dataset into a new MS Access table?

I'm consuming a 3rd-party web service that outputs a Dataset (from XML). I'd like to create a new table in my local MS Access database that contains the DataSet data.
Is there a simple way to hand-off the DataSet to a .net object and ask it to "create a table from this"?
I know that we can use different parts of ADO to extract schema, build commands, insert rows, etc. I figured there has to be a simpler way.
The only two ways I know of are to
Walk through the DataSet field by
field and generate a DDL instruction
(which is valid for MS-Access)
Add a reference to ADOX, create a new table (with columns) and append the new table to the ADOX catalog. More info here. But again you are walking throught the dataset table field by field.
I haven't provided much detail on either of these approaches since I don't think they match what you've specified.
It seems you are looking for a quicker way than either of those so I guess the answer to your question is no.

How to save an arbitrary DataTable to a new SQL Table

Suppose I have an ADO.NET DataTable that I was to 'persist' by saving it to a new table in a SQL Server database - is there a fast way of doing this?
I realise I could write code generating the DDL for the 'CREATE TABLE' statement by looping through the DataColumns collection and working out the right type mappings and so on ... but I'm wondering if there is an existing method to do this, or a framework someone has written?
(NB: I need to be able to handle arbitrary columns, nothing too fancy like blobs; just common column types like strings, numbers, guids and dates. The program won't know what the columns in the DataTable are until run-time so they can't be hard-coded.)
ADO.net cannot create tables in SQL Server directly, however, SMO can do this with the .Create method of the Table class. Unfortunately, there is no built-in way to use a DataTable to define an SMO Table object.
Fortunately, Nick Tompson wrote just such a DataTable-to-SMO.Table routine back in 2006. It is posted as one of the replies to this MSDN forums topic http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/4929a0a8-0137-45f6-86e8-d11e220048c3/ (edit: I can make hyperlinks now).
Note also, the reply post that shows how to add SQLBulkCopy to it.
If the table exists, you can use SqlBulkCopy (which will accept a DataTable) to get the data into the table in the fastest possible way (much faster than via an adapter). I don't think it will create the table though. You might have to write the DDL yourself, or find some existing code to loop over the DataTable.Columns to do it.
I think this post can help
I might be easier to store your DataTable as XML. just create a table with an XML column.

Loading a lookup table from a database into a C# program - data structure?

I have a table full of id's,categories and weights that I need to reference in my program as I read in records that contain those categories. What is the most efficient method to read those from a database and put into a structure that I can reference?
The ID's (and possibly the names) would be unique
Data might look like:
ID,Category,Weight
1,Assignment,5
2,Test,10
3,Quiz,5
4,Review,3
Your best bet is to read in your table using a DataReader, and put each row into an object containing Category and Weight, then each object into a Dictionary.
If you're using a later version of .NET, you could always use Linq to just grab that data for you.
If you want to avoid a database hit to fetch static data, you can hard-code the values into a common class in your solution. A Dictionary collection would work fine here too.
The trade off of course is; 2 locations to manage for any possible future changes.

Categories

Resources