问题描述
大家好,
我发现这篇文章
https://visualstudiomagazine.com/articles/2015/06/01 /table-valued-parameters.aspx
实体数据模型为我的sp创建了这个:
public virtual int sp_TinUserManten(Nullable< int> pTypeLang ,Nullable< int> pTypeOpe,ObjectParameter pUsers_id)
{
var pTypeLangParameter = pTypeLang.HasValue?
新ObjectParameter(QUOT; pTypeLang" ;, pTypeLang):
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; new ObjectParameter(" pTypeLang",typeof(int));
var pTypeOpeParameter = pTypeOpe.HasValue?
新ObjectParameter(QUOT; pTypeOpe" ;, pTypeOpe):
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; new ObjectParameter(" pTypeOpe",typeof(int));
返回((IObjectContextAdapter)此).ObjectContext.ExecuteFunction(QUOT; sp_TinUserManten" ;, pTypeLangParameter,pTypeOpeParameter,pUsers_id);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
pTypeLang是数据库类型。我的想法是将DataTable作为参数传递,返回可以是错误或新的id。
并为表创建了这个(我添加了一些字段):
公共部分类tin_user
{
public int users_id {get;组; }
public string user_name {get;组; }
公共字符串姓氏{get;组; }
公共字符串名称{get;组; }
public Nullable< short> type_document {get;组; }
public string number_id {get;组; }
public string email {get;组; }
公共字符串地址{get;组; }
// ------------------------ NEW FIELDS -------------------- //
[StringLength(20)]
$
私有字符串_desc_status_users {get;组; }
private gen_StatusProcess _gen_StatusProcess {get;组; }
}
我从我的WCF方法调用sp为:
&NBSP; &NBSP; &NBSP; public String addUser(string uID)
{
return insertUser(uID);
}
public String insertUser(string uID)
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; DataTable uTbl = new DataTable(" type_tin_user");
uTbl.Columns.Add(" users_id",typeof(Int32));
uTbl.Columns.Add(" user_name",typeof(string));
uTbl.Columns.Add(" surnames",typeof(string));
uTbl.Columns.Add(" names",typeof(string));
uTbl.Columns.Add(" type_document",typeof(Int32));
uTbl.Columns.Add(" number_id",typeof(string));
uTbl.Columns.Add(" email",typeof(string));
uTbl.Columns.Add(" address",typeof(string));
$
  ;&NBSP;&NBSP; uTbl.Rows.Add(此处为"所有值");
&NBSP; SqlConnection conn;
conn = ConnectionManager.GetConnection();
conn.Open();
$
SqlCommand newCmd = conn.CreateCommand();
$
newCmd.CommandText =" sp_TinUserManten";
newCmd.CommandType = CommandType.StoredProcedure;
newCmd.Parameters.Add(new SqlParameter(" @ pReg",uTbl)); //传递表
newCmd.Parameters.AddWithValue(" @ pTypeLang",1);
newCmd.Parameters.AddWithValue(" @ pTypeOpe",1);
newCmd.Parameters.Add(QUOT; @ pUsers_id" ;, SqlDbType.Int).Direction = ParameterDirection.Output;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; newCmd.ExecuteNonQuery();
$
字符串tempID = newCmd.Parameters [" @ pUsers_id"]。Value.ToString();
conn.Close();
return tempID;
}
如何使用/配置sp类来传递来自外部的值并使用复杂类型从我的WCF方法调用?
也许在WCF和数据访问层之间创建一个业务层。
谢谢!
https://www.youtube.com / watch?v = 6g8Z82xoWHw
Hi All,
I found this article
https://visualstudiomagazine.com/articles/2015/06/01/table-valued-parameters.aspx
The entity data model created this for my sp:
public virtual int sp_TinUserManten(Nullable<int> pTypeLang, Nullable<int> pTypeOpe, ObjectParameter pUsers_id)
{
var pTypeLangParameter = pTypeLang.HasValue ?
new ObjectParameter("pTypeLang", pTypeLang) :
new ObjectParameter("pTypeLang", typeof(int));
var pTypeOpeParameter = pTypeOpe.HasValue ?
new ObjectParameter("pTypeOpe", pTypeOpe) :
new ObjectParameter("pTypeOpe", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_TinUserManten", pTypeLangParameter, pTypeOpeParameter, pUsers_id);
}
Where the pTypeLang is a database type. The idea is to pass a DataTable as parameter and the return can be error or the new id.
And created this for table as (I added some fields): public partial class tin_user
{
public int users_id { get; set; }
public string user_name { get; set; }
public string surnames { get; set; }
public string names { get; set; }
public Nullable<short> type_document { get; set; }
public string number_id { get; set; }
public string email { get; set; }
public string address { get; set; }
//------------------------ NEW FIELDS --------------------//
[StringLength(20)]
private string _desc_status_users { get; set; }
private gen_StatusProcess _gen_StatusProcess { get; set; }
}
I called the sp from my WCF method as:
public String addUser(string uID)
{
return insertUser(uID);
}
public String insertUser(string uID)
{
DataTable uTbl = new DataTable("type_tin_user");
uTbl.Columns.Add("users_id", typeof(Int32));
uTbl.Columns.Add("user_name", typeof(string));
uTbl.Columns.Add("surnames", typeof(string));
uTbl.Columns.Add("names", typeof(string));
uTbl.Columns.Add("type_document", typeof(Int32));
uTbl.Columns.Add("number_id", typeof(string));
uTbl.Columns.Add("email", typeof(string));
uTbl.Columns.Add("address", typeof(string));
uTbl.Rows.Add("all values here");
SqlConnection conn;
conn = ConnectionManager.GetConnection();
conn.Open();
SqlCommand newCmd = conn.CreateCommand();
newCmd.CommandText = "sp_TinUserManten";
newCmd.CommandType = CommandType.StoredProcedure;
newCmd.Parameters.Add(new SqlParameter("@pReg", uTbl)); //pass table
newCmd.Parameters.AddWithValue("@pTypeLang", 1);
newCmd.Parameters.AddWithValue("@pTypeOpe", 1);
newCmd.Parameters.Add("@pUsers_id", SqlDbType.Int).Direction = ParameterDirection.Output;
newCmd.ExecuteNonQuery();
String tempID = newCmd.Parameters["@pUsers_id"].Value.ToString();
conn.Close();
return tempID;
}
How can I use/configure the sp class to pass the values coming from outside and call from my WCF method using the complex type?
Maybe create a business layer between the WCF and the Data Access Layer.
Thanks!
这篇关于WCF具有复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!