【发布时间】:2016-11-05 11:35:58
【问题描述】:
我试图将数据库连接到我的 ASP.NET 网站并遇到此错误:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Cannot open database "airplane_reservation" requested by the login. The login failed.
Login failed for user 'PELim\PeiErn'.
我在 SQL Management Studio 2014 中创建数据库。我正在使用 Visual Studio Coommunity 2015,这是我的 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class CompleteOrder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random rnd = new Random();
int ordernum = rnd.Next(3, 10000);
string Name = Request.QueryString["name"];
string IC = Request.QueryString["ic"];
string Contact = Request.QueryString["contact"];
string Date = Request.QueryString["date"];
string Seats = (string) (Session["seats"]);
Label7.Text = Convert.ToString(ordernum);
Label4.Text = Name;
Label8.Text = IC;
Label10.Text = Contact;
Label12.Text = Date;
Label14.Text = Seats;
SqlConnection con = new SqlConnection(@"Data Source=PELim;Initial Catalog=airplane_reservation;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd =new SqlCommand("INSERT INTO Airplane_Reservation(Order_Number,Name,Identification_Number, Contact_Number, Departure_Date, Seats)VALUES('" + ordernum + "','" + Name + "','" + IC + "','" + Contact + "', '" + Date + "', '" + Seats + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("Main.aspx");
}
}
请帮帮我,谢谢。
【问题讨论】:
-
尝试在 db 中添加用户并更新您的连接字符串。顺便说一句,在 web.config 中使用连接字符串,而不是直接在代码中。
-
你的意思是我为这个数据库创建了一个用户来登录和更改连接字符串? @SanishJoseph
-
是的。我正是这个意思。授予具有适当权限的角色。
-
好的,我试试,谢谢!