ASP.NET(vb.net) & CSV and ADO.NET Insert Record - In this article we will example scripts ASP.NET how to insert new line/rows into CSV file using ADO.NET
ShotDev Focus:
- ASP.NET(vb.net) & CSV and ADO.NET Insert Record
Example
AspNetCsvAdoInsertRecord.aspx
<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Dim objConn As New OleDbConnection Dim objCmd As New OleDbCommand Dim strConnString,strSQL As String strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath("csv/") & _ ";Extended Properties='TEXT;HDR=Yes;FMT=Delimited;Format=Delimited(,)'" objConn.ConnectionString = strConnString objConn.Open() '*** Query 1 ***' strSQL = "INSERT INTO customer.csv (CustomerID,Name,Email,CountryCode,Budget,Used) " & _ "VALUES ('C005','Weerachai Nukitram','webmaster@shotdev.com'," & _ "'TH','2000000','100000')" objCmd = New OleDbCommand() With objCmd .Connection = objConn .CommandType = CommandType.Text .CommandText = strSQL End With objCmd.ExecuteNonQuery() '*** Query 1 (End) ***' '*** Query 2 ***' strSQL = "INSERT INTO customer.csv (CustomerID,Name,Email,CountryCode,Budget,Used) " & _ "VALUES ('C006','Surachai Sirisart','surachai@shotdev.com'," & _ "'TH','1000000','200000')" objCmd = New OleDbCommand() With objCmd .Connection = objConn .CommandType = CommandType.Text .CommandText = strSQL End With objCmd.ExecuteNonQuery() '*** Query 2 (End) ***' Me.lblText.Text = "Record Insert Successful." objCmd = Nothing objConn.Close() objConn = Nothing End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:Label id="lblText" runat="server"></asp:Label> </form> </body> </html>
Screenshot