Окна формы текстовых полей в таблицу SQL Server? - PullRequest
0 голосов
/ 01 октября 2018

Я хочу отправить данные в текстовом поле формы Windows в базу данных SQL Server.Мне удалось подключиться к базе данных, но я не знаю, как я могу отправить значения текстовых полей в назначенные им столбцы и таблицы.У меня проблемы с поиском в сети, так как большая часть синтаксиса не обновляется (в настоящее время я использую Visual Studio 2017), часть синтаксиса не работает (я не знаю, что-то упустил),Я приложу скриншот формы, а также код на форме. введите описание изображения здесь enter image description here

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

ошибка при исключении

моя база данных

1 Ответ

0 голосов
/ 01 октября 2018

хорошо, давайте вернемся к основам, мы будем использовать первые 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();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...