将通用列表保存到SQL数据库

时间:2013-07-09 18:43:16

标签: c#

我正在尝试将通用列表保存到SQL数据库。该代码采用HTML表文件,并使用HTMLAgilityPack拉出每列。当我设置contract.ToList();对于Datalist并显示它,数据是正确的。现在我无法将其保存到SQL数据库。

我以为我应该将列表存储在DataTable中然后使用SQLBulkCopy,但是我收到错误“无法将类型为'System.Collections.Generic.List`1 [ContractsImporter.Form1 + Contract]'的对象强制转换为'' System.Data.DataTable”。”使用下面的代码

DataTable dt = (DataTable)(dataGridView1.DataSource);
                dataGridView2.DataSource = dt;

我还在努力学习C#,所以他们比我想做的更好吗?

感谢您的反馈。

此外,SQL表列名称与程序中的联系人变量名称相同。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using HtmlAgilityPack;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace ContractsImporter
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            var DB = new System.Windows.Forms.OpenFileDialog();
            if (DB.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileToOpen = DB.FileName;
                textBoxImport.Text = fileToOpen;

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public static void ImportData()
        {
            SqlConnection thisConnection = new SqlConnection("Server=sqlsrv1;Initial Catalog=LoadData;User ID=user;Password=password");
            SqlCommand nonqueryCommand = thisConnection.CreateCommand();
        }

        public class Contract
        {
            public int ID { get; set; }
            public string AssetManager { get; set; }
            public string BillingAddress { get; set; }
            public string BillingCity { get; set; }
            public string BillingState { get; set; }
            public string BillingZip { get; set; }
            public string ContractAmount { get; set; }

        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            testing();

        }

        public void testing()
        {

            var data = File.ReadAllText(textBoxImport.Text);
            var table = new DataTable("ImportTable");

            string xmlData;


            var document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(data);
            document.OptionOutputAsXml = true;


            using (var stream = new StringWriter())
            using (var writer = XmlWriter.Create(stream))
            {
                document.Save(writer);
                xmlData = stream.ToString();
            }


            var contract =
                XDocument.Parse(xmlData)
                        .XPathSelectElements("//table/tr")
                        .Skip(1)
                        .Select(x => new Contract
                        {
                            ID = int.Parse(x.Elements("td").First().Value),
                            AssetManager = x.Elements("td").Skip(1).First().Value,
                            BillingAddress = x.Elements("td").Skip(2).First().Value,
                            BillingCity = x.Elements("td").Skip(3).First().Value,
                            BillingState = x.Elements("td").Skip(4).First().Value,
                            BillingZip = x.Elements("td").Skip(5).First().Value,
                            ContractAmount = x.Elements("td").Skip(6).First().Value,
                            });

            dataGridView1.DataSource = contract.ToList();

            DataTable dt = (DataTable)(dataGridView1.DataSource);
            dataGridView2.DataSource = dt;

            label1.Text = "Done";
        }
    }
}

0 个答案:

没有答案