需要SQL连接帮助(VB.net) [英] Need Help with SQL Connection (VB.net)

查看:83
本文介绍了需要SQL连接帮助(VB.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

希望有人可以帮助我,希望能够轻松解决!

Hoping someone can help me, hopefully an easy fix!

我是VB / Visual的新手工作室,我基本上是在教自己如何 - 我有一个我已经构建的Windows窗体,非常简单,只是一个名字和姓氏字段,我正试图找出如何得到它,无论输入什么值进入这两个字段,
进入我的SQL数据库 - 我有一个名为"Users"的表。我已经将SQL数据库导入到项目中,所以我确信所有工作都正常 - 但我似乎无法让它执行!我一直得到以下错误。

I'm very new to VB/Visual Studio, I'm basically teaching myself how - I've got a Windows Form I have built, very simple, just a First Name and Last Name field and I'm trying to work out how to get it to wrtie whatever value is entered into those two fields, into my SQL Database - where i have a table called "Users". I have imported the SQL database into the project so i'm sure thats all working ok - but i just cant seem to get it to execute! i keep getting the below error.

我已经坐下来阅读/观看了几小时的YouTube视频和指南,了解如何让它发挥作用,但我无法理解。有人可以提供一些帮助吗?我提前感谢!

I've sat and read/watched hours of youtube videos and guides on how to get this to work, but i just cant get past this. Please could someone provide some help? My thanks in advance!

System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'

这就发生在这一行:

command.ExecuteNonQuery()

以下是完整代码:

Imports System.Data
Imports System.Data.SqlClient

Public Class SQTest

    Dim cn As New SqlConnection("Data Source=DESKTOP-94NTTM7\SQLEXPRESS01;Initial Catalog=HATS;Integrated Security=True")


    Private Sub SQTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim command As New SqlCommand("insert into Users(First Name,Last Name) value('" & FName.Text & "', '" & Lname.Text & "')")

        cn.Open()

        command.ExecuteNonQuery()

        cn.Close()




    End Sub


End Class




推荐答案

你好,

以下分离出创建与基本异常类耦合的连接字符串。我假设你有一个主要的自动递增主键(如果你不是真的需要添加一个)。

The following separates out creating the connection string which couples with a base exception class. I assume you have a primary auto-incrementing primary key (if you don't you really need to add one).

这里可以下载完整的源代码

运行插入操作的表单代码,我使用了硬编码值,但你可以让它们来自说两个TextBox控件。

Form code to run the insert operations, I used hard coded values but you can have them come from say two TextBox controls.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ops As New DataOperations
        Dim newIdentifier = ops.InsertHat("Karen", "Payne")
        If ops.IsSuccessFul Then
            MessageBox.Show(


"新记录标识符是{newIdentifier}")
Else
MessageBox.Show(
"New record identifier is {newIdentifier}") Else MessageBox.Show(


"无法插入记录{环境.NewLine} {ops.LastExceptionMessage}")
End if
End Sub
En d Class
"Failed to insert record{Environment.NewLine}{ops.LastExceptionMessage}") End If End Sub End Class

将以下类添加到项目中。

Add the following classes to your project.

Imports System.Data.SqlClient
Public Class BaseExceptionsHandler
    Protected mHasException As Boolean
    Public ReadOnly Property HasException() As Boolean
        Get
            Return mHasException
        End Get
    End Property
    Protected mLastException As Exception
    Protected ReadOnly Property LastException() As Exception
        Get
            Return mLastException
        End Get
    End Property
    Public ReadOnly Property HasSqlException() As Boolean
        Get
            If LastException IsNot Nothing Then
                Return TypeOf LastException Is SqlException
            Else
                Return False
            End If
        End Get
    End Property
    Public ReadOnly Property LastExceptionMessage() As String
        Get
            Return LastException.Message
        End Get
    End Property
    Public ReadOnly Property IsSuccessFul() As Boolean
        Get
            Return Not HasException
        End Get
    End Property
End Class

	Imports ExceptionsLibrary

Public MustInherit Class BaseSqlServerConnection
	    Inherits BaseExceptionsHandler
    ''' <summary>
    ''' This points to your database server
    ''' </summary>
    Protected DatabaseServer As String = "DESKTOP-94NTTM7\SQLEXPRESS01"
    ''' <summary>
    ''' Name of database containing required tables
    ''' </summary>
    Protected DefaultCatalog As String = "HATS"
    Public ReadOnly Property ConnectionString() As String
        Get
            Return


这篇关于需要SQL连接帮助(VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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