尝试使用SqlDatareader填充数据表(我不认为我可以使用DataAdapter,因为我解析xml字符串的方式)。只是通过在线查看数据表的示例,这应该可行,但事实并非如此。当我调试它时,表在运行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>
答案 0 :(得分:3)
您是否点击了望远镜来查看实际的桌子?从快速浏览一下,您似乎正在做正确的事情,如果调试没有错误,那么您几乎肯定会让您的数据恢复正常。当然,在您添加行之前,该表不会包含数据。我知道这听起来很傻,但请确保在至少一次table.Rows.Add(row)迭代后点击spyglass; ....
答案 1 :(得分:0)
从代码中查看此摘录:
conn.Open();
conn.ChangeDatabase(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
第二行强制连接再次关闭。只需交换这两行就可以了。