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.
精彩评论