使用 Visual Studio Express 版本连接到 SQL Server
我觉得奇怪的是,在 Visual C# 2008 Express 版本中,当您使用数据库资源管理器时,您的选项是:
- Microsoft Access
- SQL Server Compact 3.5 和
- SQL Server 数据库文件。
但是,如果您使用 Visual Web Developer 2008 Express,则可以连接到常规 SQL Server、Oracle、ODBC 等。
对于开发需要与 SQL Server 数据库通信的命令行或其他 C# 应用程序的人来说,您真的需要使用一个 IDE (Visual Web Developer) 构建 LINQ/数据访问代码,并在另一个 IDE (Visual C#) 中构建程序?
这不是一个困难的解决方法,但看起来很奇怪。 如果 Microsoft 想要强制您升级到 Visual Studio 以连接到 SQL Server,为什么他们会在其中一个免费 IDE 中包含该功能,而不是在另一个 IDE 中呢? 我觉得我可能错过了一些东西(比如如何在 Visual C# 中完成这一切)。
I find it odd that in Visual C# 2008 Express edition, when you use the database explorer, your options are:
- Microsoft Access
- SQL Server Compact 3.5, and
- SQL Server Database File.
BUT if you use Visual Web Developer 2008 Express, you can connect to a regular SQL Server, Oracle, ODBC, etc.
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#)?
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#).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该能够选择 SQL Server 数据库文件选项来获取正确类型的数据库(
system.data.SqlClient
提供程序),然后手动更正连接字符串以指向您的数据库。我认为这些数据库选择背后的原因可能是这样的:
然而,这个逻辑并不完全成立。 即使这 4 点中的每一点在 90% 的情况下都是正确的,当您应用所有这四点时,它只适用于大约 65% 的受众,这意味着高达 35% 的快递市场可能有理由想要谈论到服务器级数据库,这是一个重要的群体。 因此,简化(贪婪)版本:
You should be able to choose the SQL Server Database file option to get the right kind of database (the
system.data.SqlClient
provider), and then manually correct the connection string to point to your db.I think the reasoning behind those db choices probably goes something like this:
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:
解决方法:
现在您有了一个 LINQ to SQL 类库,它链接到 Visual C# Express 中的 SQL Server 数据库。
更新
该解决方案适用于 Visual Studio Express 2010。
Workaround:
Now you have a LINQ to SQL classes library that is linked to your SQL Server database in Visual C# Express.
Update
The solution is for Visual Studio Express 2010.
我的猜测是,使用 VWD,您的解决方案更有可能部署到第三方服务器,其中许多服务器不允许动态附加 SQL Server 数据库文件。 因此允许其他连接类型。
IDE 行为的这种差异是升级到完整版本的关键原因之一。
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.
This difference in IDE behavior is one of the key reasons for upgrading to a full version.
如果您使用它来获取 LINQ to SQL(我为 Visual Developer 做的和想要的),1)获取免费的 Visual WEB Developer,使用它连接到 SQL Server 实例,创建 LINQ 接口,然后将生成的文件复制到你的 Vis-Dev 项目(我不使用 VD,因为它听起来很有趣)。 仅包括 *.dbml 文件。 Vis-Dev 环境将需要一两秒钟来识别支持文件。 这是一个额外的步骤,但肯定比手动完成或完全放弃或更糟糕的是付费更好。 哞哈哈哈哈。
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.
我能够让 C# Express 2008 正常工作的唯一方法是移动数据库文件。 因此,我打开 SQL Server Management Studio,删除数据库后,将文件复制到项目文件夹中。 然后我将数据库重新连接到管理工作室。 现在,当我尝试附加到本地副本时,它可以工作。 显然,您不能多次使用同一个数据库文件。
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.
为了方便起见,我只是碰巧在 Windows 窗体中启动了我的家庭业务应用程序。 我目前正在使用 Visual C# Express 2010 / SQL Server 2008 R2 Express 来开发它。 我遇到了与 OP 相同的问题,我需要连接到 SQL 服务器的实例。 我在这里跳过详细信息,但该数据库将是在 2-3 台计算机之间同步的合并数据库,这些计算机也将使用我现在正在开发的应用程序。
我找到了一个快速的解决方法,至少我认为我做到了,因为我现在可以在表适配器中使用我的存储过程,到目前为止没有任何问题。
我将在工作中的另一个项目(VS2010 Premium)中使用的 SQL 连接复制粘贴到 app.config 中,并更改了我需要的所有内容。 当我返回到 Settings.settings 时,我只需确认我想要 app.config 文件中的内容。 我看到的唯一缺点是您无法“测试”连接,因为当您进入连接字符串的配置时,您无法去任何地方,因为“SQL Server”不是一个选项。 另一个缺点是您需要手动输入所有内容,因为您无法使用任何向导来使其工作。
我不知道我是否应该这样做,但至少我现在可以连接到我的 SQL 服务器:)。
编辑:
它仅适用于 SQL Server 2008 R2 Express 实例。 如果您尝试使用 SQL Server 2008 R2 Workgroup 及更高版本,您会收到来自 Visual C# 2010 Express 的令人讨厌的警告,告诉您“您无法在当前版本的 Visual Studio 中使用该连接”。 当我尝试修改我的一些表适配器时,我得到了这一点。 我切换回 SQL Express 实例进行开发,它再次运行良好。
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.
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.
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.
I don't know if I should have done it that way but at least I can connect to my SQL server now :).
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.