SQL异常未被用户代码处理连接数据库到ASP.网的网站
本文关键字:数据库 ASP 网站 连接 处理 异常 用户 代码 SQL | 更新日期: 2023-09-27 18:16:22
我试图将数据库连接到我的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 community 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");
}
}
请帮帮我,谢谢。
看起来您没有给用户正确的权限。
如果你使用用户名/通过PELim'PeiErn作为登录凭证在SQL管理工作室2014,你可以访问数据库airplane_reservation?
看另一个问题,它似乎在讨论一个类似的问题。
从你上面的ConnectionString我注意到
- 你的数据来源:我认为它应该是plim - pc或使用"。"(周期)默认服务器
- 使用sql server身份验证并添加password="your server pwd"