最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決WCF不能直接序列化SqlParameter類型的問(wèn)題

 更新時(shí)間:2022年03月04日 10:33:55   作者:.NET開(kāi)發(fā)菜鳥(niǎo)  
這篇文章介紹了解決WCF不能直接序列化SqlParameter類型的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

錯(cuò)誤描述:

由于內(nèi)部錯(cuò)誤,服務(wù)器無(wú)法處理該請(qǐng)求。有關(guān)該錯(cuò)誤的詳細(xì)信息,請(qǐng)打開(kāi)服務(wù)器上的 IncludeExceptionDetailInFaults (從 ServiceBehaviorAttribute 或從 <serviceDebug> 配置行為)以便將異常信息發(fā)送回客戶端,或打開(kāi)對(duì)每個(gè) Microsoft .NET Framework SDK 文檔的跟蹤并檢查服務(wù)器跟蹤日志。

客戶端調(diào)用WCF的時(shí)候報(bào)上面的錯(cuò)誤,WCF只能序列化基礎(chǔ)的數(shù)據(jù)類型,不能直接序列化SqlParameter類型,需要使用自定義類,然后在WCF服務(wù)端轉(zhuǎn)換的方式解決:

自定義類代碼如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace CommonLib.CustomClass
{
    /// <summary>
    /// 方法標(biāo)記為DataContract約束,屬性標(biāo)記為DataMember
    /// </summary>
    [Serializable]
    [DataContract]
    public class SetSqlParameter
    {
        #region 屬性

        /// <summary>
        /// 參數(shù)名稱
        /// </summary>
        [DataMember]
        private string paraName = "";
        public string ParaName
        {
            get { return this.paraName; }
            set { this.paraName = value; }

        }


        /// <summary>
        /// 參數(shù)長(zhǎng)度
        /// </summary>
        [DataMember]
        private int paraLength = 0;
        public int ParaLength
        {

            get { return this.paraLength; }
            set { this.paraLength = value; }
        }


        /// <summary>
        /// 參數(shù)值
        /// </summary>
        [DataMember]
        private object paraValue = null;
        public object ParaValue
        {
            get { return this.paraValue; }
            set { this.paraValue = value; }
        }


        /// <summary>
        /// 參數(shù)類型
        /// </summary>
        [DataMember]
        private SqlDbType paraDbType = SqlDbType.NVarChar;
        public SqlDbType ParaDbType
        {
            get { return this.paraDbType; }

            set { this.paraDbType = value; }
        }

        #endregion

        /// <summary>
        /// 構(gòu)造函數(shù)
        /// </summary>
        /// <param name="sPara"></param>
        public SetSqlParameter(SqlParameter sPara)
        {
            this.paraName = sPara.ParameterName;
            this.paraLength = sPara.Size;
            this.paraValue = sPara.Value;
            this.paraDbType = sPara.SqlDbType;
        }

        /// <summary>
        /// 轉(zhuǎn)換成SqlParameter類型
        /// </summary>
        /// <returns></returns>
        public SqlParameter ConvertToSqlParameter()
        {
            SqlParameter parameter = new SqlParameter(this.paraName, this.paraDbType, this.paraLength);
            parameter.Value = this.paraValue;
            return parameter;
        }
    }
}

WCF服務(wù)端代碼如下:

接口代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CommonLib.CustomClass;

namespace WcfServiceDemo
{
    // 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時(shí)更改代碼和配置文件中的接口名“IMyService”。
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        DataTable ExeceteQuery(string strSQL, params SetSqlParameter[] parameters);
    }
}

接口實(shí)現(xiàn)類代碼:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Configuration;
using CommonLib.CustomClass;

namespace WcfServiceDemo
{
    // 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時(shí)更改代碼、svc 和配置文件中的類名“MyService”。
    // 注意: 為了啟動(dòng) WCF 測(cè)試客戶端以測(cè)試此服務(wù),請(qǐng)?jiān)诮鉀Q方案資源管理器中選擇 MyService.svc 或 MyService.svc.cs,然后開(kāi)始調(diào)試。
    public class MyService : IMyService
    {

        public DataTable ExeceteQuery(string strSQL, params SetSqlParameter[] parameters)
        {
            DataTable dtReturn = new DataTable();
            dtReturn.TableName = "ExecuteQuery";
            string strCon = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(strCon))
            {
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                conn.Open();
                if (parameters != null)
                {
                    SqlParameter[] para = new SqlParameter[parameters.Length];
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        //把SetSqlParameter類型的數(shù)組轉(zhuǎn)換成SqlParameter類型的數(shù)組
                        para[i] = parameters[i].ConvertToSqlParameter();
                    }
                    cmd.Parameters.AddRange(para);
                }

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dtReturn);
            }
            return dtReturn;
        }
    }
}

客戶端調(diào)用WCF代碼:

using CommonLib.CustomClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace winClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_GetData_Click(object sender, EventArgs e)
        {

            string strSQL = " SELECT * FROM BaseSetMainInfo WHERE TypeCode=@TypeCode ";

            //定義SqlParameter
            SqlParameter para = new SqlParameter("@TypeCode", SqlDbType.Int);
            para.Value = 1;

            //定義SetSqlParameter類型的數(shù)組
            SetSqlParameter[] paras = new SetSqlParameter[] {
                new SetSqlParameter(para)
            };

            //實(shí)例化WCF服務(wù)
            ServiceReference.MyServiceClient client=new ServiceReference.MyServiceClient();
            //調(diào)用WCF服務(wù)提供的方法
            DataTable dt = client.ExeceteQuery(strSQL, paras);
            this.dataGridView1.DataSource = dt;

        }
    }
}

這樣就可以解決WCF不能直接序列化SqlParameter類型的問(wèn)題了。

代碼下載地址:點(diǎn)此下載

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問(wèn)題的解決

    C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問(wèn)題的解決

    這篇文章主要給大家介紹了關(guān)于C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • WCF實(shí)現(xiàn)雙向通信

    WCF實(shí)現(xiàn)雙向通信

    這篇文章介紹了WCF實(shí)現(xiàn)雙向通信的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • c# 在Emit代碼中如何await一個(gè)異步方法

    c# 在Emit代碼中如何await一個(gè)異步方法

    這篇文章主要介紹了c# 在Emit代碼中如何await一個(gè)異步方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放

    C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放

    這篇文章主要介紹了C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放,涉及C#采用Dispose模式操作資源的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C#實(shí)現(xiàn)將選中復(fù)選框的信息返回給用戶的方法

    C#實(shí)現(xiàn)將選中復(fù)選框的信息返回給用戶的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將選中復(fù)選框的信息返回給用戶的方法,涉及C#針對(duì)復(fù)選框操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 一篇文章帶你輕松了解C# Lock關(guān)鍵字

    一篇文章帶你輕松了解C# Lock關(guān)鍵字

    這篇文章主要給大家介紹了如何通過(guò)一篇文章帶你輕松了解C# Lock關(guān)鍵字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Unity UGUI Shadow陰影組件的介紹使用示例

    Unity UGUI Shadow陰影組件的介紹使用示例

    這篇文章主要為大家介紹了Unity UGUI Shadow陰影組件的介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • C#/VB.NET中從?PDF?文檔中提取所有表格

    C#/VB.NET中從?PDF?文檔中提取所有表格

    這篇文章主要介紹了C#/VB.NET中從PDF文檔中提取所有表格,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • C#實(shí)現(xiàn)簡(jiǎn)單屏幕監(jiān)控的方法

    C#實(shí)現(xiàn)簡(jiǎn)單屏幕監(jiān)控的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單屏幕監(jiān)控的方法,涉及C#的圖標(biāo)隱藏及屏幕截圖等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • LINQ投影操作符Select與限制操作符where介紹

    LINQ投影操作符Select與限制操作符where介紹

    這篇文章介紹了LINQ投影操作符Select與限制操作符where,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02

最新評(píng)論

朝阳县| 绥江县| 梁河县| 开封市| 桑日县| 西和县| 昆明市| 新和县| 华坪县| 闽清县| 宜黄县| 佳木斯市| 民勤县| 黔江区| 临朐县| 塔河县| 临桂县| 金湖县| 中卫市| 开江县| 太和县| 乌苏市| 高州市| 大冶市| 霸州市| 筠连县| 灵寿县| 古丈县| 社会| 桃江县| 武清区| 广水市| 临汾市| 永济市| 蕉岭县| 临邑县| 瑞金市| 明溪县| 常熟市| 新化县| 晋江市|