Tag: connection string

在web.config中放置连接字符串的位置

我的web.config看起来像这样: 当我在下面添加我的连接字符串时,我得到一个错误,说只允许一个元素。 我应该把我的连接字符串放在哪里?

覆盖代码生成的DbContext构造函数

我确定我在某个阶段之前已经做过这个,但我现在还不清楚怎么做! 我的情景: // This is generated from EDMX public partial class HOLDbEntities : DbContext { public HOLDbEntities() : base(“name=HOLDbEntities”) { } } 现在,我希望这个连接字符串很容易更改(我想从HOLDbEntities实现),所以我需要覆盖这个构造函数。 我试过了: public partial class HOLDbEntities { private const string _contextName = “HOLDbEntities”; public static string ContextName { get { return _contextName; } } public HOLDbEntities() : base(ContextName) { } } 但这会引发一个错误: HOLDbEntities已经定义了一个名为“HOLDbEntities”的成员,它具有相同的参数类型。 […]

从外部配置文件中读取连接字符串

我创建了一个控制台应用程序和一个app.config文件以及Connections.config文件。 app.config文件有一个指向Connections.config的connectionstring属性源 当我尝试读取应用程序中的连接字符串时,我得到一个ConfigurationErrorException 这是我的主要方法。 static void Main(string[] args) { var settings = ConfigurationManager.ConnectionStrings; if (settings != null) { foreach (ConnectionStringSettings setting in settings) { Console.WriteLine(setting.ConnectionString); } } } App.config文件 Connections.config文件 我在这里观察了两件事。 第一:如果我指定configSource,我无法读取连接字符串(抛出exception。) 第二:如果我在App.config文件中放入相同的连接字符串并尝试读取然后代码正在工作但获得两个连接字符串(应该只返回一个空字符串)第一个连接字符串是sqlexpress连接字符串,如下所示 data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true 它返回的第二个连接字符串是空字符串(这是预期的)。 我想从我的场景中读取外部文件中的连接字符串。 怎么做? 我在这里想念的是什么?

如何在代码中添加entity framework6提供程序?

我在C#应用程序中使用Entity Framework 6,它完美地运行。 创建模型时,将生成app.config并包含所有必需的配置。 现在我不喜欢app.config中的东西,所以我使用连接字符串构建器。 我成功地删除了app.config文件中的所有内容,除此之外: 如果我删除它,它不起作用。 那么如何将该配置转换为c#代码? 我该怎么办? 我查看了基于代码的配置( http://msdn.microsoft.com/en-us/data/jj680699 )但是,它没有帮助。 我创建连接字符串的部分类看起来像这样: public partial class LogEntities { public LogEntities(string serverName) : base(GetConnectionString(serverName)) { } public static string GetConnectionString(string serverName) { // Specify the provider name, server and database. const string databaseName = “_LOG”; // Initialize the connection string builder for the underlying provider. SqlConnectionStringBuilder […]

entity framework连接字符串不是来自配置

public class Context : DbContext { public Context(string connString) : base(connString) { Database.SetInitializer(new MyContextInitializer()); } //… 需要将连接字符串传递给上下文构造函数。 字符串应该如何,例如,对于SQL Compact? 试过这个,但没有成功: Context db = new Context(“Provider=System.Data.SqlServerCe.4.0;Data Source=D:\\Context.sdf”); 编辑: 如果我尝试这个字符串: “Data Source=D:\\Context.sdf” System.Data.ProviderIncompatibleException未处理 Message =从数据库获取提供程序信息时发生错误。 这可能是由entity framework使用不正确的连接字符串引起的。 检查内部exception以获取详细信息,并确保连接字符串正确。 来源=的EntityFramework 如果我尝试提到这样的提供程序: “Data Source=D:\\Context.sdf;provider=System.Data.SqlServerCe.4.0” System.ArgumentException未处理 消息=不支持关键字:’provider’。 来源= System.Data

ConnectionString用于获取Excel文件问题

在名为(ExcelFiles)的文件夹中有一个Excel文件命名(a.xlsx)。 ExcelFiles文件夹位于我的项目的根目录中。 所以我获取excel文件数据的连接字符串是这样的: aaa =我的项目名称 使用此连接字符串,每个东西在本地都可以,但上传网站后我有一个错误。 问题出在哪儿?? 这条路是真的 – >〜/ ExcelFiles / a.xlsx与否 你能解决这条道路吗我… 解决路径问题后,错误是这样的: The ‘Microsoft.ACE.OLEDB.12.0’ provider is not registered on the local machine. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the […]

连接字符串麻烦MetaDataException

我的项目中有两个模型。 当我添加第二个模型时,我在所有帐户页面中都出现了此错误: System.Data.MetadataException: The specified schema is not valid. Errors: (8.6): error 0040: the nclob type is not qualified with a namespace or alias. Only primitive types can be used without qualification. At line 34 of `InitializeSimpleMembershipAttribute.cs` : using (var context = new UsersContext()) Ligne 33 : { Ligne 34 : if (!context.Database.Exists()) Ligne 35 […]

如何连接SDF数据库? 我试过没有连接字符串似乎工作

我已尝试在我的本地数据库的连接字符串上进行50多次不同的尝试,似乎没有任何效果。 我本质上只是试图打开数据库文件的连接,所以我可以转储我从excel电子表格中提取的数据。 我正在使用Visual C#制作离线winform应用程序。 无论我在app.config中尝试什么连接字符串,它总是在尝试将“dReader”写入数据库时​​失败。 错误通常取决于我尝试的字符串: “建立与SQL Server的连接时发生与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。validation实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 – 无法打开与SQL Server的连接)“ 我已经浏览了许多在线示例和资源,似乎没有任何工作。 我希望这里有人可以指出为什么会失败。 这是我最新forms的app.config: 这是我的表单代码: private void Profiles_Click(object sender, EventArgs e) { profilesDialog.FileName = “[YOUR_UPLOAD_FILE_HERE]”; var result = profilesDialog.ShowDialog(); if (result == DialogResult.OK) { HandleFileSelection(); } } private void HandleFileSelection() { var file = profilesDialog.FileName; // Create a connection to the file datafile.sdf in […]

Microsoft.ACE.OLEDB.12.0 CSV ConnectionString

我知道这种问题会不时被问到,但我找不到任何令人满意的解决方案。 如何使用MS ACE OLEDB 12打开CSV文件? 我尝试使用以下代码。 DbConnection connection = new OleDbConnection(); connection.ConnectionString = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\”Text;HDR=Yes\””; connection.Open(); DbCommand cmd; cmd = connection.CreateCommand(); cmd.CommandText = “SELECT * FROM [Mappe1#csv]”; DbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) Console.Write("(" + reader.GetValue(i).ToString() + ")"); Console.WriteLine(); } cmd.Dispose(); connection.Dispose(); Console.WriteLine("Done"); Console.ReadKey(); […]

EF 5.0和动态连接字符串?

有一件事让我现在发疯了。 我的(数据库优先)EF模型需要动态连接字符串(服务器的IP地址可能偶尔会改变)。 因此,在较旧的EF版本中,您可以通过构造函数传递连接字符串,但这在5.0中是不可能的。 到目前为止我所读到的内容,您可以更改数据模板,但每次重新生成模型等时都会被覆盖,因此不是最好的方法。 另一件事是SQLConnectionFactory,但这似乎根本不起作用(Database.DefaultConnectionFactory = new SqlConnectionFactory( … )似乎完全被忽略)。 什么是正确的方法?