.net MVC 連接數(shù)據(jù)本地數(shù)據(jù)庫三種方法總結(jié)
更新時間:2016年12月01日 11:37:14 作者:jinzhaoyoujiu
這篇文章主要介紹了.net MVC 連接數(shù)據(jù)本地數(shù)據(jù)庫三種方法總結(jié)的相關資料,這里附有代碼實例,需要的朋友可以參考下
.net MVC 連接數(shù)據(jù)本地數(shù)據(jù)庫三種方法
<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
<h2>Index</h2>
您可能感興趣的文章:
- ASP.NET MVC3關于生成純靜態(tài)后如何不再走路由直接訪問靜態(tài)頁面
- asp.net MVC實現(xiàn)無組件上傳圖片實例介紹
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- IIS7 配置大全(ASP.NET 2.0, WCF, ASP.NET MVC,php)
- ASP.NET MVC 5使用X.PagedList.Mvc進行分頁教程(PagedList.Mvc)
- ASP.NET MVC中為DropDownListFor設置選中項的方法
- 教你如何在 Javascript 文件里使用 .Net MVC Razor 語法
- 基于Asp.Net MVC4 Bundle捆綁壓縮技術(shù)的介紹
- Asp.net實現(xiàn)MVC處理文件的上傳下載功能實例教程
- ASP.NET MVC使用EasyUI的datagrid多選提交保存教程
相關文章
asp.net實現(xiàn)Gradview綁定數(shù)據(jù)庫數(shù)據(jù)并導出Excel的方法
這篇文章主要介紹了asp.net實現(xiàn)Gradview綁定數(shù)據(jù)庫數(shù)據(jù)并導出Excel的方法,涉及asp.net操作Gradview實現(xiàn)數(shù)據(jù)庫綁定及數(shù)據(jù)導出的相關技巧,非常簡單實用,需要的朋友可以參考下2015-11-11
ASP.NET Core應用錯誤處理之ExceptionHandlerMiddleware中間件呈現(xiàn)“定制化錯誤頁面”
這篇文章主要給大家介紹了關于ASP.NET Core應用錯誤處理之ExceptionHandlerMiddleware中間件呈現(xiàn)“定制化錯誤頁面”的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧2019-01-01
使用JavaScript代碼實現(xiàn)各種數(shù)據(jù)控件的反選功能 不要只做拖控件的菜鳥
在我們做許多項目的時候,會用到反選這個功能,但是我一般使用C#代碼創(chuàng)建數(shù)組遍歷實現(xiàn)功能,今天我想換一種語言實現(xiàn)一下,于是我就用JavaScript研究了一下怎么實現(xiàn)這個功能2011-12-12
ASP.NET 4.0配置文件中的ClientIDMode屬性詳解
在ASP.NET 4.0中的每個控件上都多了一個叫做ClientIDMode的屬性,本文主要介紹了ASP.NET 4.0配置文件中的ClientIDMode屬性詳解,非常具有實用價值,需要的朋友可以參考下2018-11-11
asp.net下無法循環(huán)綁定投票的標題和選項的解決方法
asp.net下無法循環(huán)綁定投票的標題和選項與無法循環(huán)獲得用戶的選擇的解決方法。2010-12-12
解決uploadify使用時session發(fā)生丟失問題的方法
這篇文章主要為大家詳細介紹了uploadify使用時發(fā)現(xiàn)session發(fā)生丟失問題的解決方法,遇到過類似問題的朋友可以參考本文進行解決2016-05-05

