这是我的数据库在Microsoft SQL Server Management Studio下的服务器上的样子
我希望如果我把我的数据库代码和连接代码放在一起,你们可以告诉我什么是错的?我不确定问题是什么,我很确定代码没问题。
这是我为电影创建的数据库 - MovieDB
CREATE TABLE [dbo].[movie]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[genre] [varchar](50) NOT NULL,
[price] [float] NOT NULL,
[rating] [float] NOT NULL,
[director] [varchar](50) NOT NULL,
[image] [varchar](255) NULL,
[review] [text] NOT NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[movie] ON
INSERT [dbo].[movie] ([id], [name], [genre], [price], [rating], [director], [image], [review]) VALUES (1, N'Guardians of the Galaxy', N'Action', 19.99, 7.6, N'James Gunn', N'../Images/Movies/GOTG-poster.jpeg', N'Gunn makes this huge entertainment accessible to the converted and the neophyte alike, and he has only has one goal: To send you out of the theater with a fat smile on your face. Mission accomplished.')
INSERT [dbo].[movie] ([id], [name], [genre], [price], [rating], [director], [image], [review]) VALUES (2, N'The Godfather', N'Drama', 9.99, 100, N'Francis Ford Coppola', N'../Images/Movies/Godfather.jpeg', N' The wedding sequence... is a virtuoso stretch of filmmaking: Coppola brings his large cast onstage so artfully that we are drawn at once into the Godfather world.')
INSERT [dbo].[movie] ([id], [name], [genre], [price], [rating], [director], [image], [review]) VALUES (3, N'Dawn of the Planet of the Apes', N'Action', 19.99, 7.9, N'Matt Reeves', N'../Images/Movies/Ape.jpeg', N' Director Matt Reeves, working from a script by Rick Jaffa, Amanda Silver and Mark Bomback, elevates the apes to primary importance in this intelligent thriller.')
INSERT [dbo].[movie] ([id], [name], [genre], [price], [rating], [director], [image], [review]) VALUES (4, N'Moonstruck', N'Romantic-Comedy', 6.13, 8.3, N'Norman Jewison', N'../Images/Movies/Moon.jpeg', N' In its warmth and in its enchantment, as well as in its laughs, this is the best comedy in a long time.')
SET IDENTITY_INSERT [dbo].[movie] OFF
这是我在Visual Studio的ConnectionClass.cs
文件夹下标记为App_Code
的连接代码
using System;
using System.Collections;
using System.Linq;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
/// <summary>
/// Summary description for ConnectionClass
/// </summary>
public static class ConnectionClass
{
private static SqlConnection conn;
private static SqlCommand command;
static ConnectionClass()
{
string connectionString =
ConfigurationManager.ConnectionStrings["movieConnection"].ToString();
conn = new SqlConnection(connectionString);
command = new SqlCommand("", conn);
}
public static ArrayList GetMovieByType(string movieType)
{
ArrayList list = new ArrayList();
string query = string.Format("SELECT * FROM movie WHERE type LIKE '{0}'", movieType);
try
{
conn.Open();
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string name = reader.GetString(1);
string genre = reader.GetString(2);
double price = reader.GetDouble(3);
double rating = reader.GetDouble(4);
string director = reader.GetString(5);
string image = reader.GetString(6);
string review = reader.GetString(7);
Movie movie = new Movie(id, name, genre, price, rating, director, image, review);
list.Add(movie);
}
}
finally
{
conn.Close();
}
return list;
}
}
最后,这就是我在web.config
文件中的内容
<connectionStrings>
<clear/>
<add name="movieConnection"
connectionString="Data Source=LOCALHOST\SQLEXPRESS; Initial Catalog=MovieDB; Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
我一直收到错误:
无法打开登录
请求的数据库“MovieDB”
答案 0 :(得分:2)
我会先确保你完成基本工作。
穷人测试连接的方法......没有代码......是设置ODBC / DSN。您可以稍后删除它。转到“控制面板”,“管理工具”,“ODBC(或DSN)”。添加一个新的系统-dsn ...只是为了看看你是否可以通过。再次,首先将你的代码从等式中取出......这将是我的建议。
我会确保你有正确的ServerName,并记住有时SqlServer安装在&#34; InstanceName&#34;。
所以它会是
或
一旦你通过&#34;那个测试.........然后我们可以继续前进。