Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自数据访问层的WCF_C#_Wcf_Serialization_Sqlparameter - Fatal编程技术网

C# 来自数据访问层的WCF

C# 来自数据访问层的WCF,c#,wcf,serialization,sqlparameter,C#,Wcf,Serialization,Sqlparameter,我遇到了一个问题,我正在努力从该数据访问层创建WCF服务应用程序: public class DataAccess { private SqlConnection connection = new SqlConnection("Data Source=LAPI;Initial Catalog=PrimierData;Integrated Security=True"); private SqlDataReader dataReader; private SqlComman

我遇到了一个问题,我正在努力从该数据访问层创建WCF服务应用程序:

public class DataAccess
{
    private SqlConnection connection = new SqlConnection("Data Source=LAPI;Initial Catalog=PrimierData;Integrated Security=True");
    private SqlDataReader dataReader;
    private SqlCommand command;
    private SqlTransaction transaction = null;
    private SqlParameter[] parameters = null;


    #region Conenction
    public void Open()
    {
        if (connection.State != ConnectionState.Open)
            connection.Open();
    }

    public void Close()
    {
        if (connection.State != ConnectionState.Closed)
            connection.Close();
    }

    #endregion




    #region Reader
    /// <summary>
    /// Executes the reader. For MultiRow Search
    /// </summary>
    /// <param name="commandType">Type of the command.</param>
    /// <param name="commandText">The command text.</param>
    /// <returns></returns>
    public SqlDataReader ExecuteReader(CommandType commandType, string commandText,SqlParameter[] readerparams)
    {
        Open();
        command = new SqlCommand(commandText, connection);
        command.CommandType = commandType;
        if (readerparams != null)
        {
            command.Parameters.AddRange(readerparams); 
        }
       this.dataReader = command.ExecuteReader();
        command.Parameters.Clear();
       // Close();
        return this.dataReader;
    }

   #endregion

    #region Execute
    /// <summary>
    /// Executes the non query. For Insert, Update and Delete
    /// </summary>
    /// <param name="commandType">Type of the command.</param>
    /// <param name="commandText">The command text.</param>
    /// <param name="parameters">The parameters.</param>
    /// <returns></returns>
    public int ExecuteNonQuery(CommandType commandType, string commandText,SqlParameter[] nonparams)
    {
        Open();
        command = new SqlCommand(commandText, connection);
        command.CommandType = commandType;
        command.Parameters.AddRange(nonparams);
        int returnValue = command.ExecuteNonQuery();
        command.Parameters.Clear();
        Close();
        return returnValue;
    }

  #endregion

}
还有我的服务.svc

public class Service1 : IService1
{
    private SqlConnection connection = new SqlConnection("Data Source=LAPI;Initial Catalog=PrimierData;Integrated Security=True");
    private SqlDataReader dataReader;
    private SqlCommand command;
    private SqlTransaction transaction = null;
    private SqlParameter[] parameters = null;


    [OperationContract]
    public void Open()
    {
        if (connection.State != ConnectionState.Open)
            connection.Open();
    }

    [OperationContract]
    public void Close()
    {
        if (connection.State != ConnectionState.Closed)
            connection.Close();
    }

    [OperationContract]
    public int ExecuteNonQuery(CommandType commandType, string commandText, SqlParameter[] nonparams)
    {
        Open();
        command = new SqlCommand(commandText, connection);
        command.CommandType = commandType;
        command.Parameters.AddRange(nonparams);
        int returnValue = command.ExecuteNonQuery();
        command.Parameters.Clear();
        Close();
        return returnValue;
    }
    [OperationContract]
    public SqlDataReader ExecuteReader(CommandType commandType, string commandText, SqlParameter[] readerparams)
    {
        Open();
        command = new SqlCommand(commandText, connection);
        command.CommandType = commandType;
        if (readerparams != null)
        {
            command.Parameters.AddRange(readerparams);
        }
        this.dataReader = command.ExecuteReader();
        command.Parameters.Clear();
        // Close();
        return this.dataReader;
    }

无法对SqlDataReader进行XML序列化。在基于服务的应用程序中,客户端应用程序不应了解任何有关数据库和相关操作的信息。我建议创建服务用来发送和接收数据的类和对象。

好的,我自己也这么做了,这对我很有用。 在客户端

public SQLArray[] SQLtoArray(SqlParameter[] parama)
    {
        if (parama != null)
        {
            SqlParameter[] param = parama;
            int lenght = param.Count();
            SQLArray[] unner = new SQLArray[lenght];

            for (int i = 0; i < lenght; i++)
            {
                unner[i] = new SQLArray();
                unner[i].ParamaterName = param[i].ParameterName;
                unner[i].Paramatertype = param[i].SqlDbType;
                unner[i].ParamaterDirection = param[i].Direction;
                unner[i].ParamaterValue = param[i].Value.ToString();
            }
            return unner;
        }
        return null;
    }
CSV


我没有看到任何WCF服务代码…如果此代码有效,您最好显示哪些代码无效…您是否正在尝试将WCF服务引用添加到DAL?你必须更具体一些,这样我们才能帮助你。是的,我试图引用DAL,很抱歉这么不具体。我的第一篇文章。我现在将添加错误代码。看看WCF数据服务-看起来您正在尝试在这里重新发明轮子…@KingCronus哦,是的。最新答复。
public SQLArray[] SQLtoArray(SqlParameter[] parama)
    {
        if (parama != null)
        {
            SqlParameter[] param = parama;
            int lenght = param.Count();
            SQLArray[] unner = new SQLArray[lenght];

            for (int i = 0; i < lenght; i++)
            {
                unner[i] = new SQLArray();
                unner[i].ParamaterName = param[i].ParameterName;
                unner[i].Paramatertype = param[i].SqlDbType;
                unner[i].ParamaterDirection = param[i].Direction;
                unner[i].ParamaterValue = param[i].Value.ToString();
            }
            return unner;
        }
        return null;
    }
[ServiceContract]
[ServiceKnownType(typeof(SqlParameter[]))]
[ServiceKnownType(typeof(object))]
[ServiceKnownType(typeof(SqlDbType))]
[ServiceKnownType(typeof(ParameterDirection))]
[ServiceKnownType(typeof(SqlDateTime))]
public interface IServerService
{
    [OperationContract]
    DataSet ExecuteDataSet(CommandType commandType, string commandText, SQLArray[] dsparams);

    [OperationContract]
    int ExecuteNonQuery(CommandType commandType, string commandText, SQLArray[] nonparams);


}
[DataContract]
[KnownType(typeof(SqlParameter[]))] 
[KnownType(typeof(object))]
[KnownType(typeof(SqlDbType))]
[KnownType(typeof(ParameterDirection))]
[KnownType(typeof(SqlDateTime))]
public class SQLArray
{
  //  private SqlParameter[] array;
    private string paramaterName;
    private string paramaterValue;
    private ParameterDirection paramaterDirection;
    private SqlDbType paramatertype;


   [DataMember]

    public string ParamaterName
    {
        get { return paramaterName; }
        set { paramaterName = value; }
    }
    [DataMember]
    public string ParamaterValue
    {
        get { return paramaterValue; }
        set { paramaterValue = value; }
    }
    [DataMember]
    public ParameterDirection ParamaterDirection
    {
        get { return paramaterDirection; }
        set { paramaterDirection = value; }
    }
    [DataMember]
    public SqlDbType Paramatertype
    {
        get { return paramatertype; }
        set { paramatertype = value; }
    }
}
[Serializable]
public class ServerService : IServerService
{
    #region Class Members

  // private ServerDataAccess dataAccess;
    SqlConnection connection = new SqlConnection("Data Source=LAPI;Initial Catalog=PrimierData;Integrated Security=True");
   // SqlDataReader dataReader;
    SqlCommand command;
   // SqlTransaction transaction = null;
   // SqlParameter[] parameters = null;

    #endregion

    #region Constructor

   public ServerService()
    {


    }
    public SqlParameter[] ArrayToSQL(SQLArray[] parama)
    {
       // bool outahere = false;
        int count = 0;
        foreach (SQLArray array in parama)
            count++;
        SqlParameter[] unner = new SqlParameter[count];
        for (int i = 0; i < count; i++)
        {
            unner[i] = new SqlParameter();
            unner[i].ParameterName = parama[i].ParamaterName;
            unner[i].SqlDbType = parama[i].Paramatertype;
            unner[i].Direction = parama[i].ParamaterDirection;
            unner[i].Value = parama[i].ParamaterValue;
        }
        return unner;
    }
    public SQLArray[] SQLtoArray(SqlParameter[] parama)
    {
        int count = 0;
        foreach (SqlParameter parameter in parama)
            count++;
        SQLArray[] unner = new SQLArray[count];

        for (int i = 0; i < parama.Count(); i++)
        {
            unner[i] = new SQLArray();
            unner[i].ParamaterName = parama[i].ParameterName;
            unner[i].Paramatertype = parama[i].SqlDbType;
            unner[i].ParamaterDirection = parama[i].Direction;
            unner[i].ParamaterValue = parama[i].Value.ToString();
        }
        return unner;
    }
    #endregion

    public void Open()
    {
        if (connection.State != ConnectionState.Open)
            connection.Open();
    }

    public void Close()
    {
        if (connection.State != ConnectionState.Closed)
            connection.Close();
    }

    #region Methods
    /// <summary>
    /// Executes the non query. For Insert, Update and Delete
    /// </summary>
    /// <param name="commandType">Type of the command.</param>
    /// <param name="commandText">The command text.</param>
    /// <param name="parameters">The parameters.</param>
    /// <returns></returns>
    public int ExecuteNonQuery(CommandType commandType, string commandText, SQLArray[] nonparams)
    {
       Open();
        command = new SqlCommand(commandText, connection);
        command.CommandType = commandType;
        command.Parameters.AddRange(ArrayToSQL(nonparams));
        int returnValue = command.ExecuteNonQuery();
        command.Parameters.Clear();
        Close();
        return returnValue;
    }
    public DataSet ExecuteDataSet(CommandType commandType, string commandText, SQLArray[] dsparams)
    {

            Open();
            command = new SqlCommand(commandText, connection);
            command.CommandType = commandType;
            if (dsparams != null)
            {
                command.Parameters.AddRange(ArrayToSQL(dsparams));
            }
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.SelectCommand = command;
            DataSet dataSet = new DataSet();
            dataAdapter.Fill(dataSet);
            command.Parameters.Clear();
            Close();

            return dataSet;



    }