VB.NET 2005 连接到 SQL Server Express,没有错误,但 cmd 不运行

发布于 2024-12-10 09:48:22 字数 992 浏览 0 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-12-17 09:48:22

你是在虚拟机中运行这个吗?

我在 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.

蓝海似她心 2024-12-17 09:48:22

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文