将数据插入数据库表 [英] Inserting data to database table

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

问题描述

我是进入vb.net的PHP开发人员.

挑战在于如何将vb.net表单中的数据插入数据库中?

假设我有一个包含3个字段(TextBox1, TextBox2, TextBox3)和一个提交按钮(Button1)的表单.
添加数据源后,我将为Button1 的Click事件编写什么样的代码,以便将3个文本框中的数据插入到名为"Register"的Access表中,该表具有3个列Firstname,Surname,Age在名为注册"的数据库中?

谢谢.

I am a PHP developer getting into vb.net.

The challenge is how do I insert data from a vb.net form into a database?

Say I have a form with 3 fields(TextBox1, TextBox2, TextBox3) and a submit button(Button1).
After adding a data source, what kind of code will I write for the Click event of Button1 so as to insert data from the 3 textboxes into an Access table named say, ''Register'' with 3 columns Firstname, Surname, Age in a database named ''Registration''?

Thanks.

推荐答案

您可能应该在此处上研究CP文章. [^ ]您可能可以找到基于以下内容的文章您将使用的确切数据库.有很多方法可以完成任务.您可以通过SQL INSERT语句(使用命令对象将其作为非查询语句运行)来执行此操作,也可以将数据放入数据表对象中并使用数据适配器对象执行填充.
You should probably research the CP Articles here.[^] You can probably find an article based on the exact database you will use. There are a lot of ways you can accomplish your task. You can do it via an SQL INSERT statement running it as non-query statement with a command object, or you can put the data into a datatable object and use data adapter object to perform a fill. Google[^] is also a good for examples.


这将是以下一行:
This would be something in the line of:
Dim OracleCon As New OracleConnection
Dim cmd As OracleCommand
Dim reader As OracleDataReader
Dim cmdAdd As String

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

cmdAdd = "INSERT INTO REGISTER " & _
         "(Firstname,Surname,Age) " & _
         "VALUES ('" & TextBox1.Text & _
         "','" & TextBox2.Text & _
         "','" & TextBox3.Text & "')"

cmd = New OracleCommand(cmdAdd)
cmd.Connection = OracleCon
cmd.CommandType = CommandType.Text

Try
reader = cmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub


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

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