我有以下代码,我尝试在基于 3 个表的单个数据集中创建 2 个关系。如果我使用 datasource.Relations.Add(relationname) 运行代码,表单将不会显示任何内容。如果我评论这一行,它将向我显示信息,但我需要定义这 2 个关系,因为我在 2 个单独的组合框中使用该信息。
public void Fill_DataSource()
{
SqlConnection conn = new SqlConnection(ConfigurationManager
.ConnectionStrings["SRDBConnection"]
.ConnectionString);
try
{
//Open the connection
conn.Open();
//Create the dataset
ds = new DataSet();
//Fill the dataset with the base table, Roles
SqlDataAdapter daRoles = new SqlDataAdapter(new SqlCommand("select *" +
"from Roles",
conn));
daRoles.TableMappings.Add("Table", "Roles");
daRoles.Fill(ds);
//Fill the dataset with the joining table, SystemRoles
SqlDataAdapter daSysRoles = new SqlDataAdapter(new SqlCommand("select *" +
"from SystemRoles", conn));
daSysRoles.TableMappings.Add("Table", "SysRoles");
daSysRoles.Fill(ds);
//Fill the dataset with the joining table, BusinessGroups
SqlDataAdapter daBGs = new SqlDataAdapter(new SqlCommand("select *" +
"from BusinessGroups", conn));
daBGs.TableMappings.Add("Table", "BusinessGroups");
daBGs.Fill(ds);
// Show created Tablenames within the Dataset
string myMessage = "Table Mappings: ";
for (int i = 0; i < ds.Tables.Count; i++)
{
myMessage += i.ToString() + " "
+ ds.Tables[i].ToString() + " ";
}
// Establish the Relationship "RolesBGs"
// between Roles>---<BusinessGroups
DataRelation relRolesBGs;
DataColumn colRoleId1;
DataColumn colBGId;
colRoleId1 = ds.Tables["Roles"].Columns["BGId"];
colBGId = ds.Tables["BusinessGroups"].Columns["BGId"];
relRolesBGs = new DataRelation("relRolesBGs", colRoleId1, colBGId);
// If I uncomment the following line the form will no longer display anything
// and also the debugger will skip any following breaking points after it
//ds.Relations.Add(relRolesBGs);
// Establish the Relationship "RolesSysRoles"
// between Roles>---<SystemRoles
//DataRelation relRolesSysRoles;
//DataColumn colRoleId2;
//DataColumn colSysRoleId;
//colRoleId2 = ds.Tables["Roles"].Columns["SysRoleId"];
//colSysRoleId = ds.Tables["SystemRoles"].Columns["SysRoleId"];
//relRolesSysRoles = new DataRelation("relRolesSysRoles", colRoleId2, colSysRoleId);
//ds.Relations.Add(relRolesSysRoles);
// Show created Relations within the Dataset
myMessage += "Relation Mappings: ";
for (int i = 0; i < ds.Relations.Count; i++)
{
myMessage += i.ToString() + " "
+ ds.Relations[i].ToString() + " ";
}
textBox4.Text = myMessage;
dsView = ds.DefaultViewManager;
//Databinding for the Sys Role ComboBox
//cbSysRole.DataSource = dsView;
//cbSysRole.DisplayMember = "SysRoles.Name";
//cbSysRole.ValueMember = "SysRoles.SysRoleId";
//Databinding for the Business Group ComboBox
//cbBG.DataSource = dsView;
//cbBG.DisplayMember = "BusinessGroups.Name";
//cbBG.ValueMember = "BusinessGroups.BGId";
//BGbindSource.DataSource = ds.Tables[0];
///bindingNavigator1.BindingSource = BGbindSource;
//RoleCode
tbRoleCode.DataBindings.Clear();
tbRoleCode.DataBindings.Add(new Binding("Text", dsView, "Roles.RoleCode",
true, DataSourceUpdateMode.OnPropertyChanged));
//RoleName
tbRoleName.DataBindings.Clear();
tbRoleName.DataBindings.Add(new Binding("Text", dsView, "Roles.RoleName",
true, DataSourceUpdateMode.OnPropertyChanged));
//RoleDesc
tbRoleDesc.DataBindings.Clear();
tbRoleDesc.DataBindings.Add(new Binding("Text", dsView,
"Roles.RoleDescription", true, DataSourceUpdateMode.OnPropertyChanged));
//RoleId
tbRoleId.DataBindings.Clear();
tbRoleId.DataBindings.Add(new Binding("Text", dsView, "Roles.RoleId",
true, DataSourceUpdateMode.OnPropertyChanged));
}
catch (Exception)
{
toolStripStatusLabel3.Text = "Database Is Offline or the Connection is not set correctly!";
}
finally
{
conn.Close();
}
}
public Roles()
{
InitializeComponent();
Fill_DataSource();
}
正如代码注释中提到的,如果我取消注释并在它之后甚至在它上面创建断点,调试器将跳过这些,我只会得到一个空表单。我不明白那里有什么问题。任何帮助将不胜感激!!!
谢谢!