下面是我将数据添加到数据库的代码.我使用SQL Server 2014和Visual Studio社区.我WebPageMovies
在服务器上创建了一个数据库.我可以在服务器资源管理器窗口中看到数据库,但不能在解决方案资源管理器窗口中看到.但在运行它时,它会返回错误
找不到连接字符串"WebPagesMovies"
码:
@{ var title = ""; var genre = ""; var year = ""; if (IsPost) { title = Request.Form["title"]; genre = Request.Form["genre"]; year = Request.Form["year"]; var db = Database.Open("WebPagesMovies"); var insertCommand = "INSERT INTO Movies (Title, Genre, Year) Values(@0, @1, @2)"; db.Execute(insertCommand, title, genre, year); Response.Redirect("~/Movies"); } }
Shekar.gvr.. 7
您可以通过多种方式添加它,即在
标签中或甚至在
标签中
要么
执行此操作后,请在.cs文件中读取您正在访问数据库的连接字符串.
private readonly SqlConnection connectionString = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["WebPagesMovies"]);
要么
private readonly SqlConnection connectionString = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WebPagesMovies"]);
分别取决于您添加连接字符串(
或
)的位置.
您可以通过多种方式添加它,即在
标签中或甚至在
标签中
要么
执行此操作后,请在.cs文件中读取您正在访问数据库的连接字符串.
private readonly SqlConnection connectionString = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["WebPagesMovies"]);
要么
private readonly SqlConnection connectionString = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WebPagesMovies"]);
分别取决于您添加连接字符串(
或
)的位置.