如何将数据集作为参数传递给 WCF 方法



在那里,我使用此代码读取excel文件的数据,它对我正常工作。但是我需要将此数据集保存到数据库中,WCF.因此,请考虑将dataset传递给 wcf 方法来实现这一目标。但是我该怎么做

这是我尝试使用Windows表单应用程序编写代码

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opn = new OpenFileDialog();
opn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
if (opn.ShowDialog() == DialogResult.Cancel)
return;
FileStream strm = new FileStream(opn.FileName, FileMode.Open);
IExcelDataReader excldr = ExcelReaderFactory.CreateOpenXmlReader(strm);
DataSet rslt = excldr.AsDataSet();
// while (excldr.Read())
// {
//    Console.WriteLine(excldr.GetString(1));
// }
}

我需要将DataSet rslt传递给WCF方法,并且在WCF内我认为是否 function.is 这种良好做法编写保存数据?那我该怎么做

阅读您的评论后,我想我应该给您一些示例,如何以稍微好一点的方式解决此问题。

我的方法是初始化请求/响应DataContract对象,允许客户端更有效地发送数据集并接收确认。

以下是DataContract类:

客户端将实例化和填充并发送到服务的请求对象:

[DataContract]
public class SaveDataSetRequest
{
[DataMember]
public DataSet Data { get; set; }
[DataMember]
public string FileName { get; set; }
// you can add more properties here in the future versions without effecting the existing clients.
}

服务将实例化和填充并发送回客户端的响应对象:

[DataContract]
public class SaveDataSetResponse
{
public string Message { get; set; }
}

服务合同:

[ServiceContract]
public interface IDataService
{
[OperationContract]
SaveDataSetResponse SaveDataSet(SaveDataSetRequest request);
}

及其实施:

public class DataService : IDataService
{
public SaveDataSetResponse SaveDataSet(SaveDataSetRequest request)
{
SaveDataSetResponse response = new SaveDataSetResponse();
try
{
// save or processes your data set from request.Data object,
// once this operation is completed successfully then return the success message.
response.Message = "Success";
}
catch (Exception ex)
{
// log your exception 
response.Message = $"Unable to save / process the data. Reason: {ex.Message}";
}
return response;
}
}

您将需要像这样更新服务配置(如果您还没有)

<endpoint
address="http://localhost:8000/<namespace>/IDataService"
binding="netTcpBinding"
contract="<namespace>.IDataService"
name="DataService">
</endpoint>

在客户端上,更新服务引用(添加新版本的服务 dll 或服务 URL - WSDL),创建客户端代理类的实例并调用SaveDataSetResponse SaveDataSet(SaveDataSetRequest request)方法。如果要按原样复制此代码,请确保客户端配置 - 终结点也使用新协定进行更新。

一些有用的链接:

  • 如果您是新手或 需要刷新:
  • 如果您有复数视力通道,那么本课程将是一个很好的 起点。

通过 WCF 传递数据表或数据集是一个备受争议的话题。 它可以很容易地完成,但是我个人更喜欢传递数据本身而不是元数据(列和行定义,数据关系等) 我通常声明一个对象,将其公开给WCF并传输它。

对于数据集中的每个表,您可以执行以下操作(未经测试):

public class ExcelService : IExcelService
{
public List<CustomExcelData> GetExcelData()
{
List<CustomExcelData> excelDataList = new List<CustomExcelData>();
OpenFileDialog opn = new OpenFileDialog();
opn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
if (opn.ShowDialog() == DialogResult.Cancel)
return null;
FileStream strm = new FileStream(opn.FileName, FileMode.Open);
IExcelDataReader excldr = ExcelReaderFactory.CreateOpenXmlReader(strm);
DataSet rslt = excldr.AsDataSet();
DataTable dt = rslt.Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
excelDataList.Add(new CustomExcelData(row));
}
}
return excelDataList;
}
}

和界面:

[ServiceContract]
interface IExcelService
{
[OperationContract]
List<CustomExcelData> GetExcelData();
}
[DataContract]
public class CustomExcelData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public DateTime BirthDate { get; set; }
public CustomExcelData(DataRow row)
{
Name = row["NameField"].ToString();
Address = row["AddressField"].ToString();
BirthDate = DateTime.Parse(row["BirthDateField"].ToString());
}
}

我解决了我的问题:)

private void button2_Click(object sender, EventArgs e)
{
ServiceReference1.Service2Client obj = new ServiceReference1.Service2Client();
OpenFileDialog opn = new OpenFileDialog();
opn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
if (opn.ShowDialog() == DialogResult.Cancel)
return;
try
{
FileStream strm = new FileStream(opn.FileName, FileMode.Open);
IExcelDataReader excldr = ExcelReaderFactory.CreateOpenXmlReader(strm);
DataSet rslt = excldr.AsDataSet();
obj.insertExecl(rslt);
}
catch (IOException x)
{
MessageBox.Show(x.Message);
}
}

服务2.cs

public class Service2 : IService2
{
public void insertExecl(DataSet rslt)
{
DataClasses1DataContext conn = new DataClasses1DataContext();
foreach (DataTable table in rslt.Tables)
{
foreach (DataRow dr in table.Rows)
{
tblExcel addTbl = new tblExcel()
{
SID = Convert.ToString(dr[0]),
Name = Convert.ToString(dr[1]),
Address = Convert.ToString(dr[2])
};
conn.tblExcels.InsertOnSubmit(addTbl);
}
}

conn.SubmitChanges();
//excldr.Close();
//strm.Close();
Console.WriteLine("successfully");
}
}

IService2.cs

[ServiceContract]
public interface IService2
{
[OperationContract]
void insertExecl(DataSet rslt);
}

最新更新