protected void btnXML2_Click(object sender, EventArgs e)
{
DataSet ds = DSReadXml(Server.MapPath("db.xml"));
this.dtlContent.DataSource=ds.Tables[0];
this.dtlContent.DataBind();
string sql=string.Empty;
foreach(DataRow dr in ds.Tables[0].Rows)
{
sql+="insert into img(";
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
sql=sql+ds.Tables[0].Columns[i].ColumnName.ToString()+",";
}
sql = sql.Substring(0, sql.Length - 1) + ") values (";
for (int x = 0; x < ds.Tables[0].Columns.Count; x++)
{
sql = sql + "'" + dr[x].ToString().Replace("'", "''") + "',";
}
sql = sql.Substring(0, sql.Length - 1) + ");";
}
SqlCommand sqlInsertCommand = new SqlCommand();
sqlInsertCommand.CommandText = sql;
sqlInsertCommand.Connection = conn;
conn.Open();
sqlInsertCommand.ExecuteNonQuery();
conn.Close();
}
public static DataSet DSReadXml(string path)
{
DataSet ds = new DataSet();
FileStream fs = null;
StreamReader reader = null;
try
{
fs = new FileStream(path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(fs);
ds.ReadXml(reader);
return ds;
}
finally
{
fs.Close();
reader.Close();
}
}