这是我的DBML Designer代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public partial class DataClassesDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public DataClassesDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
}
这是我的C#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdLogin_Click(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
}
}
当我写DataClassesDataContex db = new DataClassesDataContex();它会显示DataClassesDataContex不包含带0参数的构造函数吗?
答案 0 :(得分:5)
您的班级DataClassesDataContext
中没有定义无参数构造函数。默认情况下,应该生成另一个构造函数,它从应用程序设置获取连接字符串:
public DataClassesDataContext() :
base(global::Foo.Properties.Settings.Default.BarConnectionString,
mappingSource)
{
OnCreated();
}
但由于某种原因,它现在缺失了。你可以:
DataClassesDataContext
并手动添加无参数构造函数答案 1 :(得分:4)
步骤:
答案 2 :(得分:0)
我用以下步骤修复了它:
卸下:
<connectionString>
<add name="exampleDB" connectionString="Data Source=example;Initial
Catalog=example;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
卸下:
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=D...8"/>
</assemblies>
您可以在<assemblies>
<compilation></compilation>
如果您有多个<add assembly />
标记,则您与DB有多个连接。
添加另一个&#34; example.dbml&#34;再次添加你的表格。
创建一个新的datacontext对象。