开发者

Problems updating a data source with data adapter update

First off, I've only started working with databases and c# so I could be making a really stupid mistake here so apologies in advance. I'm connected to a SQL server express database and can read from it fine, however whenever I have updated my DataSet to add new columns and then try to update the data source it wont update successfully. Code below:

        DataColumn newColumn;
        SqlCommandBuilder cmdBuild = new SqlCommandBuilder(_myAdapt);

        foreach (String s in myList)
        {
            newColumn = new DataColumn(s, System.Type.GetType("System.String"));
            _myData.Tables["Table1"].Columns.Add(newColumn);
        }

        _myAdapt.Update(_myData, "Table1");

Anyone got any suggestions as to why this wont work? I've debugged it and the data set is definitely being updated with the new columns but I cant seem to update th开发者_StackOverflowe data source.

Thanks for any responses.


Does that column exist in the database (and just not in your database), or are you trying to ALTER the table definition in the database using the dataset.

I don't believe the 2nd of the above is possible. Your dataset needs to have a column structure to match that of the database table that you're updating. You can however add as many rows as you like to the dataset and they will all INSERT to the database fine.

If you feel you have a need to add columns to your table at run-time I'd suggest revisiting your data-model. Where possible your column definitions should be be unchanging over time, to represent the main attributes of the data entities you're storing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜