如何使用VB.NET Windows窗体在SQL SERVER 2005中插入数据? [英] How to insert data in SQL SERVER 2005 using VB.NET windows form?

查看:80
本文介绍了如何使用VB.NET Windows窗体在SQL SERVER 2005中插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我被这个问题困扰.我已经写了一些代码,想知道代码中缺少什么,因为我无法使用此代码在SQL Server的表中插入数据.

在此先感谢您的帮助.

整个代码如下:-



导入System.Data
导入System.Data.SqlClient
导入System.Windows.Forms.Form
公共类Form1
'私有子txtname_TextChanged(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理txtname.TextChanged
公共子类txtname_KeyPress(ByVal发送者作为对象,ByVal e作为System.Windows .Forms.KeyPressEventArgs)处理txtname.KeyPress
昏暗的名称作为字符串仅限文本'')
End If

End Sub

''Private Sub txtage_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理txtage.TextChanged
Public Sub txtage_KeyPress(ByVal s ender作为对象,ByVal作为System.Windows.Forms.KeyPressEventArgs)处理txtage.KeyPress "1"表示或e.KeyChar> " 5")然后
MessageBox.Show(输入年龄介于0-5岁之间")
End If
Age = txtage.Text
End Sub

'Private Sub txtbatch_TextChanged(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理txtbatch.TextChanged
公共Sub txtbatch_KeyPress(ByVal sender作为Object,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理txtbatch.KeyPress
将Dim Batch作为Char
Batch = txtbatch.Text
End Sub







作为新的SqlConnection("server = localhost; database = Database1")
Private Sub Button1_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理Button1.Click


AddHistory("Name => txtname.Text","Age => txtage.Text",批处理=> txtbatch.Text")

End Sub

Public Sub AddHistory(ByVal名称为字符串,ByVal年龄为Char,ByVal批次为Char)

Tr将y
MySqlConnection.Open()年龄,批次)值(Malha,'4','2009')''

MyInsertCommand.ExecuteNonQuery()

MySqlConnection.Close()
结束尝试

结束子

结束类

Hi,

I am stuck with this problem. I've written some code and want to know what is missing in the code, as I'm not able to insert data in tables of SQL server using this code.

Thanks a ton in advance for your help.

The whole code is given below:-



Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms.Form
Public Class Form1
    'Private Sub txtname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtname.TextChanged
    Public Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtname.KeyPress
        Dim Name As String
        Name = txtname.Text
        If (e.KeyChar = "0") Then
            MessageBox.Show("Enter Name as text only")
        End If

    End Sub

    'Private Sub txtage_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtage.TextChanged
    Public Sub txtage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtage.KeyPress
        Dim Age As Char
        If (e.KeyChar < "1" Or e.KeyChar > "5") Then
            MessageBox.Show("Enter Age between 0-5 yrs")
        End If
        Age = txtage.Text
    End Sub

    'Private Sub txtbatch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbatch.TextChanged
    Public Sub txtbatch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbatch.KeyPress
        Dim Batch As Char
        Batch = txtbatch.Text
    End Sub




    Dim MySqlConnection As New SqlConnection("server=localhost;database=Database1")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        AddHistory("Name => txtname.Text", "Age => txtage.Text", "Batch => txtbatch.Text")

    End Sub

    Public Sub AddHistory(ByVal Name As String, ByVal Age As Char, ByVal Batch As Char)

        Try
            MySqlConnection.Open()

            Dim MyInsertCommand As New SqlCommand

            MyInsertCommand.Connection = MySqlConnection
            MyInsertCommand.CommandText = "Insert into Database1.pubs.test1 (Name, Age, Batch) values (Malha,'4','2009')"

            MyInsertCommand.ExecuteNonQuery()

            MySqlConnection.Close()
        Catch ex As Exception
        End Try

    End Sub

   
End Class

推荐答案

感谢您的帖子!我建议用以下其中一种方式发布您的问题

<身体>
TechNet论坛»SQL Server»SQL Server工具常规
位于此处: http://forums.microsoft.com/TechNet/ShowForum.aspx?ForumID=84&SiteID=17 .

Thank you for your post!  I would suggest posting your question in one of the 

TechNet Forums » SQL Server » SQL Server Tools General
located here:  http://forums.microsoft.com/TechNet/ShowForum.aspx?ForumID=84&SiteID=17.


这篇关于如何使用VB.NET Windows窗体在SQL SERVER 2005中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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