In the ASP.NET, insert the data into the data

zhaozj2021-02-16  51

During our usual development, you often insert data into the database, sometimes you might want to make many similar operations, such as inserting several data simultaneously with the same table in the database, i.e., inserting data.

Insert data into the database, you can insert a number of data once insert a trade database, improve the performance efficiency of the program, or reduce our workload.

Batch inserted data, you can use two methods.

First, use the transaction (Transaction)

'Define the function of executing bulk inserted data, arguments for the INSERT SQL statement

Sub Extractions (Byval Sqlstrlist As String ())

DIM conn as string = "provider = microsoft.jet.Oledb.4.0; data source = Tax.mdb"

DIM Trans as oledbtransaction = Nothing

Try

IF conn.state = connectionState.closed then

Conn.open ()

END IF

DIM CMD AS Oledbcommand = New OLEDBCommand ()

cmd.connection = conn

cmd.comMandType = CommandType.Text

Trans = conn.begintransaction ()

cmd.transaction = trans

DIM I as integer

For i = 0 to sqlstrlist.getupperbound (0)

CMD.CommandText = SQLSTRLIST (i) 'Value in parameter (array)

cmd.executenonquery ()

NEXT

TRANS.COMMIT ()

Catch ex as oledbexception

TRANS. ROLLBACK ()

FANLLY

CONN.CLOSE ()

END TRY

End Sub

Second, use dataset

Public Sub INSERT ()

'Establish a DataTable data source

DIM DT AS DATATABLE = New DataTable ()

DIM DR AS DATAROW

Dt.columns.add (New Datacolumn ("Name"))

DIM J AS INTEGER

For j = 0 to 10

DR = DT.NEWROW ()

DR (0) = "name" j.toString

Dt.Rows.Add (DR)

NEXT

DIM conn as string = "provider = microsoft.jet.Oledb.4.0; data source = Tax.mdb"

Conn.open ()

DIM myadapter as oledbdataadapter = new oleDataAdapter ()

.

DIM cmd as oledbcommand = new oledbcommand ("INSERT INTO TABLE (NAME) VALUES (@Name)", Conn

Cmd.Parameters.Item ("@ name"). SourceColumns = dt.columns ("name"). ColumnSname

Myadapter.Update (DT)

CONN.CLOSE ()

End Sub

Using the above two methods, you can complete the data batch insertion data.

转载请注明原文地址:https://www.9cbs.com/read-23487.html

New Post(0)