使用VB.Net代码附加SQL Server Express数据库 [英] Attaching SQL Server Express DB using VB.Net Code

查看:79
本文介绍了使用VB.Net代码附加SQL Server Express数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用以下VB代码将数据库附加到服务器.

Hi,
I am using the following VB code to attach my DBs to server.

Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Database='" & TextBox1.Text & "';Connect Timeout=30")
       cnnConnection.Open()



但它给出了一个错误:

无法打开物理文件"D:\.Net程序\ SQLDBAttachExperiments \ SQLDBAttachExperiments \ bin \ Debug \ datafiles \ dbSQLtest.mdf".操作系统错误5:"5(未找到错误)".无法将文件``D:\.Net程序\ SQLDBAttachExperiments \ SQLDBAttachExperiments \ bin \ Debug \ datafiles \ dbSQLtest.mdf''附加为数据库``dbSQLtest''.

如何解决?
Thanks



But it gives an error:

Unable to open the physical file "D:\.Net Programs\SQLDBAttachExperiments\SQLDBAttachExperiments\bin\Debug\datafiles\dbSQLtest.mdf". Operating system error 5: "5(error not found)". Cannot attach the file ''D:\.Net Programs\SQLDBAttachExperiments\SQLDBAttachExperiments\bin\Debug\datafiles\dbSQLtest.mdf'' as database ''dbSQLtest''.

How to fix it?
Thanks

推荐答案

这意味着您的文件路径不正确.
检查您的文件系统,该mdf文件是否存在应用程序期望找到的位置.即D:\.Net程序\ SQLDBAttachExperiments \ SQLDBAttachExperiments \ bin \ Debug \ datafiles \ dbSQLtest.mdf.
如果在该目录中找不到该文件,则需要在开始调试之前将其放置在该目录中.
It means that your file path is incorrect.
Check your file system, does the mdf file exist where the application expects to find it. i.e. D:\.Net Programs\SQLDBAttachExperiments\SQLDBAttachExperiments\bin\Debug\datafiles\dbSQLtest.mdf.
If the file is not found in that directory, then you need to put it there before you begin debugging.


这篇关于使用VB.Net代码附加SQL Server Express数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆