尝试使用 SqlDatareader 填充数据表(由于我解析 xml 字符串的方式,我认为我不能使用 DataAdapter)。只需查看在线数据表的示例,这应该可以工作,但事实并非如此。当我调试它时,当它通过 do while 循环运行时,它总是 {}。这是怎么回事?
string sqlEntry = ConfigurationManager.ConnectionStrings["sqlPass"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlEntry);
try
{
conn.Open();
conn.ChangeDatabase(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
string sqlQuery = "SELECT * from equipInspection";
SqlCommand sqlComm = new SqlCommand(sqlQuery, conn);
SqlDataReader myReader;
myReader = sqlComm.ExecuteReader();
DataTable table = new DataTable();
table.Columns.Add("Equipment", typeof(String));
table.Columns.Add("SerialNo",typeof(String));
table.Columns.Add("Contractor",typeof(String));
table.Columns.Add("Date", typeof(String));
table.Columns.Add("Deficiencies", typeof(String));
if (myReader.Read())
{
do
{
string stringtosplit = myReader["formXML"].ToString();
string[] xmlInfo = stringtosplit.Split(new string[] { ";ANKR!" }, StringSplitOptions.None);
DataRow row = table.NewRow();
row["Equipment"] = xmlInfo[0];
row["SerialNo"] = xmlInfo[1];
row["Contractor"] = myReader["Name"].ToString();
row["Date"] = myReader["Date"].ToString();
row["Deficiencies"] = xmlInfo[12];
table.Rows.Add(row);
} while (myReader.Read());
这是 xml 格式的示例。这可能是它的结构方式或我解析它的方式。
<!--Equipment inspection form by Test-->
<EquipmentInspection Date="2013/08/07" Time="12:05 AM" Location="Somewhere" ContractorName="Joe" Operator="Jane" Position="Boss" Contact="2132213421">
<Field ID="txtEquipment" Type="Textbox">Jackhammer;ANKR!</Field>
<Field ID="txtSerial" Type="Textbox">1234A5A-1;ANKR!</Field>
<Field ID="txtManufacturer" Type="Textbox">Test;ANKR!</Field>
<Field ID="txtUsage" Type="Textbox">Test;ANKR!</Field>
<Field ID="txtService" Type="Textbox">2013/08/05;ANKR!</Field>
<Field ID="rblRecentInspect" Type="RadioButtonList">No;ANKR!</Field>
<cblShift>2, ;ANKR!</cblShift>
<cblDaysInspected>W, F, ;ANKR!</cblDaysInspected>
<Field ID="rblDamage" Type="RadioButtonList">Yes;ANKR!</Field>
<Field ID="txtDamage" Type="Textbox">;ANKR!</Field>
<Field ID="txtRepairs" Type="RadioButtonList">No;ANKR!</Field>
<Field ID="rblDef" Type="RadioButtonList">No;ANKR!</Field>
<Field ID="txtDef" Type="Textbox">it broke;ANKR!</Field>
</EquipmentInspection>