2013-05-08 43 views
1

尝试将我的项目文件夹复制到另一个硬盘驱动器时出现问题。我在Visual Studio中的项目在h:\中定义,虽然运行exe文件项目没有任何问题,但将文件夹项目复制到另一个硬盘驱动器时,我有问题将应用程序连接到SQL Server。无法将项目连接到SQL Server数据库

看到这些截图:

例如,在登录表单,并在登录按钮我的代码是:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim conn As New SqlConnection("Server=.\SQLExpress;AttachDbFilename=" + Environment.CurrentDirectory + "\Database\Automation.mdf;Database=Automation;Trusted_Connection=Yes;") 
     s2 = "Select count(*) from Login where UserName = '" & TextBox1.Text & "' and Password = '" & TextBox2.Text & "'" 
     Dim com As New SqlCommand(s2, conn) 
     Dim res As Object 
     conn.Open() 
     res = com.ExecuteScalar() 
     conn.Close() 
     If res = 1 Then 
      MsgBox("Welcome Dear " + TextBox1.Text, MsgBoxStyle.OkOnly, "Login Successful") 
      us = TextBox1.Text 
      If us = "admin" Then 
       Main.Label4.Text = "ADMINISTRATOR" 
      Else 
       Main.Label4.Text = "USER" 
       us = TextBox1.Text 
       Main.Button3.Enabled = False 
       Main.Button5.Enabled = False 
       Main.Button9.Enabled = False 
      End If 
      Main.Show() 
      Me.Hide() 
     Else 
      MsgBox("Invalid User OR Password", MsgBoxStyle.Critical, "Attention Please") 
      TextBox1.Text = "" 
      TextBox2.Text = "" 
      Exit Sub 
     End If 
    End Sub 
+0

非常感谢你 – 2013-05-08 16:33:16

+0

看看你的定义'SqlConnection'。它包括'相对'路径。你有没有把这些物体放在其他硬盘上? – 2013-05-08 16:35:30

+0

这看起来像VBA。你应该用这个标签更新它。 – Kermit 2013-05-08 16:40:16

回答

0

你需要指定数据库是,你不能使用Env铁器。当前目录。 CurrentDirectory使用当前工作目录的路径。所以要么将数据库移动到同一个硬盘上,要么改变数据库的路径。

http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx

+0

谢谢......但要解决这个问题应该改变我的代码? – 2013-05-09 03:54:20

+0

请更改您的代码,以便它指向正确的目录。或者移动数据库。 – Frank 2013-05-10 17:30:48