.net MVC 连接数据本地数据库三种方法总结

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了.net MVC 连接数据本地数据库三种方法总结脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

.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
 <h2>Index</h2>

脚本宝典总结

以上是脚本宝典为你收集整理的.net MVC 连接数据本地数据库三种方法总结全部内容,希望文章能够帮你解决.net MVC 连接数据本地数据库三种方法总结所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:.netMVC