简体   繁体   English

初始安装和从Visual Studio连接到Microsoft SQL Server的问题

[英]trouble with initial setup and connection to microsoft sql server from Visual Studio

I am on a Windows 10 machine, which has Visual Studio 2015. I am now trying to do projects with a database on the same machine. 我在装有Visual Studio 2015的Windows 10计算机上。我现在正在尝试在同一台计算机上使用数据库进行项目。 I seem to have SQL server ( I am really new to Databases ) Here is why I think I have a Database, which I can use: 我似乎有SQL Server(我对Databases真的很陌生),这就是为什么我认为我有一个Database(可以使用)的原因:

C:\Users\zohal>wmic product get name,version | findstr "SQL"
Microsoft SQL Server Compact 4.0 SP1 x64 ENU                                                                    4.0.8876.1

When I try to connect to a database from Visual Studio I do the following: click on Project > Add New Data Source new Window Comes up select Database new Window Comes up select Dataset the only option available, new Window Comes up select NewConnection new Window Comes up There are six options select Micorsoft SQL Server new Window Comes up but the pulldown is blank. 当我尝试从Visual Studio连接到数据库时,请执行以下操作:单击“项目”>“添加新数据源”,然后单击“新窗口出现”。选择“数据库新窗口出现”,选择“数据集”,唯一可用的选项,“新窗口出现”,选择“ NewConnection,新窗口出现”。向上有六个选项选择Micorsoft SQL Server新窗口出现,但下拉菜单为空白。 I looked in the services section of Computer Management application, and do not see a SQL service running. 我查看了“计算机管理”应用程序的“服务”部分,但没有看到正在运行的SQL服务。 I want to know how do I connect to this SQL server and does it work for what I want to do, which is to run .Net C# code with SQL statements? 我想知道如何连接到此SQL Server,它是否对我要执行的工作有效,即使用SQL语句运行.Net C#代码?

Update........... Guys, thank you for the content and advice. 更新...........伙计们,谢谢您的内容和建议。 I have tried both SQL compact and SQL Express, and ran into the following problems. 我同时尝试了SQL Compact和SQL Express,并遇到了以下问题。 I wonder if you can help, or perhaps I'll create a new post. 我想知道您是否可以提供帮助,或者我会创建一个新职位。 I have tried everything I could think of. 我已经尽力想了一切。 1) I tried to use the SQL compact. 1)我尝试使用SQL Compact。 Got as far as "Creating a SQL server Compact Database". 深入到“创建SQL Server Compact数据库”。 When I right click on the "App_Data", I just don't see an option of "SQL Server Compact 4.0 Local Database" as it stated in the procedures. 当我右键单击“ App_Data”时,我只是没有看到过程中所述的“ SQL Server Compact 4.0本地数据库”选项。 Here is an 这是一个 图片 of what I see. 我所看到的。 I did a search online for this type of issue, and the solution was to install a tools package. 我在线搜索了这种类型的问题,解决方案是安装工具包。 I did that. 我做到了 In fact I ran the Visual Studio install file, and chose the modify option and added all the tools, and support packages I could think of. 实际上,我运行了Visual Studio安装文件,并选择了Modify选项,并添加了所有我能想到的工具和支持包。 I rebooted the machine also; 我也重新启动了机器; no change. 没变。 I also ended up installing SQLExpress, and followed this link to set it up, but when I click on a new connection option I do not see the SQL Express option in the server name pull down as it states in the procedure. 我还结束了安装SQLExpress的操作,并单击了链接进行设置,但是当我单击一个新的连接选项时,服务器名称中的SQL Express选项没有按过程中的说明下拉。 I basically get as far as creating the project and three steps into the procedure. 我基本上可以完成创建项目的过程,并完成了三个步骤。 I can put the projects on Git if that helps. 如果有帮助,我可以将项目放到Git上。 My immediate goal is to create a table and query it. 我的近期目标是创建一个表并对其进行查询。 Here the code in my project: 这是我项目中的代码:

var query = from c in db.Customers
                        where c.City == "Nantes"
                        select new { c.City, c.CompanyName };

Right now, I get an error under "c" and "db" as I have not created a connection. 现在,由于没有创建连接,因此在“ c”和“ db”下出现错误。 While I don't know how to do that yet, I do know that is not a huge task. 虽然我还不知道该怎么做,但我知道这不是一项艰巨的任务。 But if I can not connect to a Database, this does not help me much. 但是,如果我无法连接到数据库,这对我没有太大帮助。 I did find the right assemblies and added them to the beginning of my project. 我确实找到了正确的程序集并将其添加到我的项目的开头。 I am talking about 我在说

using System.Linq.Expressions;
using System.Data.SqlServerCe;

Just tell me what to do. 告诉我该怎么办。 Should post again or can help me? 应该再次发布还是可以帮助我?

Update: Thank you. 更新:谢谢。 The MVC .Net Core MVC procedures worked. MVC .Net Core MVC程序正常工作。 Thank you so much. 非常感谢。

If you are not developing mobile (win mobile) apps, you need to download SQL server express edition which is suitable for any database application built for .NET and targeting the desktop/web apps. 如果您不是在开发移动(成功移动)应用程序,则需要下载SQL Server Express Edition,该版本适用于为.NET构建且针对桌面/ Web应用程序的任何数据库应用程序。

To install the latest SQL express 2016, please visit this link or do some search on the web: https://www.microsoft.com/en-us/sql-server/sql-server-editions-express 要安装最新的SQL Express 2016,请访问此链接或在网上进行一些搜索: https : //www.microsoft.com/zh-cn/sql-server/sql-server-editions-express

SQL Server Compact is not a full fledged database service. SQL Server Compact不是完整的数据库服务。 It is a way to store data in a file and run sql like queries against it. 这是一种将数据存储在文件中并像对它的查询一样运行sql的方法。

Here is a short walk through of how to connect to a sql compact database: https://msdn.microsoft.com/en-us/library/gg606540(v=vs.100).aspx 这是有关如何连接到SQL紧凑型数据库的简短说明: https : //msdn.microsoft.com/zh-cn/library/gg606540​​(v=vs.100).aspx

If you want to use an actual sql server you can download and install SQL Server Express for free. 如果要使用实际的sql服务器,则可以免费下载和安装SQL Server Express。 Here is a walk through for that: https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application 这是一个逐步的说明: https : //www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model- asp-net-mvc应用程序

Both of the linked examples use Entity Framework to connect which is an ORM . 两个链接的示例都使用实体框架进行连接,这是一个ORM There are many ways to connect to a database in C#. 有许多方法可以使用C#连接到数据库。 But since you mentioned running sql statements directly you may want to look into Dapper.Net which is a mirco ORM that let's you run SQL statements and map the results back to objects. 但是,由于您提到直接运行sql语句,因此您可能需要研究Dapper.Net ,这是一个mirco ORM,可让您运行SQL语句并将结果映射回对象。

I would first verify in "Control Panel > Programs > Programs and Features" to check whether SQL Server is installed on the machine. 我首先要在“控制面板>程序>程序和功能”中进行验证,以检查计算机上是否安装了SQL Server。 You should see program with name "Microsoft SQL Server 20xx Setup". 您应该看到名称为“ Microsoft SQL Server 20xx安装程序”的程序。 If you don't see any such names, then I suggest you to download and install a fresh copy of SQL Server. 如果看不到任何此类名称,则建议您下载并安装SQL Server的新副本。

Microsoft® SQL Server® 2014 Express Microsoft®SQLServer®2014 Express

If you do see it in control panel, then open RUN (Windows Key + R) and type SSMS and press enter. 如果确实在控制面板中看到它,则打开RUN(Windows键+ R)并键入SSMS,然后按Enter。 This should open SQL Server Management Studio. 这应该打开SQL Server Management Studio。 (Sometimes SQL Server might be installed but Management Studio might not be. You can download Management Studio as well from the link provided above) (有时可能已安装SQL Server,但可能未安装SQL Server。您也可以从上面提供的链接下载Management Studio。)

After opening management studio, enter the server name and click on "Connect". 打开Management Studio后,输入服务器名称,然后单击“连接”。 If you don't know server name enter . 如果您不知道服务器名称,请输入。 (dot) and click "Connect". (点),然后单击“连接”。 Now you should be connected to SQL Server. 现在,您应该已连接到SQL Server。

You should be able to connect C# code with SQL Server using a connection string. 您应该能够使用连接字符串将C#代码与SQL Server连接。

Different formats of connection strings 不同格式的连接字符串

How to store connection strings in web.config or app.config 如何在web.config或app.config中存储连接字符串

Execute a command in c# by connecting to SQL Server 通过连接到SQL Server在C#中执行命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM