Live Scores
Sport Statistics
Movie Stars
Programming
Multimedia
Mobile
Database
Enterprise
in
.NET
Application Servers
(Entire Site)
Visual Basic 2008 - Read Text File and Insert values into MS SQL 2005 database table
I want to read a text file, then the values read will be inserted in a MS SQL database table. I wrote my code as shown below however, when I'm trying to insert the read values from the text file, an exception is thrown AS if I can only use constat values in the SQL insert statment. Please assist.
Imports System.IO
Imports System.Data.SqlClient
Public Class Import
Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open.Click
Dim conn As New SqlConnection("server = RIY00-POR01;database = PortiaTest;uid = rkhozam;pwd = 12345678")
'Dim FileReader As StreamReader
'Dim results As DialogResult
Dim dbcmd As New SqlCommand
Dim FSO, F, I
Dim INT As Integer
dbcmd.Connection = conn
conn.Open()
FSO = CreateObject("Scripting.Fi
leSystemOb
ject")
F = FSO.OpenTextFile("C:\TEXT.
TXT")
Dim N As Integer
For N = 1 To 5
I = F.ReadLine()
INT = CInt(Mid(I, 1, 5))
MsgBox(INT)
dbcmd.CommandText = "Insert Into ramzi values (INT)" 'Error is in the command"
dbcmd.ExecuteNonQuery()
Next
conn.Dispose()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Solution: Visual Basic 2008 - Read Text File and Insert values into MS SQL 2005 database table
I think the fault is in the line
dbcmd.CommandText = "Insert Into ramzi values (INT)" 'Error is in the command"
assuming you have a column called INT in the ramzi table, try
dbcmd.CommandText = "Insert Into ramzi (INT) Values (" & INT.tostring & ")"
if your column name is something different change the bracketted instance of (INT) to the actual column name, still using brackets
dbcmd.CommandText = "Insert Into ramzi (MyColumn) Values (" & INT.tostring & ")"
Note that this will still fail if other columns in the table are not allowed to be NULL.
Popular Tags
.net
Tags:
2003
2005
64-bit
ASP.NET
C#
Error in Application .NET from Scheduled Tasks [The description for Event ID ( 0 ) in Source ... ]
IIS
Internet Explorer
Microsoft
MSSQL
Mssql2005 iis problem
server
smtp relay
SmtpMail.Send
vb.net
VbNet
Visual Basic .NET
Visual Basic 2008 - MS SQL server 2005
Visual Studio
Visual Studio.NET
wap
Webservice
wincvs
Word97
wwwroot
x32
x64
x86
Browse All Tags