I have a problem i am struggling to create a WCF Service Application form this Data Access Layer :
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
}
I would like to use WCF but i get a Error
Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.
And i tried to code it I am failing hard. The code works that i am using but when creating the WCF i am a complte noob.
[ServiceContract]
public interface IService1
{
// TODO: Add your service operations here
[OperationContract]
void Open();
[OperationContract]
void Close();
[OperationContract]
SqlDataReader ExecuteReader(CommandType commandType, string commandText, SqlParameter[] readerparams);
[OperationContract]
int ExecuteNonQuery(CommandType commandType, string commandText, SqlParameter[] nonparams);
}
and my Service.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;
}