首页 > 解决方案 > Windows 窗体文本框值到 SQL Server 表?

问题描述

我想将 Windows 窗体上的文本框上的数据发送到 SQL Server 数据库。我能够连接数据库,但我不知道如何将文本框值发送到它们指定的列和表。我很难通过网络搜索,因为大多数语法都没有更新(我目前正在使用 Visual Studio 2017)一些语法不起作用(我不知道我是否遗漏了什么) . 我将附上表格的屏幕截图以及表格上的代码。 在此处输入图像描述 在此处输入图像描述

Imports MessagingToolkit.QRCode.Codec
Imports System.Data.SqlClient
Public Class AddBook
    Dim QR_Generator As New QRCodeEncoder


    Public Property QR_Generator1 As QRCodeEncoder
        Get
            Return QR_Generator
        End Get
        Set(value As QRCodeEncoder)
            QR_Generator = value
        End Set
    End Property


    Private Sub b_cancel_Click(sender As Object, e As EventArgs) Handles b_cancel.Click
        Me.Close()
        MainMenu.Show()
    End Sub

    Private Sub tb_quantity_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tb_quantity.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
          AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub tb_isbn_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tb_isbn.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
                  AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub tb_ddn_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tb_ddn.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
          AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub tb_year_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tb_year.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
                         AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub tb_isbn_TextChanged(sender As Object, e As EventArgs) Handles tb_isbn.TextChanged
        Try
            qrbox.Image = QR_Generator1.Encode(tb_isbn.Text)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Dial_save_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles dial_save.FileOk
        Try
            Dim img As New Bitmap(qrbox.Image)
            img.Save(dial_save.FileName, Imaging.ImageFormat.Png)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub b_add_Click(sender As Object, e As EventArgs) Handles b_add.Click
        dial_save.FileName = tb_isbn.Text
        dial_save.ShowDialog()
        'Dim Bookname As String = tb_name.Text.Trim()
        'Dim publisher As String = tb_publisher.Text.Trim()
        'Dim isbn As String = tb_isbn.Text.Trim()
        '' at this point we have the text box data in variables

        ''Next you need a string to hold the sql database query 
        Dim sql As String = "insert into [Book Totality](isbn,book,author,publisher,dds,class,yr,edition,quantity) VALUES ('" + tb_isbn.Text + "','" + tb_name.Text + "','" + tb_author.Text + "','" + tb_publisher.Text + "','" + tb_ddn.Text + "','" + cb_type.SelectedText + "','" + tb_year.Text + "','" + cb_ver.SelectedText + "','" + tb_quantity.Text + "',)"
        Dim sc As SqlConnection
        sc = New SqlConnection()
        Dim com As SqlCommand
        com = New SqlCommand()
        sc.ConnectionString = "Data Source=MARK-HP;Initial Catalog=QR Library System DB;Integrated Security=True"
        sc.Open()
        com.Connection = sc
        com.CommandText = sql
        com.ExecuteNonQuery()
        sc.Close()
    End Sub

End Class

异常错误

我的数据库

标签: sql-servervb.netwinforms

解决方案


好的,让我们回到基础,我们将使用前 3 个框,我假设我知道文本框的名称

Private Sub b_add_Click(sender As Object, e As EventArgs) Handles b_add.Click
Dim Bookname as String = txtAuthor.Text .Trim()
Dim publisher as String = txtpublisher.Text .Trim()
Dim isbn as String = txtisbn.Text .Trim()
 '' at this point we have the text box data in variables

''Next you need a string to hold the sql database query 

Dim sql as String = "INSERT INTO Stock (bookcolumnname,publishercolumnname,isbncolumnname) VALUES ('" + tb_isbn.Text + "','" + tb_isbn.Text + "','" + tb_isbn.Text + "')

除此之外

SqlConnection sc = New SqlConnection();
SqlCommand com = New SqlCommand();
sc.ConnectionString = ("Data Source=MARK-HP;Initial Catalog="QR Library System DB";Integrated Security=True");
sc.Open();
com.Connection = sc; 
com.CommandText = sql
com.ExecuteNonQuery();
sc.Close();

推荐阅读