PDA

View Full Version : سوال: انتقال اطلاعات از فایل xml به دیتابیس



mehdin69
یک شنبه 17 آذر 1392, 18:59 عصر
سلام دوستان
من یه فایل xml دارم که با دستورات Sql ساختم...
حالا می خوام این فایل xml رو بدم به برنامه و خودش بصورت یک فایل backup عمل کنه و اطلاعات توی فایل xml رو توی دیتابیس restore کنه
باید چطوری یک فایل xml رو بدم و بگم اطلاعات هر جدول رو توی جدول خودش بریزه؟
مرسی

parvizwpf
یک شنبه 17 آذر 1392, 19:03 عصر
چند روش کار با xml وجود داره. یعنی تک تک نودها رو بخونید و بریزید توی فیلدهای دیتا؟

mehdin69
یک شنبه 17 آذر 1392, 19:13 عصر
نه نمی خوام اینطوری باشه
میخوام فایل xml بدم و خودش اطلاعات هر جدولی رو سر جای خودش بذاره
چون با این دستور تونستم خرجی xml بگیرم فکر کنم راهی وجود داشته باشه برای اینکه با یه دستور شبیه به این عکس این دستور رو انجام داد


SELECT BusinessEntityID, PersonType, Title, FirstName, MiddleName, LastName
FROM Person
WHERE BusinessEntityID = 10001
FOR XML AUTO, ELEMENTS

parvizwpf
یک شنبه 17 آذر 1392, 19:16 عصر
والا دقیقا هنوز نفهمیدم حکم سی شارپ اینجا چیه. حخب ااینکارا سمت اس کیو ال بهتر هندل میشه که.

fmehrvarzi
یک شنبه 17 آذر 1392, 19:23 عصر
//1. Create a new console application called ReadingXML in the directory
//2. Add a using directive for the System.Data namespace to the top of the code:
using System.Data;
//3. Add the following code to the Main() method:
static void Main(string[] args)
{
//Creat an empty DataSet
DataSet thisDataSet = new DataSet();

//Fill it with XML in this directory : "c:\Northwind\nwinddata.xml"
thisDataSet.ReadXml(@"c:\Northwind\nwinddata.xml");

// Show the Data in DataSet
foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows)
{

Console.WriteLine("Customer ID: " + custRow["CustomerID"] + " Name: " + custRow["CompanyName"]);
}

//Show the table name of DataSet
Console.WriteLine("Table created by ReadXml is called {0}",thisDataSet.Tables[0].TableName);

Console.Write("Program finished, press Enter/Return to continue:");

Console.ReadLine();

}

mehdin69
دوشنبه 18 آذر 1392, 09:04 صبح
پیدا کردم :)


var reportData = new DataSet();
reportData.ReadXml("yourfile.xml");
var connection = new SqlConnection("DB ConnectionSTring");
var sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "yourXMLTable";