Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VB.NET database

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use VB.NET database", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use VB.NET database" this article.

Sample VB.NET database code:

Imports System

Imports System.Data

Imports System.Data.SqlClient

Public Class MainClassclass MainClass

Shared Sub Main () Sub Main ()

Dim thisConnection As New SqlConnection

("server= (local)\ SQLEXPRESS;" & _

"integrated security=sspi;database=

MyDatabase ")

'Sql Query

Dim sql As String = "SELECT *

FROM Employee "

Dim insertSql As String = "INSERT

INTO Employee "& _

"(ID, FirstName, LastName) VALUES" & _

"(@ ID, @ FirstName, @ LastName)"

Try

'Create Data Adapter

Dim da As New SqlDataAdapter

Da.SelectCommand = New SqlCommand

(sql, thisConnection)

'Create and fill Dataset

Dim ds As New DataSet

Da.Fill (ds, "Employee")

'Get the Data Table

Dim dt As DataTable = ds.Tables

("Employee")

'Display Rows Before Changed

Console.WriteLine ("Before altering"

The dataset ")

For Each row As DataRow In dt.Rows

Console.WriteLine ("{0} | {1} | {2}", _

Row ("ID") .ToString () .PadRight (10), _

Row ("FirstName") .ToString () .PadRight (10), _

Row ("LastName"))

Next

'Add A Row

Dim newRow As DataRow = dt.NewRow ()

NewRow ("FirstName") = "Edna"

NewRow ("LastName") = "Everage"

NewRow ("ID") = "2"

Dt.Rows.Add (newRow)

'Display Rows After Alteration

Console.WriteLine ("=")

Console.WriteLine ("After"

Altering the dataset ")

For Each row As DataRow In dt.Rows

Console.WriteLine ("{0} | {1} | {2}", _

Row ("ID") .ToString () .PadRight (10), _

Row ("FirstName") .ToString () .PadRight (10), _

Row ("LastName"))

Next

'Insert employees

'1. Create command

Dim insertCmd As New SqlCommand

(insertSql, thisConnection)

'2. Map parameters

InsertCmd.Parameters.Add ("@ FirstName", _

SqlDbType.NVarChar, 10, "FirstName")

InsertCmd.Parameters.Add ("@ LastName", _

SqlDbType.NVarChar, 20, "LastName")

InsertCmd.Parameters.Add ("@ ID", _

SqlDbType.Int, 15, "ID")

'3. Insert employees

Da.InsertCommand = insertCmd

Da.Update (ds, "Employee")

Catch ex As SqlException

'Display error

Console.WriteLine ("Error:"

& ex.ToString ()

Finally

'Close Connection

ThisConnection.Close ()

Console.WriteLine ("Connection"

Closed ")

End Try

End Sub

End Class

The above is all the contents of the article "how to use VB.NET Database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report