bind structured arraylist to database
I have an arraylist
ArrayList backupSpecList = new ArrayList();
And a structure:
public struct BackupSpecEntry
{
//for Multiple BACKUP_SPEC_EXCLUDE_PATHS
public string path;
public string inclExcl;
public byte inclExclFlags;
public bool indexContents;
public int serverBackupSpecId;
public int freq;
public int retention;
//for Multiple BACKUP_SPEC_EXCLUDE_FILE_NAMES
public BackupSpecEntry(string Path, string InclExcl, byte InclExclFlags, bool IndexContents,
int ServerBackupSpecId, int Freq, int Retention)
{
path = Path;
inclExcl = InclExcl;
inclExclFlags = InclExclFlags;
indexCont开发者_如何学Pythonents = IndexContents;
serverBackupSpecId = ServerBackupSpecId;
freq = Freq;
retention = Retention;
}
}
I put this structure in the arraylist and send it to the next page as a session
Session["BackupSpecList"] = backupSpecList;
I retrieve this on the next page as
ArrayList jaja = (ArrayList)Session["BackupSpecList"];
All this is happening perfect, but I need help to put this in the database. Can someone please guide me? Thanks.
Assuming you want to insert or update your database with the contents of your struct, then ADO.NET is a good place to start. Here is a good tutorial that walks you through the basics of reading, updating and inserting against a SQL Server database. There is also the official MSDN site for more in depth info.
Using .NET 3.5 you have the option of using the Entity Framework, or LINQ to SQL . I recommend the latter for a beginner. This way you get all the database plumbing done for you, all you have to do is bind and enjoy. Please note that you don't have to know much at all about LINQ to use the infrastructure.
The normal route to using LINQ to SQL
is to create your database entities first, and let Visual Studio generate objects for you based on them. Here you would create a BackupSpecEntry
table, and follow one of these tutorials for setting up data binding to LINQ to SQL Classes:
- Data Binding using LINQ to SQL in C#
- LINQ to SQL Databinding to ASP.NET
- LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
I have no guarantee of the quality of the above articles (except for ScottGu's). I just quickly picked them out of a search because the looked appropriate.
To add your own functionality to the generated BackupSpecEntry
class, you can derive your own class from the generated one, or use a partial class. I recommend using partial classes over inheritance whenever possible, because that is how Microsoft intend us to extend the generated classes.
What you need is called a data-persistence layer. In .NET the best way to access data stored in Relational Database Management Systems (e.g. SQL Server) is ADO.NET. It provides the low-level nuts and bolts to do so.
All Kinds of data-persistence layers work with ADO.NET internally, but provide abstractions on top of it.
From the phrasing of your question (your using objects to represent you applications data) I suggest you use a Data Mapper. Such a Data Mapper maps between your objects and the database. Since you probably use an RDBMS you'll need an Object-Relational-Mapper (ORM).
There are several options available. Since you use .NET Framework 3.5 you can use Linq-To-Sql or the Entity-Framework that are both provided by Microsoft and have good IDE integration. Other options are NHibernate etc. There is lots of information about that here on SO.
精彩评论