如何将字符串 属性 传递给 WCF 并将 return 传递给数据集?
How to pass in String property to WCF and return a dataset?
程序returns System.Data.DataSet。我想要数据集 returned 的项目。
我有一个包含 4 个 table 的数据库:Bands
、Festivals
、Stages
;和一个交界处 table - Band_Festival
.
sql执行时查询所有工作,表单上的DataGridView
显示正确的记录。
我正在尝试让 WCF 检索数据并将 return 一个 DataSet
或 list<DataSet>
到表单。
调用时应将表单上选定的 ComboBox
项传递给 wcf,用作 SQL 查询的参数。
String Festivals {get; set;}
ComboBox.SelectedItem = Festivals
用于向sql查询传入参数。
当sql查询的WHERE条件为festival_name = 'Boomtown Fair'
时调用returns "System.Data.DataSet",我要列表项目。当条件为 festival_name = @festival
和 Parameters.AddValue("@festival", Festivals)
时抛出异常 "Parameter not supplied"
这是一些代码:
表格
String Festivals { get; set; }
FestivalsBandsServiceReference.FestivalBandsServiceClient ws = null;
public Form1()
{
InitializeComponent();
ComBoxFestivals.SelectedItem = Festivals;
}
private void Form1_Load(object sender, EventArgs e)
{
ws = new FestivalsBandsServiceReference.FestivalBandsServiceClient();
}
private void comBoxFestivals_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> festival = ws.GenerateNewBandList(Festivals);
Console.WriteLine(String.Join("," ,festival));
}
FestivalBandsService.svc.cs
public List<DataSet> GenerateNewBandList(String Festivals)
{
List<DataSet> bands = new List<DataSet>();
string dataSource = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = " +
@"D:\WCFFestivalBands\WCFFestivalBands\App_Data\FestivalBands.mdf; Integrated Security = True; Connect Timeout = 30";
//Create new instance of sql connection, pass in the connection string for FestivalBands.mdf to connect to database.
SqlConnection conn = new SqlConnection(dataSource);
string sqlQuerySelectBands = "SELECT Bands.Band_Name FROM Band_Festival JOIN dbo.Bands ON Bands.Band_ID =" +
"Band_Festival.Band_ID JOIN dbo.Festival ON Festival.Festival_ID " +
"= Band_Festival.Festival_ID WHERE Festival_Name = @festival";
SqlCommand cmd = new SqlCommand(sqlQuerySelectBands, conn);
cmd.Parameters.AddWithValue("@festival", Festivals);
conn.Open();
//Create new instance of DataAdpater to retrieve the data pass in Sql command
SqlDataAdapter da = new SqlDataAdapter(cmd);
//Create new instance of dataSet to hold the data retrieved from sql query
DataSet ds = new DataSet();
//using DataAdapter fill in dataSet wiht data if user input and stored data matches
da.Fill(ds);
bands.Add(ds);
return bands;
}
}
IFestivalBandsService.cs
[ServiceContract]
public interface IFestivalBandsService
{
[OperationContract]
List<DataSet> GenerateNewBandList(String Festivals);
}
这很新。刚刚学会了如何使用 WCF 传递和 return 值。从数据集中传递列表是下一个级别。非常感谢任何帮助。
我还没有添加 table 的列或行,不确定它是否相关?
提前致谢。
我怀疑您是否可以从 WCF return List<DataSet>
,DataSet 可以是任何东西,它不知道如何序列化它。
我们为传输做的是使用 DTO(数据传输对象)。基本上这只是一种奇特的说法,创建一个具体的 class 并确保它用 DataContract
属性标记,并且每个 属性 都用 DataMember
属性标记,其中合适的 。例如
[DataContract]
public class PurchaseOrder
{
// Apply the DataMemberAttribute to the property.
[DataMember]
public int PurchaseOrderId { get; set
}
在此处详细了解它们
根据你的代码片段,我复制到本地测试了一下,貌似你的WCF服务端没有报错。否则,当您硬编码 SQL 语句来查询您的数据库时,它不会成功。但是当你在客户端使用服务时,我找不到你初始化节日的过程属性。
String Festivals { get; set; }
ComBoxFestivals.SelectedItem = Festivals;
这些代码片段可能是导致您出现问题的原因。
在.net 4.6+中你可以参考下面的代码设置默认的属性.
public int Price { get; set; } = 100;
或者你可以参考下面的代码
class Product
{
private string name = "Apple";
public int ID { get; set; }
public string Name
{
get { return name; }
set { this.name = value; }
}
}
程序returns System.Data.DataSet。我想要数据集 returned 的项目。
我有一个包含 4 个 table 的数据库:Bands
、Festivals
、Stages
;和一个交界处 table - Band_Festival
.
sql执行时查询所有工作,表单上的DataGridView
显示正确的记录。
我正在尝试让 WCF 检索数据并将 return 一个 DataSet
或 list<DataSet>
到表单。
调用时应将表单上选定的 ComboBox
项传递给 wcf,用作 SQL 查询的参数。
String Festivals {get; set;}
ComboBox.SelectedItem = Festivals
用于向sql查询传入参数。
当sql查询的WHERE条件为festival_name = 'Boomtown Fair'
时调用returns "System.Data.DataSet",我要列表项目。当条件为 festival_name = @festival
和 Parameters.AddValue("@festival", Festivals)
时抛出异常 "Parameter not supplied"
这是一些代码:
表格
String Festivals { get; set; }
FestivalsBandsServiceReference.FestivalBandsServiceClient ws = null;
public Form1()
{
InitializeComponent();
ComBoxFestivals.SelectedItem = Festivals;
}
private void Form1_Load(object sender, EventArgs e)
{
ws = new FestivalsBandsServiceReference.FestivalBandsServiceClient();
}
private void comBoxFestivals_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> festival = ws.GenerateNewBandList(Festivals);
Console.WriteLine(String.Join("," ,festival));
}
FestivalBandsService.svc.cs
public List<DataSet> GenerateNewBandList(String Festivals)
{
List<DataSet> bands = new List<DataSet>();
string dataSource = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = " +
@"D:\WCFFestivalBands\WCFFestivalBands\App_Data\FestivalBands.mdf; Integrated Security = True; Connect Timeout = 30";
//Create new instance of sql connection, pass in the connection string for FestivalBands.mdf to connect to database.
SqlConnection conn = new SqlConnection(dataSource);
string sqlQuerySelectBands = "SELECT Bands.Band_Name FROM Band_Festival JOIN dbo.Bands ON Bands.Band_ID =" +
"Band_Festival.Band_ID JOIN dbo.Festival ON Festival.Festival_ID " +
"= Band_Festival.Festival_ID WHERE Festival_Name = @festival";
SqlCommand cmd = new SqlCommand(sqlQuerySelectBands, conn);
cmd.Parameters.AddWithValue("@festival", Festivals);
conn.Open();
//Create new instance of DataAdpater to retrieve the data pass in Sql command
SqlDataAdapter da = new SqlDataAdapter(cmd);
//Create new instance of dataSet to hold the data retrieved from sql query
DataSet ds = new DataSet();
//using DataAdapter fill in dataSet wiht data if user input and stored data matches
da.Fill(ds);
bands.Add(ds);
return bands;
}
}
IFestivalBandsService.cs
[ServiceContract]
public interface IFestivalBandsService
{
[OperationContract]
List<DataSet> GenerateNewBandList(String Festivals);
}
这很新。刚刚学会了如何使用 WCF 传递和 return 值。从数据集中传递列表是下一个级别。非常感谢任何帮助。
我还没有添加 table 的列或行,不确定它是否相关?
提前致谢。
我怀疑您是否可以从 WCF return List<DataSet>
,DataSet 可以是任何东西,它不知道如何序列化它。
我们为传输做的是使用 DTO(数据传输对象)。基本上这只是一种奇特的说法,创建一个具体的 class 并确保它用 DataContract
属性标记,并且每个 属性 都用 DataMember
属性标记,其中合适的 。例如
[DataContract]
public class PurchaseOrder
{
// Apply the DataMemberAttribute to the property.
[DataMember]
public int PurchaseOrderId { get; set
}
在此处详细了解它们
根据你的代码片段,我复制到本地测试了一下,貌似你的WCF服务端没有报错。否则,当您硬编码 SQL 语句来查询您的数据库时,它不会成功。但是当你在客户端使用服务时,我找不到你初始化节日的过程属性。
String Festivals { get; set; }
ComBoxFestivals.SelectedItem = Festivals;
这些代码片段可能是导致您出现问题的原因。
在.net 4.6+中你可以参考下面的代码设置默认的属性.
public int Price { get; set; } = 100;
或者你可以参考下面的代码
class Product
{
private string name = "Apple";
public int ID { get; set; }
public string Name
{
get { return name; }
set { this.name = value; }
}
}