Reputation: 473
Here's what my database looks like on my server under Microsoft SQL Server Management Studio
I was hoping if I put my database code and my connection code, you guys could tell me what's wrong? I'm not sure what the problem is, I'm pretty sure the code is ok.
This is the database I created for movies - 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
And here is the connection code I have labeled ConnectionClass.cs
under my App_Code
folder in Visual Studio
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;
}
}
Finally, this is what I have in my web.config
file
<connectionStrings>
<clear/>
<add name="movieConnection"
connectionString="Data Source=LOCALHOST\SQLEXPRESS; Initial Catalog=MovieDB; Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I keep getting the error:
Cannot open database "MovieDB" requested by the login
Upvotes: 2
Views: 6268
Reputation: 27852
I would make sure you do the very basics first.
The poor man's way to test a connection..without code...is to setup a ODBC/DSN. You can delete it later. Go to Control Panel , Admin Tools , ODBC (or DSN) . Add a new system-dsn...just to see if you can get through. Again, take your code out of the equation first...would be my advice.
I would make sure you have the ServerName correct, and remember that sometimes SqlServer is installed under an "InstanceName".
So it would be either
or
Once you "pass" that test.........then we can move on.
Upvotes: 2