这篇文章将为大家详细讲解有关.netMVC连接数据本地数据库方法有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
.net MVC 连接数据本地数据库三种方法
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="con" value="server=.\sqlexpress; user id = sa;password = a123456;database = xsgl1;max pool size=512;"/>
</appSettings>
<connectionStrings>
<add name="conSql" connectionString="server=(local)\sqlexpress; User Id = sa;password = a123456;database = xsgl1;max pool size=512;"/>
</connectionStrings>
Configuration
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
#region connect sql function one
SqlConnectionStringBuilder one = new SqlConnectionStringBuilder();
one.DataSource = "(local)\\sqlexpress";
one.InitialCatalog = "xsgl1";
one.UserID = "sa";
one.Password = "a123456";
one.MaxPoolSize = 512;
SqlConnection sct = new SqlConnection(one.ConnectionString);
#endregion
#region connect sql function two
//string conn = ConfigurationManager.AppSettings["con"].ToString();
//SqlConnection sct = new SqlConnection(conn);
#endregion
#region connect sql function three
//string conn = ConfigurationManager.ConnectionStrings["conSql"].ConnectionString;
//SqlConnection sct = new SqlConnection(conn);
#endregion
SqlCommand scm = new SqlCommand();
scm.Connection = sct;
scm.CommandType = CommandType.Text;
scm.CommandText = "select 课程名 from kc where 课程号='A001'";
sct.Open();
SqlDataReader sdr = scm.ExecuteReader();
if (sdr.Read())
{
ViewBag.hao = sdr["课程名"];
}
sdr.Close();
return View();
}
public ActionResult About()
{
return View();
}
}
Controller
@{
ViewBag.Title = "Index";
}
@ViewBag.hao
<h3>Index</h3>
关于.netMVC连接数据本地数据库方法有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。