登录失败了数据库

时间:2012-01-14 04:59:00

标签: c# vb.net sql-server-2008

我使用以下代码备份我的数据库......

Dim con As SqlConnection
Dim cmd As SqlCommand
con = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=LIC;Trusted_Connection = yes")
cmd = New SqlCommand("BACKUP DATABASE LIC to disk='C:\Backup\ICLS.bak'", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()

但是我收到了这个错误

  

Cannot open database "LIC" requested by the login. The login failed. Login failed for user 'mono-PC\mono'.

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

试试这个。它仍然会失败,但会打印一个数字(例外状态)。下面的表格列出了可能的状态及其可能发生的原因(来自http://blogs.msdn.com/b/sql_protocols/archive/2006/02/21/536201.aspxhttp://msdn.microsoft.com/en-us/library/ms366351.aspx)。请参阅这些链接以获得解决方案。

try
  Dim con As SqlConnection
  Dim cmd As SqlCommand
  con = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=LIC;Trusted_Connection = yes")
  cmd = New SqlCommand("BACKUP DATABASE LIC to disk='C:\Backup\ICLS.bak'", con)
  con.Open()
  cmd.ExecuteNonQuery()
  con.Close()
catch e as SqlException
    Console.WriteLine("State: " & e.State)
end try

可能的国家:

ERROR STATE ERROR DESCRIPTION
2 and 5     Invalid userid
6           Attempt to use a Windows login name with SQL Authentication
7           Login is disabled, and the password is incorrect
8           Password mismatch
9           Invalid password
11 and 12   Valid login but server access failure
13          SQL Server service paused
18          Change password required