连接到基于服务的数据源在C# [英] Connecting to a Service-Based Datasource in C#

查看:126
本文介绍了连接到基于服务的数据源在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常noobish的问题:



我不明白SQL。我试图做一个通用连接到一个空的SQL数据库在C#。我不想提前创建表,我只想要能够打开连接。



每个connectionstring我尝试结果InvalidOperationException需要一些帮助理解在Visual Studio中连接到SQL数据文件的正确语法。



我所做的是去添加 - >新项目 - >服务 - 基于数据库的数据库



这给我一个解决方案资源管理器中的Database1.mdf。





我相信下一步是使用:SQLConnection SQLConn = new SQLConnection(...);



但我不知道在connectionstring中为server或database >

有人可以告诉我在C#中连接到基于服务的数据源的最基本方法吗?



基于服务的数据库包括在最终编译?例如。如果我编译一个完成的可执行文件,每个安装都会有自己的本地SQL数据库吗?

解决方案

嵌入在dll中。请参阅下面的最后一节(以粗体显示)以了解如何获取连接字符串。



从MSDN文章( http://msdn.microsoft.com/en-us/library/vstudio/ms233763 %28v = vs.110%29.aspx ):



要创建项目和数据库文件: >


  1. 创建名为SampleDatabaseWalkthrough的Windows Forms项目。




  2. 在菜单栏上选择项目,添加新项目。




  3. 在模板列表中,向下滚动直到基于服务的数据库


  4. 命名数据库SampleDatabase,然后选择添加按钮。




  5. 在选择数据库模型页面上,选择数据集图标,然后选择下一步按钮。


  6. p>选择完成按钮创建数据库和数据集并将其添加到项目。



    数据库的属性窗口显示连接字符串和位置的数据库的主要.mdf文件。要显示此窗口,请选择服务器资源管理器选项卡,展开数据连接节点,打开数据库的快捷菜单,然后选择属性。



This is a very noobish question:

I don't understand SQL. I'm trying to make a generic connection to an empty SQL database in C#. I don't want to make tables in advance, I just want to be able to open the connection.

Every "connectionstring" I try results in "InvalidOperationException" so I need some help understanding the proper syntax to connect to a SQL data file in Visual Studio.

What I have done is gone to "Add -> New Item -> Service-based Database"

This gives me a "Database1.mdf" in the solution explorer.

Now I want to open a connection through C# to this file at which point I can use SQL strings to manipulate the file.

I believe the next step is to use: SQLConnection SQLConn = new SQLConnection(" ... ");

But I don't know what to put for the "server" or the "database" in the connectionstring.

Can someone tell me the most basic way to connect to a "Service-based Datasource" in C#?

Will this "service-based database" be included in the final compile? E.g. if I compile a finished executable, will each installation come with its own local SQL database?

解决方案

The service based database will not be embedded in the dll. See the last section of the below (in bold) to see how to obtain the connection string.

From an MSDN article (http://msdn.microsoft.com/en-us/library/vstudio/ms233763%28v=vs.110%29.aspx):

To create a project and a database file:

  1. Create a Windows Forms project that's named SampleDatabaseWalkthrough.

    See Creating Solutions and Projects.

  2. On the menu bar, choose Project, Add New Item.

    The Add New Item dialog box appears so that you can add items that are appropriate in a windows Form project.

  3. In the list of templates, scroll down until Service-based Database appears, and then choose it.

  4. Name the database SampleDatabase, and then choose the Add button.

    The Data Source Configuration Wizard opens.

  5. On the Choose a Database Model page, choose the Dataset icon, and then choose the Next button.

  6. On the Choose Your Database Objects page, no database objects are available because the database is new.

  7. Choose the Finish button to create the database and dataset and add them to the project.

    The properties window for the database shows the connection string and the location of the primary .mdf file for the database. To display this window, choose the Server Explorer tab, expand the Data Connections node, open the shortcut menu for the database, and then choose Properties.

这篇关于连接到基于服务的数据源在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆