Similar Threads
1. Excel VBA - Import Data for manipulation from multiple text files - Excel
2. Importing tab delimited text file data into SQL Server 2000 table via VB
Greetings,
I need to create a project in Visual Basic 2003 that imports a text
file with tab delimited data (4 columns) into a SQL Server 2000 table.
Do you guys know a tutorial or a sample code to accomplish this? I
have tons of these text files to import to SQL 2000, so I'm thinking of
putting them into a folder and run them all as one batch.
Thanks in advance.
3. Test Text File before Data Import
4. Import data from a text file into SQL Server table at one go
Hello,
I could use the following statement to transfer data from Excel to SQL
Server.
With objConnMisc
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & App.Path & "\Data.xls;
Extended Properties=Excel 8.0;"
.Open
End With
strSQL = "Insert Into [ODBC;Driver={SQL Server};Server=SERVE;
Database=MARKET;uid=abc" _
& ";pwd=cba].[TABEXCEL] Select * From [Sheet1$]"
objConnMisc.Execute strSQL
I tried to do the same with a text file. I could connect to the folder
which has the text files and could also create a recordset from one of
the text files. But it is giving the following error:
Syntax error in INSERT INTO statement.
the connection object:
With objConnMisc
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & App.Path & "\;Extended
Properties=""Text;HDR=yes;FMT=Delimited=,;"""
.Open
End With
strSQL = "Insert Into
[ODBC;Driver={Text};Server=SERVE;Database=MARKET;uid=abc" _
& ";pwd=cba].[TABTEXT] Select * From text1.txt"
It is giving an error when I do the
objConnMisc.Execute strSQL
Note: There are no column names in the text files.
Any help in this regard is highly appreciated.
Thanks
5. Saving Form Data Only as Plain Text File for Importing into Database
6. export datagridview to text file and import text file to sql server table on the fly
Hi!
I am trying to export only the visible columns from a datagridview in
my windows form in VB 2008.
Should't it be no comma after the first row where the headers are?
Also should there be a comma at the last row?
When I try to insert into SQL Server in a table on the fly using this
code:
Dim strCreate As String = "IF EXISTS(SELECT name FROM sysobjects " & _
"WHERE name = N'temp_test_spam' AND type = 'U')" & _
"DROP TABLE temp_test_spam;" & _
"SELECT * INTO temp_test_spam FROM
OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt;
*.csv)};DefaultDir=C:\test_imports\;','select * from GridExport.txt')"
Dim conDatabase As SqlConnection = New SqlConnection("Network
Library=DBMSSOCN;Data Source=dr-ny-
sql003;Database='Spam_BB_Report';Integrated Security=yes;")
Dim cmdDatabase As SqlCommand = New SqlCommand(strCreate, conDatabase)
conDatabase.Open()
cmdDatabase.ExecuteNonQuery()
conDatabase.Close()
I get the following error:
System.Data.SqlClient.SqlException was unhandled
Class=16
ErrorCode=-2146232060
LineNumber=1
Message="Cannot initialize the data source object of OLE DB provider
"MSDASQL" for linked server "(null)". OLE DB provider "MSDASQL" for
linked server "(null)" returned message "[Microsoft][ODBC Text Driver]
'(unknown)' is not a valid path. Make sure that the path name is
spelled correctly and that you are connected to the server on which
the file resides."."
Number=7303
Procedure=""
Server="dr-ny-sql003"
Source=".Net SqlClient Data Provider"
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection) at
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStat
eObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior
runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj) at
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String
methodName, Boolean async) at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe) at
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at
search_engine.Form1.button1_Click(Object sender, EventArgs e) in C:
\Search Engine\Form1.vb:line 655 at
System.Windows.Forms.Control.OnClick(EventArgs e) at
System.Windows.Forms.Button.OnClick(EventArgs e) at
System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at
System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&
m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at
System.Windows.Forms.Button.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms .UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData) at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int
32 reason, ApplicationContext context) at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context) at
System.Windows.Forms.Application.Run(ApplicationContext context) at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
OnRun() at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
DoApplicationModel() at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
Run(String[] commandLine) at
search_engine.My.MyApplication.Main(String[] Args) in 17d14f5c-
a337-4978-8281-53493378c1071.vb:line 81 at
System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) at
System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationC
ontext activationContext, String[] activationCustomData) at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationC
ontext activationContext) at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugIn
Zone() at System.Threading.ThreadHelper.ThreadStart_Context(Object
state) at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state) at
System.Threading.ThreadHelper.ThreadStart()
InnerException:
Thanks in advanced for your help!!!
Tammy
I am attaching the code that I am using to achieve this:
Public Function ImportSQL(ByVal dgv As DataGridView, ByVal FN As
String) As Integer
Dim writer As StreamWriter = New StreamWriter("C:\test_imports
\GridExport.txt")
If (DataGridView1.Rows.Count > 0) Then
For Each col As DataGridViewColumn In DataGridView1.Columns
If col.Visible = True Then
If (col.Index = (DataGridView1.Columns.Count - 1)) Then
writer.WriteLine(col.HeaderText)
Else
writer.Write(String.Concat(col.HeaderText, ","))
End If
End If
Next
For Each row As DataGridViewRow In DataGridView1.Rows
'If Not omitIndices.Contains(row.Index) Then
For Each cell As DataGridViewCell In row.Cells
If DataGridView1.Columns.Item(cell.OwningColumn.Index).Visible = True
Then
If (cell.OwningColumn.Index = (DataGridView1.Columns.Count - 1)) Then
If (Not (cell.Value) Is Nothing) Then
writer.WriteLine(cell.Value.ToString)
Else
writer.WriteLine("")
End If
ElseIf (Not (cell.Value) Is Nothing) Then
writer.Write(String.Concat(cell.Value.ToString, ","))
Else
writer.Write(String.Concat("", ","))
End If
End If
Next
'End If
Next
End If
writer.Close()
End Function
7. delimiting data before the data file is being import
8. Access VBA - Create a text file and add data to text file
Any Access (97) gurus out there know how to (through VBA, module):
1) Create an external text file
2) Insert text (data) into the text file just created?
Thanks!