开发者

Is there a bug in .NET ColumnMappings class?

开发者 https://www.devze.com 2022-12-09 03:59 出处:网络
This works: using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.OleDb;

This works:

using System;
using System.Collections.Generic;
using System.Data;     
using System.Data.Common;
using System.Data.OleDb;

public class MyClass
{
    public static void Main()
    {
    OleDbConnection mySqlConnection =new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\nwind.Mdb");

    OleDbCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText = "SELECT EmployeeID AS MappedID, FirstName, LastName " +
      "FROM Employees AS Emp " +
      "WHERE EmployeeID = 9";
    OleDbDataAdapter mySqlDataAdapter = new OleDbDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();
    mySqlConnection.Open();
    mySqlDataAdapter.Fill(myDataSet, "Employees");
    mySqlConnection.Close();

    DataTableMapping myDataTableMapping = mySqlDataAdapter.TableMappings.Add("Employees", "dtEmployee");

    myDataSet.Tables["Employees"].TableName = "dtEmployee";

    Console.WriteLine("myDataTableMapping.DataSetTable = " + myDataTableMapping.DataSetTable);
    Console.WriteLine("myDataTableMapping.SourceTable = " + myDataTableMapping.SourceTable);

    myDataTableMapping.ColumnMappings.Add("EmployeeID", "MappedId");

    DataTable开发者_Go百科 myDataTable = myDataSet.Tables["dtEmployee"];
    foreach (DataRow myDataRow in myDataTable.Rows)
    {
      Console.WriteLine("ID = " + myDataRow["MappedId"]);
      Console.WriteLine("FirstName = " + myDataRow["FirstName"]);
      Console.WriteLine("LastName = " + myDataRow["LastName"]);
    }   
        Console.ReadLine();
    }

}

Just change "MappedId" to something else like "NewMappedId":

myDataTableMapping.ColumnMappings.Add("EmployeeID", "NewMappedId");
Console.WriteLine("ID = " + myDataRow["NewMappedId"]);

to something else like "NewMappedId" and the program will crash at runtime saying NewMappedId doesn't belong to table dtEmployee. Is this a bug ?!


No bug here.

If you change the contents of a bound dataset, this error is normal since you no longer match the underlying schema you are bound to.

0

精彩评论

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