使用带有linq的单例到sql执行存储过程时出错 [英] Error using singleton with linq to sql execting stored procedure

查看:79
本文介绍了使用带有linq的单例到sql执行存储过程时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我尝试使用单件模式获取错误时使用wcf服务





 '  System.Data.Linq.DataContext' 不包含带 0 参数的构造函数





你可以指导如何解决这个错误,





 使用系统; 
使用 System.Data;
使用 System.Configuration;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.Linq.Mapping;
使用 System.Data.Linq;
使用 System.Reflection;
命名空间 WcfService5
{

public class Singleton:DataContext
{
private static Singleton _instance = null ;

private static Object _mutex = new Object ();
static String 连接;
private Singleton( String connectionString)
{
// 无论
}

public static Singleton GetInstance( String ConnectionString)
{
if (_instance == null
{
lock (_mutex) // 现在我可以声称某种形式的线程安全......
{
if (_instance == null
{
_instance = new Singleton(ConnectionString);
}
}
}

return _instance;
}
[功能(名称= @ GetEmpByNames,IsComposable = < span class =code-keyword> false )]
public ISingleResult< myems> getCustomerAll([Parameter(DbType = Nvarchar(50))] 字符串 Ename,[参数(DbType = Int)] Int32 Eno)
{

IExecuteResult objResult = this .ExecuteMethodCall(< span class =code-keyword> this ,(MethodInfo)MethodInfo.GetCurrentMethod(),Ename,Eno);
ISingleResult< myems> objresults =(ISingleResult< myems>)objResult.ReturnValue;
return objresults;
}
}

}

解决方案

你的施工人员需要



 ...:base({基类构造函数想要的参数}} 




。如果没有这个,C#会尝试使用没有参数的构造函数初始化基类,你可以从错误中看到基类中没有这样的构造函数。



祝你好运


试试这个代码...



  public  Singleton( String  connectionString): base (connectionString)
{
// 无论
}







将构造函数访问修饰符设置为public

并使用基类构造函数中的connectionstring启动基类。


Hi All ,

I was tried to work with wcf service using singleton pattern getting error


'System.Data.Linq.DataContext' does not contain a constructor that takes 0 arguments



can u guide how to solve this error ,


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using System.Reflection;
namespace WcfService5
{
   
    public class Singleton : DataContext
    {
        private static Singleton _instance = null;

        private static Object _mutex = new Object();
        static String connection;
        private Singleton(String connectionString)
        {
            // whatever
        }

        public static Singleton GetInstance(String ConnectionString)
        {
            if (_instance == null)
            {
                lock (_mutex) // now I can claim some form of thread safety...
                {
                    if (_instance == null)
                    {
                        _instance = new Singleton(ConnectionString);
                    }
                }
            }

            return _instance;
        }
        [Function(Name = @"GetEmpByNames", IsComposable = false)]
        public ISingleResult<myems> getCustomerAll([Parameter(DbType = "Nvarchar(50)")] string Ename, [Parameter(DbType = "Int")] Int32 Eno)
        {

            IExecuteResult objResult = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(), Ename, Eno);
            ISingleResult<myems> objresults = (ISingleResult<myems>)objResult.ReturnValue;
            return objresults;
        }
    }  
   
}

解决方案

Your constructors need

... : base({arguments the base class constructor wants})



after the declaration line. Without this, C# tries to initialize the base class using a constructor with no arguments and you can see from the error that there is no such constructor in the base class.

Good Luck


try this code...

public Singleton(String connectionString): base(connectionString)
       {
           // whatever
       }




Make the constructor access modifiers as public
and initiate base class with the connectionstring in the base class constructor..


这篇关于使用带有linq的单例到sql执行存储过程时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆