在那里,我使用此代码来读取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
我认为要编写保存数据函数。这是一个好的做法吗?那我该怎么做呢
答案 0 :(得分:2)
通过WCF传递DataTable或DataSet是一个备受争议的话题。 它可以轻松完成,但我个人更喜欢传递数据本身而不是元数据(列和行定义,数据关系等) 我通常声明一个对象,将它暴露给WCF并传输它。
对于DataSet中的每个表,您可以执行以下操作(未测试):
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());
}
}
答案 1 :(得分:1)
在阅读您的评论后,我想我应该给您一些示例,以更好的方式解决这个问题。
我的方法是初始化请求/响应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)
方法。如果要按原样复制此代码,请确保客户端配置 - 端点也使用新合同进行更新。
一些有用的链接:
答案 2 :(得分:0)
我解决了我的问题:)
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);
}
}
<强> Service2.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);
}