帮助向数据库添加数据 [英] Help with adding data to the database

查看:89
本文介绍了帮助向数据库添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的编码员,

我设法通过谷歌搜索有关vb和sql的方法创建了以下代码,但是在完成所有这些编码之后,程序正在执行,但数据未插入所需的表中.同时,如果我直接通过sql服务器插入数据,则它正在处理..请帮助我解决此问题.

代码是:

Dear Coders,

I managed to create the following code from googling about vb and sql but after all this coding the program is executing but data is not inserting to the desired table. In the meantime if i insert data straight through the sql server it is working on.. Please help me fix this.

Code is:

Imports System.Data.SqlClient

Public Class Form1
    Dim con As New SqlConnection


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim myconstring As String = "Data Source=eta-it;Initial Catalog=Procurement;Integrated Security=True"
            con.ConnectionString = myconstring

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        'TODO: This line of code loads data into the 'ProcurementDataSet.Material_Master' table. You can move, or remove it, as needed.
        Me.Material_MasterTableAdapter.Fill(Me.ProcurementDataSet.Material_Master)

        'TODO: This line of code loads data into the 'ProcurementDataSet.Project' table. You can move, or remove it, as needed.
        Me.ProjectTableAdapter.Fill(Me.ProcurementDataSet.Project)

        'TODO: This line of code loads data into the 'ProcurementDataSet.E1' table. You can move, or remove it, as needed.
        Me.E1TableAdapter.Fill(Me.ProcurementDataSet.E1)

    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

    End Sub

    Private Sub ComboBox2_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Material_IDTextBox.Text = ComboBox2.SelectedValue
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Project_IDTextBox.Text = ComboBox1.SelectedValue

        Dim dt As New DataTable
        Dim da As New SqlDataAdapter("select max(sl_no) from e1 where project_id = '" + Project_IDTextBox.Text + "'", con)

        da.Fill(dt)

        DataGridView1.DataSource = dt.DefaultView

        TextBox1.Text = DataGridView1.CurrentCell.Value.ToString()

        If Sl_NoTextBox.Text = "" Then
            MsgBox("iam null")
            Sl_NoTextBox.Text = "1"
        Else
            'MsgBox("iam not null" + Sl_NoTextBox.Text.ToString)
            Sl_NoTextBox.Text = Sl_NoTextBox.Text + 1

        End If
    End Sub

    Private Sub Project_IDTextBox_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Project_IDTextBox.TextChanged

    End Sub

    Private Sub Sl_NoTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sl_NoTextBox.TextChanged
        Dim dt As New DataTable
        Dim da As New SqlDataAdapter("select max(sl_no) from e1 where project.project_id and e1.project_id = '" + Project_IDTextBox.Text + "'", con)
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

推荐答案

您需要返回从那里获得的信息,然后再获取其余信息.坦白说,这只是一部分代码(我从您的上一个问题中可以识别出其中的一部分),它们看起来并不是为了协同工作而设计的.

例如:
You need to go back to where you got that from, and get the rest of it. Because frankly, that is just partial bits of code (one of which I recognise from a previous question of yours) that don''t look designed to work together.

For example:
Private Sub Sl_NoTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sl_NoTextBox.TextChanged
    Dim dt As New DataTable
    Dim da As New SqlDataAdapter("select max(sl_no) from e1 where project.project_id and e1.project_id = '" + Project_IDTextBox.Text + "'", con)
End Sub

将不执行任何操作. DataAdapter和DataTable仍未连接,并且它们都超出范围,因此在方法结束时未使用. (忽略了您仍在使用字符串连接来构建SQL查询,正如我上次告诉您的那样,这是一种危险的做法)

停止抓住随机的代码块并寄希望于此:这不是一个好的开发策略.考虑您的问题并一步一步地工作会好得多.

Will do nothing. The DataAdapter and the DataTable are still not connected, and they both go out of scope and are thus unused at the end of the method. (Ignoring that you still are using string concatenation to build SQL queries, which as I told you last time is a dangerous practice)

Stop grabbing random lumps of code and hoping: that is not a good development strategy. Thinking about your problem and working in small steps is a much, much better one.


我通过在表格中添加表格活页夹保存按钮来解决了这个问题.

代码是

I solved this myself by adding the table binder save button to the form.

the code is

Private Sub E1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles E1BindingNavigatorSaveItem.Click

        MsgBox(Sl_NoTextBox.Text)
        Me.Validate()
        Me.E1BindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ProcurementDataSet)

    End Sub




任何方式都感谢您提供的原始信息.




Any ways thanks for the information originalgriff.


这篇关于帮助向数据库添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆