【问题标题】:SQL Exception was unhandled by user code Connect database to ASP.NET website用户代码未处理 SQL 异常 将数据库连接到 ASP.NET 网站
【发布时间】: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
  • 是的。我正是这个意思。授予具有适当权限的角色。
  • 好的,我试试,谢谢!

标签: c# sql asp.net database


【解决方案1】:

您似乎没有向用户授予正确的权限。

如果您在 SQL Management Studio 2014 中使用用户名/密码 PELim\PeiErn 作为登录凭据,您可以访问数据库飞机预订吗?

看到这个other question似乎在讨论一个类似的问题。

【讨论】:

  • 我已经在 PELim\PeiErn 用户中检查了航空公司预订数据库的用户映射,它显示了 db_owner 和 public。我也在使用 Windows 身份验证
【解决方案2】:

从你上面的 ConnectionString 我注意到了

  1. 您的数据源:我认为应该是 PELim-PC 或使用“.” (句点)默认服务器
  2. 使用sql server认证并添加password="your server pwd"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多