views:

642

answers:

2

I have an option to choose between local based data storing (xml file) or SQL Server based. I already created a long time ago a typed dataset for my application to save data local in the xml file.

Now, I have a bool that changes between Server based version and local version. If true my application get the data from the SQL Server.

I'm not sure but It seems that Sql Adapter's Fill Method can't fill the Data in my existing schema

SqlCommand cmd = new SqlCommand("Select * FROM dbo.Categories WHERE CatUserId = 1", _connection);
                cmd.CommandType = CommandType.Text;

                _sqlAdapter = new SqlDataAdapter(cmd);
                _sqlAdapter.TableMappings.Add("Categories", "dbo.Categories");
                _sqlAdapter.Fill(Program.Dataset);

This should fill my data from dbo.Categories to Categories (in my local, typed dataset). but it doesn't. It creates a new table with the name "Table". It looks like it can't handle the existing schema.

I can't figure it out. Where is the problem?

btw. of course the database request I do isn't very useful that way. It's just a simplified version for testing...

A: 

Try _sqlAdapter.TableMappings.Add("Table", "Categories");, but as i remember you will have to add column mapping also. btw, you can try to create typed dataadapter, it is useless thing, but you can take mapping from there.

Andrey
I also have ColumnMappings but It doesn't work better that way. It's just strange. It doesn't matter which way I set the TableMappings Add Parameters. All the time I get "Table" back instead of the named from the TableMapping.
René
A: 

The Fill overload you are using, passing in a DataSet will always create a NEW DataTable in the supplied DataSet with name "Table" (MSDN).

So, I think you either need to change your TableMapping to:

_sqlAdapter.TableMappings.Add("Table", "Categories");

(MSDN) assuming your DataTable name is "Categories".

Or, don't use TableMappings and just supply the second argument - the name of the DataTable in that DataSet you want to populate (MSDN). The approach I usually use is actually to pass the DataTable itself that you want to populate, instead of the DataSet (MSDN).

AdaTheDev
Problem is, I already have an existing data schema. But the tables have other names than the tables at the sql database. So i tried to make the TableMapping the sql database match the local dataset schema.Don't ask me, why it is this way. But I think there should be a work around instead of rename the tables at the server.
René
@Rene - I wasn't suggesting you need to rename any tables. Did you check the 2nd link in my answer? It doesn't matter that the table names in the SQL Database have different names to the DataTables in your DataSet. See figure 1 in the 2nd link I gave
AdaTheDev
Ah, ok, sorry, your right. I have to test that. 3th links nice. I hope that also works with the column names.it's a workaround, but it doesn't solve my problem I have with using a TableMapping. But it's a solution.I'm going to try that.
René
Ok. I tested that. Works like a charm, but without ColumnMapping. I have no idea how to Add columnMapping when I use this way of TableMapping (2nd link, 3th link).
René
@Rene - I don't know about that approach and ColumnMappings off the top of my head. Check out this example which relates more to your original approach, and the first 2 links I gave above: http://www.c-sharpcorner.com/UploadFile/puranindia/164/
AdaTheDev
Ok, seems to work. Thank you.
René