Click here to Skip to main content
15,900,725 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: am i a programmer or not Pin
Eddy Vluggen9-Apr-09 10:03
professionalEddy Vluggen9-Apr-09 10:03 
AnswerRe: am i a programmer or not Pin
Anubhava Dimri10-Apr-09 23:24
Anubhava Dimri10-Apr-09 23:24 
AnswerRe: am i a programmer or not Pin
je_gonzalez12-Apr-09 21:55
je_gonzalez12-Apr-09 21:55 
GeneralRe: am i a programmer or not Pin
dojohansen15-Apr-09 1:48
dojohansen15-Apr-09 1:48 
AnswerRe: am i a programmer or not Pin
dojohansen15-Apr-09 1:38
dojohansen15-Apr-09 1:38 
AnswerRe: am i a programmer or not Pin
Pete O'Hanlon15-Apr-09 2:04
mvePete O'Hanlon15-Apr-09 2:04 
QuestionXML and XSD into to a SQL Database Pin
binarymax9-Apr-09 2:19
binarymax9-Apr-09 2:19 
AnswerRe: XML and XSD into to a SQL Database Pin
dojohansen9-Apr-09 4:09
dojohansen9-Apr-09 4:09 
It sort of depends doesn't it? I would probably go down a dynamic code generation route, partly because it's fun to do (it's real easy in .net to compile code at run time and then run it), but also because it results in the fastest possible execution while also being very very flexible.

For example, you could take a reflection-based approach. What matters to you isn't so much the database types used, but the .net types they map to. As far as I know there's no simple way to query this mapping from the ado.net providers, so the easy way is to do a "select top 0 * from [table]" and fill a datatable, then loop through it's columns and check the DataType property, which is the .net type and not the native database type.

Then you can generate an sql command, such as an INSERT statement, knowing little more than the names of the columns and the table:

INSERT [table] (col1, ..., colN) VALUES (@col1, ..., @colN);

Of course, for this to work you do need to know about any identity or computed columns that cannot be inserted.

Then, generate C# code to create an SQL command, use the SQL as the CommandText and add parameters to it for all the columns. And generate a *separate* method that takes an XmlNode and assigns the parameters of the command object. This will allow you to insert N records creating only one command instance. What I've done before is have the C# code generator use reflection to see if the type used for each column has a Parse(string) method and also if it has the Parse(string, IFormatInfo) overload. If it has the latter I generate code to use it - passing CultureInfo.InvariantCulture as I want all our files to be culture-independent; only *presentation* should depend on this. (I've had enough of Microsoft's French "CSV" (comma separated values) files. Because the comma is decimal point in the French culture they changed the *file* format and use semicolon (or whatever is "list separator" in Windows Regional Settings) instead, meaning users of the English Excel version cannot open files saved by the French version unless you go and change the default settings...)

Obviously the generation itself is not super quick, depending on reflection and having to compile the results into an in-memory ("dynamic") assembly before use. But it's quick enough that I find it's perfectly acceptable not to pre-compile the generated code, just cache the compiled type so it can be reused for the rest of the apps lifetime.

Other alternatives exist of course. If you have or want to have a business layer you may want to look at Entity Framework. If not, and if you want to statically create the data access code at design-time rather than on the fly at run-time, you may wish to check out CodeSmith. Typed datasets should certainly work too, but personally I find the absolutely awful naming conventions used and the nested classes so inconvenient and confusing that it totally ruins them for me. I just cannot quite getmyself to code like this:

AdventureWorksDataSet.AdventureWorksProductsTable t = new AdventureWorksDataSet.AdventureWorksProductsTable();
AdventureWorksDataSet.AdventureWorksProductsTableRow r = ds.AdventureWorksProductsTable.NewAdventureWorksProductTableRow();


It may not be exactly like that - it's been a long time since I used this - but whatever the differences it was in fact just that awful. If you don't mind putting a bunch of using aliases at the top of each file where you use these types you can resolve some of it though (type names, not method names). You could derive your adapters from the generated ones and add methods with sensible names that just call the insanely-named ones, but it's getting involved already.

And if you target framework 3.x you can of course use type inference for locals as well.

using ProductTab = AdventureWorksDataSet.AdventureWorksProductsTable;
using ProductRow = AventureWorksDataSet.AdventureWorksProductTableRow;
...
// nice :)
var t = new ProductTab();
// not so nice
var r = t.NewAdventureWorksProductTableRow();


Just my two cents.
GeneralRe: XML and XSD into to a SQL Database Pin
binarymax9-Apr-09 8:39
binarymax9-Apr-09 8:39 
GeneralRe: XML and XSD into to a SQL Database Pin
dojohansen15-Apr-09 2:52
dojohansen15-Apr-09 2:52 
QuestionError "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Doan Quynh8-Apr-09 23:16
Doan Quynh8-Apr-09 23:16 
AnswerRe: Error "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Dave Kreskowiak9-Apr-09 4:11
mveDave Kreskowiak9-Apr-09 4:11 
GeneralRe: Error "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Doan Quynh9-Apr-09 18:29
Doan Quynh9-Apr-09 18:29 
GeneralRe: Error "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Dave Kreskowiak10-Apr-09 2:28
mveDave Kreskowiak10-Apr-09 2:28 
GeneralRe: Error "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Doan Quynh11-Apr-09 18:34
Doan Quynh11-Apr-09 18:34 
GeneralRe: Error "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Dave Kreskowiak11-Apr-09 18:40
mveDave Kreskowiak11-Apr-09 18:40 
GeneralRe: Error "Record(s) cannot be read; no read permission on 'table's name' " when Connect Access 2.0 (.mdb) file in Net ? Pin
Doan Quynh12-Apr-09 15:13
Doan Quynh12-Apr-09 15:13 
QuestionRetrieving the COM class factory for component with CLSID {83F04DB3-9710-11D5-87F6-005004FEB4BD} failed due to the following error: 800703e6. Pin
Renukapadhamanaban8-Apr-09 19:03
Renukapadhamanaban8-Apr-09 19:03 
QuestionWeb Service for Playing Media Player Pin
lynn10077-Apr-09 22:40
lynn10077-Apr-09 22:40 
AnswerRe: Web Service for Playing Media Player Pin
Dave Kreskowiak8-Apr-09 7:42
mveDave Kreskowiak8-Apr-09 7:42 
GeneralRe: Web Service for Playing Media Player Pin
lynn10078-Apr-09 15:15
lynn10078-Apr-09 15:15 
GeneralRe: Web Service for Playing Media Player Pin
Dave Kreskowiak8-Apr-09 17:40
mveDave Kreskowiak8-Apr-09 17:40 
GeneralRe: Web Service for Playing Media Player Pin
lynn10078-Apr-09 18:42
lynn10078-Apr-09 18:42 
GeneralRe: Web Service for Playing Media Player Pin
Dave Kreskowiak9-Apr-09 4:08
mveDave Kreskowiak9-Apr-09 4:08 
GeneralRe: Web Service for Playing Media Player Pin
lynn10079-Apr-09 16:17
lynn10079-Apr-09 16:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.