C# 使用 Visual Studio Express 版本连接到 SQL Server

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/188963/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:19:45  来源:igfitidea点击:

Connecting to SQL Server with Visual Studio Express Editions

c#visual-studio-2008visual-web-developer

提问by Tom Lianza

I find it odd that in Visual C# 2008 Express edition, when you use the database explorer, your options are:

我觉得奇怪的是,在 Visual C# 2008 Express 版中,当您使用数据库资源管理器时,您的选项是:

  1. Microsoft Access
  2. SQL Server Compact 3.5, and
  3. SQL Server Database File.
  1. 微软访问
  2. SQL Server Compact 3.5,以及
  3. SQL Server 数据库文件。

BUT if you use Visual Web Developer 2008 Express, you can connect to a regular SQL Server, Oracle, ODBC, etc.

但是,如果您使用 Visual Web Developer 2008 Express,则可以连接到常规 SQL Server、Oracle、ODBC 等。

For people developing command-line or other C# apps that need to talk to a SQL Server database, do you really need to build your LINQ/Data Access code with one IDE (Visual Web Developer) and your program in another (Visual C#)?

对于开发命令行或其他需要与 SQL Server 数据库通信的 C# 应用程序的人,您真的需要使用一个 IDE (Visual Web Developer) 构建您的 LINQ/数据访问代码并使用另一个 (Visual C#) 构建您的程序吗?

It's not a hard workaround, but it seems weird. If Microsoft wanted to force you to upgrade to Visual Studio to connect to SQL Server, why would they include that feature in one of their free IDEs but not the other? I feel like I might be missing something (like how to do it all in Visual C#).

这不是一个困难的解决方法,但它看起来很奇怪。如果 Microsoft 想要强制您升级到 Visual Studio 以连接到 SQL Server,为什么他们会在其免费 IDE 中包含该功能而不在另一个中包含该功能?我觉得我可能遗漏了一些东西(比如如何在 Visual C# 中做到这一切)。

采纳答案by Joel Coehoorn

You should be able to choose the SQL Server Database file option to get the right kind of database (the system.data.SqlClientprovider), and then manually correct the connection string to point to your db.

您应该能够选择 SQL Server 数据库文件选项来获取正确类型的数据库(system.data.SqlClient提供程序),然后手动更正连接字符串以指向您的数据库。

I think the reasoning behind those db choices probably goes something like this:

我认为这些数据库选择背后的原因可能是这样的:

  • If you're using the Express Edition, and you're notusing Visual Web Developer, you're probably building a desktop program.
  • If you're building a desktop program, and you're using the express edition, you're probably a hobbyist or uISV-er working at home rather than doing development for a corporation.
  • If you're not developing for a corporation, your app is probably destined for the end-user and your data store is probably going on their local machine.
  • You really shouldn't be deploying server-classdatabases to end-user desktops. An in-processdb like Sql Server Compact or MS Access is much more appropriate.
  • 如果您使用的是 Express Edition,并且您没有使用 Visual Web Developer,那么您可能正在构建一个桌面程序。
  • 如果您正在构建一个桌面程序,并且您使用的是 express 版本,那么您可能是一个在家工作的业余爱好者或 uISV-er,而不是为公司进行开发。
  • 如果您不是为公司开发,那么您的应用程序可能是面向最终用户的,而您的数据存储可能会在他们的本地机器上运行。
  • 您真的不应该将服务器级数据库部署到最终用户桌面。一个进程分贝如SQL Server精简或MS Access是更合适的。

However, this logic doesn't quite hold. Even if each of those 4 points is true 90% of the time, by the time you apply all four of them it only applies to ~65% of your audience, which means up to 35% of the express market might legitimately want to talk to a server-class db, and that's a significant group. And so, the simplified (greedy) version:

然而,这个逻辑并不完全成立。即使这 4 点中的每一个在 90% 的情况下都是正确的,当你应用所有四点时,它也只适用于大约 65% 的观众,这意味着多达 35% 的快递市场可能有理由想要谈论到服务器级数据库,这是一个重要的群体。因此,简化(贪婪)版本:

  • A real db server (and the hardware to run it) costs real money. If you have access to that, you ought to be able to afford at least the standard edition of visual studio.
  • 一个真正的数据库服务器(以及运行它的硬件)要花钱。如果您可以访问它,那么您至少应该能够负担得起 Visual Studio 的标准版。

回答by Mitchel Sellers

My guess is that with VWD your solutions are more likely to be deployed to third party servers, many of which do not allow for a dynamically attached SQL Server database file. Thus the allowing of the other connection type.

我的猜测是,使用 VWD,您的解决方案更有可能部署到第三方服务器,其中许多不允许动态附加 SQL Server 数据库文件。因此允许其他连接类型。

This difference in IDE behavior is one of the key reasons for upgrading to a full version.

IDE 行为的这种差异是升级到完整版本的关键原因之一。

回答by Joel Coehoorn

If you are using this to get a LINQ to SQL which I do and wanted for my Visual Developer, 1) get the free Visual WEB Developer, use that to connect to SQL Server instance, create your LINQ interface, then copy the generated files into your Vis-Dev project (I don't use VD because it sounds funny). Include only the *.dbml files. The Vis-Dev environment will take a second or two to recognize the supporting files. It is a little extra step but for sure better than doing it by hand or giving up on it altogether or EVEN WORSE, paying for it. Mooo ha ha haha.

如果您正在使用它来获取我为 Visual Developer 所做的 LINQ to SQL,1) 获取免费的 Visual WEB Developer,使用它连接到 SQL Server 实例,创建您的 LINQ 接口,然后将生成的文件复制到你的 Vis-Dev 项目(我不使用 VD,因为它听起来很有趣)。仅包括 *.dbml 文件。Vis-Dev 环境需要一两秒钟来识别支持文件。这是一个额外的步骤,但肯定比手工完成或完全放弃或者更糟糕的是,为它付费。莫哈哈哈哈。

回答by Z.K.

The only way I was able to get C# Express 2008 to work was to move the database file. So, I opened up SQL Server Management Studio and after dropping the database, I copied the file to my project folder. Then I reattached the database to management studio. Now, when I try to attach to the local copy it works. Apparently, you can not use the same database file more than once.

我能够让 C# Express 2008 工作的唯一方法是移动数据库文件。因此,我打开了 SQL Server Management Studio,在删除数据库后,我将文件复制到了我的项目文件夹中。然后我将数据库重新附加到管理工作室。现在,当我尝试附加到本地副本时,它可以工作。显然,您不能多次使用同一个数据库文件。

回答by Sergey Shandar

Workaround:

解决方法:

  1. Open your solution in Visual Web Developer Express. It will not load some of the projects in the solution but it is ok.
  2. Make a new connection in Database Explorer to the required database from SQL Server.
  3. Add a new class library project.
  4. Add a LINQ to SQL Classes item and link it to your database.
  5. Close the solution.
  6. Open the solution in Visual C# Express.
  1. 在 Visual Web Developer Express 中打开您的解决方案。它不会加载解决方案中的某些项目,但没关系。
  2. 在数据库资源管理器中建立一个从 SQL Server 到所需数据库的新连接。
  3. 添加一个新的类库项目。
  4. 添加 LINQ to SQL Classes 项并将其链接到您的数据库。
  5. 关闭解决方案。
  6. 在 Visual C# Express 中打开解决方案。

Now you have a LINQ to SQL classes library that is linked to your SQL Server database in Visual C# Express.

现在,您有一个链接到 Visual C# Express 中的 SQL Server 数据库的 LINQ to SQL 类库。

Update

更新

The solution is for Visual Studio Express 2010.

该解决方案适用于 Visual Studio Express 2010。

回答by Tribalinius

I just happened to have started my home business application in windows forms for the convenience. I'm currently using Visual C# Express 2010 / SQL Server 2008 R2 Express to develop it. I got the same problem as OP where I need to connect to an instance of SQL server. I'm skipping details here but that database will be a merged database synched between 2-3 computers that will also use the application I'm developing right now.

为方便起见,我刚好在 Windows 窗体中启动了我的家庭业务应用程序。我目前正在使用 Visual C# Express 2010 / SQL Server 2008 R2 Express 来开发它。我遇到了与 OP 相同的问题,我需要连接到 SQL 服务器的实例。我在这里跳过细节,但该数据库将是一个在 2-3 台计算机之间同步的合并数据库,这些计算机也将使用我现在正在开发的应用程序。

I found a quick workaround, at least, I think I did because I'm now able to use my stored procedures in tableadapters without any issues so far.

我找到了一个快速的解决方法,至少,我认为我做到了,因为我现在可以在 tableadapters 中使用我的存储过程,到目前为止没有任何问题。

I copy pasted an SQL connection that I used in a different project at work (VS2010 Premium) in the app.config and changed everything I needed there. When I went back to my Settings.settings, I just had to confirm that I wanted what was inside the app.config file. The only downsides I can see is that you can't "test" the connection since when you go inside the configuration of the connection string you can't go anywhere since "SQL Server" is not an option. The other downside is that you need to input everything manually since you can't use any wizards to make it work.

我在 app.config 中复制粘贴了我在另一个工作项目 (VS2010 Premium) 中使用的 SQL 连接,并在那里更改了我需要的所有内容。当我回到我的 Settings.settings 时,我只需要确认我想要 app.config 文件中的内容。我能看到的唯一缺点是你不能“测试”连接,因为当你进入连接字符串的配置时你不能去任何地方,因为“SQL Server”不是一个选项。另一个缺点是您需要手动输入所有内容,因为您无法使用任何向导来使其工作。

I don't know if I should have done it that way but at least I can connect to my SQL server now :).

我不知道我是否应该这样做,但至少我现在可以连接到我的 SQL 服务器 :)。

EDIT :

编辑 :

It only works with SQL Server 2008 R2 Express instances. If you try with SQL Server 2008 R2 Workgroup and up, you'll get a nasty warning from Visual C# 2010 Express telling you that "you can't use that connection with the current version of Visual Studio". I got that when I was trying to modify some of my tableadapters. I switched back to an SQL Express instance to develop and it's working fine again.

它仅适用于 SQL Server 2008 R2 Express 实例。如果您尝试使用 SQL Server 2008 R2 Workgroup 及更高版本,您将收到来自 Visual C# 2010 Express 的令人讨厌的警告,告诉您“您不能在当前版本的 Visual Studio 中使用该连接”。当我试图修改我的一些 tableadapters 时,我明白了。我切换回 SQL Express 实例进行开发,它再次正常工作。