C# 在 Visual Studio 2010 中创建和使用本地数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14430485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating and utilizing a local database in Visual Studio 2010
提问by Alex
I can't seem to find any straight-forward guides on how to create a windows forms application with C# .NET in Visual Studio 2010 that uses a local database. When I create the local database, a .sdf file, I can't figure out how to have my form access it. All the discussions I have been able to find on it seem to assume I know more than I do on the subject. Can anyone direct me towards a straight-forward beginner's guide on the subject, or outline the steps I need to take?
我似乎找不到任何关于如何在使用本地数据库的 Visual Studio 2010 中使用 C# .NET 创建 windows 窗体应用程序的直接指南。当我创建本地数据库,一个 .sdf 文件时,我不知道如何让我的表单访问它。我所能找到的所有讨论似乎都假设我对这个主题的了解比我知道的多。任何人都可以指导我获得有关该主题的直接初学者指南,或概述我需要采取的步骤吗?
采纳答案by Stephen Byrne
回答by Lord Darth Vader
I found this library very helpful
我发现这个库非常有帮助
https://github.com/martincostello/sqllocaldb
https://github.com/martincostello/sqllocaldb
Once added via nuget its very easy to use
通过 nuget 添加后,它非常易于使用
ISqlLocalDbProvider provider = new SqlLocalDbProvider();
ISqlLocalDbInstance instance = provider.GetOrCreateInstance("MyInstance");
instance.Start();
using (SqlConnection connection = instance.CreateConnection())
{
connection.Open();
// Use the connection...
}
instance.Stop();