Mam wiele tabel z danymi: chcę je połączyć (mój PK: emp_num)Jak połączyć kilka DataTables (każdy DataTable powrót jeden wiersz) w jednym rzędzie
var tblEmp_data = new DataTable();//one row
var tblEmp_time = new DataTable();//one row
var tbl_emp_mission = new DataTable();//one row
tblEmp_data = GetEmpInfo(empNum);
tblEmp_time = GetEmpTime(empNum, start_period, end_period);
tbl_emp_mission = GetEmpMission(empNum, start_period, end_period);
tblEmp_data.Merge(tblEmp_time, false, MissingSchemaAction.Add);
tblEmp_data.AcceptChanges();
tblEmp_data.Merge(tbl_emp_mission, false, MissingSchemaAction.Add);
tblEmp_data.AcceptChanges();
Teraz mogę dane w wielu wierszach zamiast w jednym wierszu! Chcę dane w jednym rzędzie? jak to zrobić ?
Uwaga: chcę wszystkie kolumny pozwalają zerowy wyjątkiem klucz podstawowy więc uniknąć tego wyjątek:
failed to enable constraints. one or more rows contain values violating non-null, unique, or foreign-key constraints
edycja:
Trzecia tabela które powodują ten problem:
public static DataTable GetEmpMission(int empNum, DateTime start_period, DateTime end_period)
{
using (IfxConnection con = new IfxConnection(ConfigurationManager.ConnectionStrings["tl"].ToString()))
{
DataTable dt = new DataTable();
StringBuilder cmdTxt = new StringBuilder();
cmdTxt.Append(" SELECT COUNT(emp_num) ");
cmdTxt.Append(" FROM hs_mission WHERE emp_num = ? AND from_date BETWEEN ? AND ? ");
using (var myIfxCmd = new IfxCommand(cmdTxt.ToString(), con))
{
myIfxCmd.CommandType = CommandType.Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
myIfxCmd.Parameters.Add("emp_num", IfxType.SmallInt);
myIfxCmd.Parameters.Add("start_period", IfxType.Date);
myIfxCmd.Parameters.Add("end_period", IfxType.Date);
myIfxCmd.Parameters[0].Value = ((object)empNum) ?? DBNull.Value;
myIfxCmd.Parameters[1].Value = ((object)start_period.Date) ?? DBNull.Value;
myIfxCmd.Parameters[2].Value = ((object)end_period.Date) ?? DBNull.Value;
using (IfxDataReader dr = myIfxCmd.ExecuteReader())
{
dt.Load(dr);
dt.Columns.Add("emp_num", typeof(Int32));
dt.Rows[0]["emp_num"] = empNum;
dt.AcceptChanges();
}
}
con.Close();
con.Dispose();
return dt;
}
}
dane powrócić, ale w ten sposób:
column1 emp_num
0 6762
i rzucać wyjątek:
failed to enable constraints. one or more rows contain values violating non-null, unique, or foreign-key constraints
masz trzy tabele można dać przykładu podoba danych we wszystkich trzech stole i format dla czwartej tabeli? podałeś tylko jeden. – Ratna