简体   繁体   English

使用现有SQL数据库的ASP.NET Visual Studio 2012

[英]ASP.NET Visual Studio 2012 using existing SQL Database

I am using Visual Studio 2012 to create an ASP.NET web application. 我正在使用Visual Studio 2012创建ASP.NET Web应用程序。 I tried using the Getting Starting with ASP.NET 4.5 Tutorial but it creates a simple local database and all queries are written directly in the code. 我尝试使用“ ASP.NET 4.5入门”,但是它创建了一个简单的本地数据库,所有查询都直接编写在代码中。 The database that I am accessing (SQL Server 2008) has fifteen complex stored procedures that I really don't want to have to retype. 我正在访问的数据库(SQL Server 2008)具有十五个复杂的存储过程,而我真的不想重新输入这些过程。

Using the DBContext example in the tutorial works fine when just grabbing all of the data from the tables, but how do I use the stored procedures that are in the database? DBContext表中获取所有数据时,使用本教程中的DBContext示例就可以很好地工作,但是如何使用数据库中的存储过程? Can someone please tell me the best way to use the stored procedures that already exist? 有人可以告诉我使用现有存储过程的最佳方法吗?

All of the questions (and answers) I've found so far are dealing with earlier versions of Visual Studio, and although I know that I could use these (since VS 2012 does support the backward compatibility), I want to make the best use of the software that I have and not use "best practices" from VS 2010. 到目前为止,我发现的所有问题(和答案)都在处理Visual Studio的早期版本,尽管我知道我可以使用这些问题(因为VS 2012确实支持向后兼容),但我还是想充分利用我拥有的软件中没有使用VS 2010中的“最佳实践”。

If you can tell me how to use the existing stored procedures, or even direct me to a book, website, or anything else that would show this to me, I would TRULY appreciate it! 如果您可以告诉我如何使用现有的存储过程,或者甚至将我定向到一本书,网站或其他任何可以向我显示的内容,我将非常感谢! Happy coding! 编码愉快! And thanks for your time! 并感谢您的宝贵时间!

I had to piece together info from a few different sources, but I was able to get this working. 我不得不从几个不同的来源整理信息,但是我能够使它工作。 Here are my steps (I may have done more than I needed, but it worked) just in case it may help someone else: 这是我的步骤(我可能做了很多事,但确实有效),以防它可能对其他人有所帮助:

  1. Added a class to my project Called MyContext, and told it was a DBContext and pointed it to my existing database like this: (Please note that I did already have the full Connection String in my webconfig file under . 在我的项目中添加了一个名为MyContext的类,并告知它是一个DBContext,并按如下所示将其指向我的现有数据库:(请注意,我的webconfig文件中已经具有完整的Connection String。

    { public class MyContext : DbContext { public MyContext() : base("name=MyDatabase") { } } } {public class MyContext:DbContext {public MyContext():base(“ name = MyDatabase”){}}}

MyDatabase is replaced with the actual name that refers to my database in the connection string of my webconfig file. MyDatabase被替换为Webconfig文件的连接字符串中引用我的数据库的实际名称。

  1. I created a class called ProductList which exposes only the fields that are returned by my stored procedure: 我创建了一个名为ProductList的类,该类仅公开存储过程返回的字段:

    { public class ProductList { [ScaffoldColumn(false)] public int ProductID { get; {public class ProductList {[ScaffoldColumn(false)] public int ProductID {get; set; 组; } [Required, StringLength(100), Display(Name = "Model")] public string ProductName { get; } [必需,StringLength(100),Display(Name =“ Model”)]公共字符串ProductName {get; set; 组; } } }}

} }

  1. I actually created a user control (.ascx) which is a just a DataReapeater. 我实际上创建了一个用户控件(.ascx),它只是一个DataReapeater。 In the code-behind on the control, right beneather the Page_Load, I created a Method called GetMostPopular: 在控件背后的代码中,在Page_Load的正下方,我创建了一个名为GetMostPopular的方法:

    public IEnumerable GetMostPopular() { var db = new MyContext(); 公共IEnumerable GetMostPopular(){var db = new MyContext(); IEnumerable result = db.Database.SqlQuery("MostPopularProducts"); IEnumerable结果= db.Database.SqlQuery(“ MostPopularProducts”); return result; 返回结果; } }

Inside the <> of IEnumerable and SQLQuery is the name of the Class I created. 在IEnumerable和SQLQuery的<>内是我创建的类的名称。 It as the exact same fields that will be returned by my stored procedure "MostPopularProducts". 它与我的存储过程“ MostPopularProducts”将返回的字段完全相同。

  1. Then in the DataRepeater, I used GetMostPopular as the "SelectMethod": 然后在DataRepeater中,我将GetMostPopular用作“ SelectMethod”:


    '><%# Eval("ProductName") %> '> <%#Eval(“ ProductName”)%>

5.Then I just dragged and dropped the User Control onto the page where I wanted it to display. 5.然后,我只是将用户控件拖放到想要显示的页面上。

I hope this helps someone else. 我希望这可以帮助其他人。 Happy coding! 编码愉快!

我建议您使用SqlClient,SqlCommand,SqlConnection ...非常简单,带有有机输出!

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

相关问题 Visual Studio 19 无法识别 ASP.NET 的 SQL Server 2012 - Visual Studio 19 not recognising SQL Server 2012 for ASP.NET 使用Visual Studio 2012部署ASP.NET网站(.NET 2) - Deploy ASP.NET Website (.NET 2) using Visual Studio 2012 使用Visual Studio 2012将具有数据库的ASP.net Web应用程序发布到Azure - Publishing ASP.net web application with database to Azure using Visual Studio 2012 使用Visual Studio 2012在ASP.NET上运行WPF应用程序 - Run WPF application on ASP.NET using Visual Studio 2012 如何使用Visual Studio 2013连接到asp.net中的本地SQL Server数据库? - How to connect to a local SQL server database in asp.net using Visual Studio 2013? 无法在Visual Studio 2012的Asp.net中使用SQL Server CE 4.0 - Can't use sql server ce 4.0 in asp.net in visual studio 2012 将ASP.NET Web项目上的现有数据从Visual Studio中的本地数据库部署到Azure数据库 - Deploy existing data on ASP.NET web project from local database in Visual Studio to Azure database 在Visual Studio 2012中更改asp.net成员表的目录 - changing directory of asp.net membership tables in visual studio 2012 Visual Studio 2010和2012 ASP.Net调试和布局问题 - Visual Studio 2010 & 2012 ASP.Net debug & layout issues ASP.Net成员资格(Visual Studio 2012和Web窗体) - ASP.Net Membership (Visual Studio 2012 & Web Forms)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM