如何将 ado.net 数据库连接到 ASP.Net MVC 5
How to connect ado.net database to ASP.Net MVC 5
我是 ASP.Net MVC 5 的新手,之前使用 ASP.Net Webforms 和 Ado.Net 数据库连接创建了两个应用程序。
我看过很多文章解释如何在 ASP.Net MVC 中使用 Ado.Net 连接,但所有教程都使用 Entity Framework。有没有办法只连接Ado.Net?
在我的 WebForm 应用程序中,我一直在使用此连接代码:
connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
sql = "SQL Statement";
cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
cmd = new SqlCommand(sql, cnn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cnn.Close();
}
是否可以在 ASP .Net MVC5 中使用它?
当我尝试时出现此错误:
Cannot open database "online_recharge" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\DefaultAppPool'.
这是我尝试使用的代码:
public ActionResult Add_Balance()
{
string record;
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
SqlCommand sqlCmd = new SqlCommand("Select * from COMMISSION_PACKAGE",sqlConnection);
SqlDataReader reader = sqlCmd.ExecuteReader();
record = reader["PKG_NM"].ToString();
sqlConnection.Close();
}
ViewBag.Demo = record;
return View();
}
没有什么能阻止您在 MVC.Entity 中使用 ADO.NET 框架是一种非常流行的 ORM,它的设置非常简单,这就是为什么人们喜欢在需要时将它用于 tutorials.But一种更 custom\controlled 的数据访问方式,您仍然可以使用 ADO.NET.
提示 - 不要将您的连接存储在代码中,这被认为是错误的 practice.If 数据库已移动到不同的服务器,您将需要更改代码并重新编译整个 application.You 应将连接字符串存储在 *.config
文件中:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"/>
</connectionStrings>
并且可以像这样从您的代码访问它:
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
我是 ASP.Net MVC 5 的新手,之前使用 ASP.Net Webforms 和 Ado.Net 数据库连接创建了两个应用程序。
我看过很多文章解释如何在 ASP.Net MVC 中使用 Ado.Net 连接,但所有教程都使用 Entity Framework。有没有办法只连接Ado.Net?
在我的 WebForm 应用程序中,我一直在使用此连接代码:
connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
sql = "SQL Statement";
cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
cmd = new SqlCommand(sql, cnn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cnn.Close();
}
是否可以在 ASP .Net MVC5 中使用它?
当我尝试时出现此错误:
Cannot open database "online_recharge" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\DefaultAppPool'.
这是我尝试使用的代码:
public ActionResult Add_Balance()
{
string record;
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
SqlCommand sqlCmd = new SqlCommand("Select * from COMMISSION_PACKAGE",sqlConnection);
SqlDataReader reader = sqlCmd.ExecuteReader();
record = reader["PKG_NM"].ToString();
sqlConnection.Close();
}
ViewBag.Demo = record;
return View();
}
没有什么能阻止您在 MVC.Entity 中使用 ADO.NET 框架是一种非常流行的 ORM,它的设置非常简单,这就是为什么人们喜欢在需要时将它用于 tutorials.But一种更 custom\controlled 的数据访问方式,您仍然可以使用 ADO.NET.
提示 - 不要将您的连接存储在代码中,这被认为是错误的 practice.If 数据库已移动到不同的服务器,您将需要更改代码并重新编译整个 application.You 应将连接字符串存储在 *.config
文件中:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"/>
</connectionStrings>
并且可以像这样从您的代码访问它:
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;