用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败。 conn.Open();
// SqlCommand 对象, 使用conn作为数据连接
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn; cmd.CommandText = "SELECT CategoryID, CategoryName FROM Categories";
// 获取 SqlDataReader 实例
SqlDataReader reader = cmd.ExecuteReader(); // 输出结果
Console.WriteLine("{0}\t{1}", "CategoryID", "CategoryName");
Console.WriteLine();
while(reader.Read())
{
Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
} conn.Close();
}
catch(Exception e)
{
Console.WriteLine("无法连接到数据库!报告异常:");
Console.WriteLine(e.Message);
}

解决方案 »

  1.   

    string strConn = "Initial Catalog=Northwind;Data Source=(local);User ID=sa;Password=sa"; // SqlConnection 对象
    SqlConnection conn = new SqlConnection(strConn); try
    {
    // 打开数据库连接
    conn.Open();
    // SqlCommand 对象, 使用conn作为数据连接
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn; cmd.CommandText = "SELECT CategoryID, CategoryName FROM Categories";
    // 获取 SqlDataReader 实例
    SqlDataReader reader = cmd.ExecuteReader(); // 输出结果
    Console.WriteLine("{0}\t{1}", "CategoryID", "CategoryName");
    Console.WriteLine();
    while(reader.Read())
    {
    Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
    } conn.Close();
    }
    catch(Exception e)
    {
    Console.WriteLine("无法连接到数据库!报告异常:");
    Console.WriteLine(e.Message);
    }
      

  2.   

    用户 'NT AUTHORITY\NETWORK SERVICE' 
    用户到底是谁呀。我是用SA做帐号的。怎么会是权限不够呀。
      

  3.   

    好像不是数据库用户的问题,你看一下IIS的权限设置.
      

  4.   

    看SqlException,应该是数据库登陆问题,你把你的ConnectionStr写出来看看