VB.NET 2005 连接到 SQL Server Express,没有错误,但 cmd 不运行
我尝试使用 VB.NET 2005 在本地连接到 SQL Server Express。我直接从 app.config 文件中提取连接字符串。当我运行时,我没有收到任何错误,并且连接状态返回打开状态,但是命令没有被处理。
Imports System.Data
Imports System.Data.SqlClient
Public Class frmAddMovie
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim conString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True;User Instance=True;"
Dim con As New SqlConnection(conString)
Dim cmd As New SqlCommand("Insert Into tblMovies(fldTitle, fldDirector, fldRating)Values('Solar Babies', 'PG', 'Rick Flair')", con)
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
If MessageBox.Show("Movie Added") = Windows.Forms.DialogResult.OK Then
Me.Close()
End If
End Sub
End Class
I am trying to connect to SQL Server Express locally using VB.NET 2005. I pulled my connection string directly from the app.config file. When I run, I get NO errors and the connection states returns open, however the commands are not being processed.
Imports System.Data
Imports System.Data.SqlClient
Public Class frmAddMovie
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim conString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True;User Instance=True;"
Dim con As New SqlConnection(conString)
Dim cmd As New SqlCommand("Insert Into tblMovies(fldTitle, fldDirector, fldRating)Values('Solar Babies', 'PG', 'Rick Flair')", con)
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
If MessageBox.Show("Movie Added") = Windows.Forms.DialogResult.OK Then
Me.Close()
End If
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是在虚拟机中运行这个吗?
我在 Snow Leopard 上运行,并且必须不时从我的 Mac 访问 VS2010。我在虚拟机上遇到了类似的问题,但是当使用相同的代码时,它运行得很好。
我不太确定这是否是问题所在,但解决方案是在基于 Windows 的 PC 上尝试。如果它有效,那么至少您知道它与虚拟机相关。
Are you running this inside of a virtual machine?
I run on Snow Leopard and have to access VS2010 from time to time from my mac. I have had similar issues on a VM but when using the same code otherwise it runs perfectly.
I am not exactly sure if that is the issue, but the solution is to try it on a windows-based PC. If it works, then at least you know it is Virtual Machine-related.
AttachDbFilename=|DataDirectory 位于 bin/debug 文件夹中,默认情况下您的
每次运行项目时都会重新复制,因此您插入的内容将被覆盖。
解决方案很简单:
1 - 直接连接到主数据
或
2 - 在项目中选择 Movies.mdf,按 F4 将复制到输出目录设置为“不复制”
AttachDbFilename=|DataDirectory is in the bin/debug folder and by default your
is recopyed each time you run your project, so what you a have inserted will be overwritten.
The solution is simply:
1 - Connect directly to main data
or
2 - In your project select the Movies.mdf, hit F4 Set Copy to output directory to 'Do not copy'