如何在sqlserver中使用C#导入xml数据 [英] how to import xml data using C# in sqlserver

查看:61
本文介绍了如何在sqlserver中使用C#导入xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></div>
    </form>
</body>
</html>





using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
   

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connStr);
        DataSet reportData = new DataSet();
        //reportData.ReadXml(Server.MapPath("test.xml"));
        reportData.ReadXml(Server.MapPath(FileUpload1.ToString()));
        SqlBulkCopy sbc = new SqlBulkCopy(con);
        sbc.DestinationTableName ="employee";
        sbc.ColumnMappings.Add("TALLY_REC_ID_CODE", "empid");
        sbc.ColumnMappings.Add("CLUSTER_ID", "empname");

        sbc.ColumnMappings.Add("UNIT_TYPE", "empaddress");
        sbc.ColumnMappings.Add("VPRC_ID", "empcity");
        sbc.ColumnMappings.Add("SHG_ID", "empstate");

        sbc.ColumnMappings.Add("PIP_NO", "empzip");
        sbc.ColumnMappings.Add("NAME_OF_THE_MEMBER", "emplic");
        sbc.ColumnMappings.Add("TYPE_OF_LOAN", "empstatus");

        sbc.ColumnMappings.Add("PRINCIPAL", "empp");
        sbc.ColumnMappings.Add("INTEREST", "empi");
        con.Open();
        sbc.WriteToServer(reportData.Tables[0]);

        con.Close();
        Label1.Text = "Records inserted successfully";

    }
}


但是我遇到了以下错误

找不到文件``D:\ WebSite1 \ System.Web.UI.WebControls.FileUpload''.
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.IO.FileNotFoundException:找不到文件"D:\ WebSite1 \ System.Web.UI.WebControls.FileUpload".


源错误:


第26行:DataSet reportData = new DataSet();
第27行://reportData.ReadXml(Server.MapPath("test.xml));
第28行:reportData.ReadXml(Server.MapPath(FileUpload1.ToString()));
第29行:SqlBulkCopy sbc = new SqlBulkCopy(con);
第30行:sbc.DestinationTableName ="employee";


But i m getting the following error

Could not find file ''D:\WebSite1\System.Web.UI.WebControls.FileUpload''.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not find file ''D:\WebSite1\System.Web.UI.WebControls.FileUpload''.


Source Error:


Line 26: DataSet reportData = new DataSet();
Line 27: //reportData.ReadXml(Server.MapPath("test.xml"));
Line 28: reportData.ReadXml(Server.MapPath(FileUpload1.ToString()));
Line 29: SqlBulkCopy sbc = new SqlBulkCopy(con);
Line 30: sbc.DestinationTableName = "employee";

推荐答案

尝试下面的代码:
Try the foolowing code :
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("report.xml"));
SqlConnection connection = new SqlConnection("CONNECTION STRING");
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "report_table";
       
//if your DB col names don’t match your XML element names 100%
//then relate the source XML elements (1st param) with the destination DB cols
sbc.ColumnMappings.Add("campaign", "campaign_id");
sbc.ColumnMappings.Add("cost", "cost_USD");
connection.Open();
//table 4 is the main table in this dataset
sbc.WriteToServer(reportData.Tables[4]);
connection.Close();


引用此链接可能会对您有所帮助:)

http://vinbhat.wordpress.com/2008/08/19/exporting-data-creating-xml-files-using-c/
Refer this link it may help you :)

http://vinbhat.wordpress.com/2008/08/19/exporting-data-creating-xml-files-using-c/


正确检查此异常:
Examine this exception properly:
Exception Details: System.IO.FileNotFoundException: Could not find file 'D:\WebSite1\System.Web.UI.WebControls.FileUpload'.


这是一个找不到文件的异常,表示找不到文件D:\ WebSite1 \ System.Web.UI.WebControls.FileUpload.

问题根源于此声明:


It''s a file not found exception meaning the file D:\WebSite1\System.Web.UI.WebControls.FileUpload can''t be found.

The issue roots to this statement:

reportData.ReadXml(Server.MapPath(FileUpload1.ToString()));


假设FileUpload1是一个上载控件,则调用ToString()方法将始终返回System.Web.UI.WebControls.FileUpload.

我猜您正在尝试获取上传文件的路径.试试这个


assuming that FileUpload1 is an upload control, calling ToString() method will always return System.Web.UI.WebControls.FileUpload.

I guess what you are trying to do is retrieving the path of the uploaded file. Try this

reportData.ReadXml(Server.MapPath(FileUpload1.FileName));


而不是使用ToString()


instead of using ToString()


这篇关于如何在sqlserver中使用C#导入xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆