如何在Asp.Net中使用Sql Server? [英] How Do I Use Sql Server In Asp.Net?

查看:87
本文介绍了如何在Asp.Net中使用Sql Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在我的应用程序中使用sql server。即,我需要一个安全的数据库(用户名和密码,但我不知道如何实现,因为我不想使用localdb。请帮助

解决方案

< blockquote>虽然我的这篇文章不是关于ASP.NET的,但是它概述了使用C#连接到SQL Server。如何将SQL数据库连接到您的C#程序,初学者的教程 [ ^ ]。您应该考虑为 SqlClient [ ^ ]名称空间首先,通常,这个类用于连接和执行SQL Server上的命令。 SqlConnection SqlCommand SqlParameter 还有更多在此命名空间中声明的对象



很可能您将能够编写自己的连接器以连接到SQL Server并执行您自己的语句。在ASP.NET中,Entity Framework是一个使用SQL Server的好框架,EF允许您专注于您的逻辑并抽象数据操作层并将其留给.NET框架。请阅读Mika的解决方案1 ​​



编辑



因为你正在开发这个应用程序,所以很好使用localDb而不是购买SQL Server许可证并安装整个服务器来运行它。您应该明白,即使运行SQL Server Express也需要您安装服务器,如果没有它,您的数据库将无法运行。 LocalDB是一个非常紧凑的版本(就像个人提示一样,我认为SQL Server CE已升级到SQL Server LocalDB,因为Microsoft不再正式发布SQL Server CE ),而LocalDB随附运行数据库所需的一些必需文件和程序集(与SQL Server CE类似)。



然后,在托管环境中,您可以在那里创建数据库。 IMO,最好在其服务器上创建新的数据库副本,而不是在那里部署数据库文件。当您尝试在服务器上部署应用程序时,许多身份验证和类似问题都会引发。



由于这些原因,我建议您继续使用LocalDB进行编程和开发,当您的Web应用程序部署在托管环境中时,您应该运行脚本来生成基于环境的数据库文件。 :-)



有关LocalDB的更多信息,读取此内容 [ ^ ]。在Microsoft停止支持SQL Server CE之后,我喜欢SQL Server LocalDB。它们节省了大量时间,我甚至不必运行任何后台服务器来获取数据。



您还应注意,托管服务提供商通常不支持LocalDB,它们为您提供SQL Server 2008-2014的许可副本。



我如何上传数据库?正如我所说,你不应该。您不确定在托管环境中将提供哪些程序集。为此,我说当你在那里上传你的文件时,在服务器端创建一个新的数据库。手动或通过脚本执行此操作。



正如Mika所提到的,LocalDB不是用于获取SQL Server副本所需的商业用途,但更好的选择是搜索一份 托管服务器上的SQL Server 其他功能,例如广告,付款处理是一个完全不同的话题。 : - )


根据您的需要,有很多教程可供使用。例如:

- 使用SqlClient类初学者指导通过C#访问SQL Server [ ^ ]

- 带MVC的实体框架 Entity Framework 6入门Code First使用MVC 5 [ ^ ]

- 简称EF 实体框架教程 [ ^ ]


Hello, i want to use sql server in my application. i.e, i need a database with security(username and password but i don,t know how to archieve that because i don't want to use localdb. Pls, help

解决方案

Although this article of mine is not about ASP.NET, but it gives you an overview of connecting to SQL Server using C#. How to connect SQL Database to your C# program, beginner's tutorial[^]. You should consider learning a few concepts for SqlClient[^] namespace first, generally, it is this class that is used to connect to and execute commands on SQL Server. SqlConnection, SqlCommand, SqlParameter and many more are objects declared in this namespace.

Chances are that you will be able to write your own connectors to connect to SQL Server and execute your own statements. In ASP.NET, Entity Framework is a good framework to work with SQL Server, EF allows you to focus on your logic and abstract the data manipulation layer and leave it to .NET framework. Read Solution 1 by Mika for that.

Edit

Since you are developing the application, it is good to use localDb rather than purchasing a SQL Server license and installing an entire server to run it. You should understand that even running SQL Server Express requires you to install the server, without it, your database won't work. LocalDB is a very compact edition (just as a personal tip, I think that SQL Server CE was upgraded to SQL Server LocalDB, because Microsoft is no longer publishing SQL Server CE officially), and LocalDB comes shipped with a few required files and assemblies (just similar to SQL Server CE) which are needed to run your database.

Then, on the hosting environment you can create a database there. IMO, it is better to create a new copy of database on their server rather than deploying your database files there. Many problems of authentication and similar raise when you try to deploy your application on server.

For these reasons, I would suggest that you continue programming and development with LocalDB, and when your web application gets deployed on hosting environment, you should run a script to generate a database file based on the environment. :-)

For more on LocalDB, read this[^]. I enjoy SQL Server LocalDB, after Microsoft stopped support for SQL Server CE. They save a lot of time and I don't even have to run any background server to get the data.

You should also note that hosting providers usually do not support LocalDB, they provide you with a licensed copy of either SQL Server 2008-2014.

How would I upload the database? As I said, you should not. You are not sure of what assemblies you will be provided on hosting environment. For this sake, I said create a new database on the server end when you upload your files there. Do that manually or by script.

As Mika has mentioned, LocalDB is not intended for commercial use you would require to get a copy of SQL Server, but a better choice is to search for a copy of SQL Server on the hosting server. Other features, such as advertising, payment processing is a totally different thing to talk about. :-)


There are a lot of tutorials available depending on your needs. For example:
- Using SqlClient classes Beginners guide to accessing SQL Server through C#[^]
- Entity Framework with MVC Getting Started with Entity Framework 6 Code First using MVC 5[^]
- and simply EF Entity Framework Tutorials[^]


这篇关于如何在Asp.Net中使用Sql Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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