尝试使用SqlDatareader填充datatable(由于我解析xml字符串的方式,我认为无法使用DataAdapter)。仅通过查看在线datatables示例,应该可以工作,但事实并非如此。当我调试它时,当它通过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>
if(myReader.Read())
中?代码里还有其他东西吗?如果没有,你可以用while
替换if
和do
。 - voithos