gpt4 book ai didi

c# - 在 C# asp.net 中使用 sqlbulkcopy 将 xml 数据写入 sql server

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:41 25 4
gpt4 key购买 nike

我在 m 系统上有两个 xml 文件,分别称为 customers.xml 和 customerdetails.xml。我试图将此数据直接写入我在 asp.net 中创建的 sql 数据库。我所有的编码都在 C# 中。 sql 数据库包含一个名为 CustomerDetails 的表,其中包含字段 CustomerID、CustomerN、CustomerLN、CustomerAdd、CustomerTelNo 和 Comments。

在 asp.net 中,我创建了一个名为更新的页面,其中包含一个上传控件,我试图获取 xml 文件并将数据写入数据库。控件的代码如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication9
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt == ".xml")
{
try
{
DataSet reportData = new DataSet();
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\CustomerDetails.mdf;Integrated
Security=True;User Instance=True");
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "CustomerInfo";
connection.Open();
sbc.WriteToServer(reportData.Tables[0]);
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "Only .xml files allowed!";
}
}
else
{
Label1.Text = "You have not specified a file.";
}
}
}

但是,当我尝试获取文件时,我收到一条错误消息,提示无法找到表 [0];

任何人都可以帮我确定这里的问题是什么!!

最佳答案

到目前为止,您所做的只是创建一个数据集。为了用户表[0],需要将数据表添加到数据集中。

以下是将数据表添加到数据集的一些基础知识:

http://msdn.microsoft.com/en-us/library/aeskbwf7(v=vs.80).aspx

关于c# - 在 C# asp.net 中使用 sqlbulkcopy 将 xml 数据写入 sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7532052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com