SQLCommand不插入表格

发布于 2025-01-19 18:49:37 字数 1827 浏览 0 评论 0原文

这段代码看起来应该可以工作,但实际上却不起作用。我已经尝试过各种方法(使用和不使用 SqlDataAdapter),

  1. 目前我将 mdf 和 xsd 选为“如果较新则复制”,并且也尝试过从不复制,并且始终复制。
  2. 插入时不会引发错误,但是如果我尝试第二次插入,则会出现错误

违反主键约束“PK_owners”。无法在对象“dbo.owners”中插入重复的键。重复的键值为 (Test)。 该声明已终止。

返回,就好像数据实际上在表中一样。

  1. 我确实有另一张桌子&确实有效的表单(不是插入) 这是一个登录表,其中包含我在创建表时手动插入的数据(SQL),该表单上的 SQL 选择语句工作得很好(也涉及文本框)
Private Sub btnInsertOwner_Click(sender As Object, e As EventArgs) Handles btnInsertOwner.Click

        Dim db As String = Path.Combine(Directory.GetCurrentDirectory(), "VBvetProject.mdf")

        Dim conn As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\VBvetProject.mdf;Integrated Security=True;")

        Using cmd As New SqlCommand("INSERT INTO owners VALUES (@fullname,@email,@phone,@dob)", conn)

            cmd.Parameters.AddWithValue("@fullname", txtOwnerFullName.Text)
            cmd.Parameters.AddWithValue("@email", txtOwnerEmail.Text)
            cmd.Parameters.AddWithValue("@phone", txtOwnerPhone.Text)
            cmd.Parameters.AddWithValue("@dob", txtOwnerDoB.Text)
            Dim adapter As New SqlDataAdapter(cmd)

            conn.Open()
            cmd.ExecuteNonQuery()
            Dim dt As New DataTable()
            adapter.Update(dt)
            conn.Close()
        End Using

        MessageBox.Show("New owner created!")

    End Sub

在尝试插入新的数据后从 win 表单中获取记录,我使用 SQL select 检查它,但没有任何返回(我还通过 Visual Studio 扩展了表的数据,这仍然为空。)

这是表和值的片段:

在此处输入图像描述

老实说,我在这里不知所措,可以使用任何帮助来弄清楚我做错了什么,这真是一个令人毛骨悚然的事情。

无论我尝试了多少种方法来更改代码、连接,它似乎都不起作用。

This code seems like it should work, but simply doesn't. I've tried it various ways (with & without SqlDataAdapter)

  1. I have both the mdf and xsd selected as "Copy if newer" currently, and have also tried never, and copy always.
  2. No errors are thrown when inserted, however if I try to insert a second time, the error

Violation of PRIMARY KEY constraint 'PK_owners'. Cannot insert duplicate key in object 'dbo.owners'. The duplicate key value is (Test).
The statement has been terminated.

is returned, as if the data is actually in the table.

  1. I do have another table & form that does work (not inserts) It is a login table with data I manually inserted (SQL) at the time of table creation, the SQL select statements on that form work perfectly fine (also involve text boxes)
Private Sub btnInsertOwner_Click(sender As Object, e As EventArgs) Handles btnInsertOwner.Click

        Dim db As String = Path.Combine(Directory.GetCurrentDirectory(), "VBvetProject.mdf")

        Dim conn As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\VBvetProject.mdf;Integrated Security=True;")

        Using cmd As New SqlCommand("INSERT INTO owners VALUES (@fullname,@email,@phone,@dob)", conn)

            cmd.Parameters.AddWithValue("@fullname", txtOwnerFullName.Text)
            cmd.Parameters.AddWithValue("@email", txtOwnerEmail.Text)
            cmd.Parameters.AddWithValue("@phone", txtOwnerPhone.Text)
            cmd.Parameters.AddWithValue("@dob", txtOwnerDoB.Text)
            Dim adapter As New SqlDataAdapter(cmd)

            conn.Open()
            cmd.ExecuteNonQuery()
            Dim dt As New DataTable()
            adapter.Update(dt)
            conn.Close()
        End Using

        MessageBox.Show("New owner created!")

    End Sub

After attempting to insert a new record from the win form, I check it with a SQL select, and nothing returns (I also expand the table's data through Visual Studio, this is also still null.)

Here is a snip of the table and values:

enter image description here

I'm honestly at a loss here and could use any help figuring out what I'm doing wrong, this has been such a hair puller.

No matter how many ways I've tried changing the code, the connections, it just doesn't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文