文本文件内容导入数据库
文本文件里的内容导入到数据库中,数据库可能是MSsql,或oracle,导入数据库不一定是在本机,也可能是局域网,写个小程序来实现这个功能。能给个解决方案吗,这样便于解决问题。 --------------------编程问答-------------------- 用imports system.io
打开文体文件,
读取一行,
加工这一行数据,生成SQL语句,存入数据库
--------------------编程问答-------------------- 可以直接通过查询分析器就可以了,写程序也用这样的语句
insert [ODBC;Driver=SQL Server;Server=SQL服务器名;Uid=用户名;Pwd=密码;database=数据库名].接受数据导入的表名(字段列表)
SELECT 导入的字段列表
FROM [Excel 5.0;HDR=NO;DATABASE=c:\test.xls].[sheet1$]
--------------------编程问答--------------------
--------------------编程问答--------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sr As TextReader = New StreamReader("d:\myfile.txt")
Dim line As String
Do
line = sr.ReadLine
Dim s() As String = Split(line.Substring(InStr(line, " "), line.Length - InStr(line, " ")), "then")
Dim s0 As String = line.Substring(0, InStr(line, " ")).Trim()
MsgBox(s0 & "-" & s(0).Trim() & "-" & s(1).Trim())
'这里输入SQL存储数据库
Loop Until line Is Nothing
sr.Close()
End Sub
--------------------编程问答-------------------- 读取txt
Public Class Form1
Dim constr As String = "User ID=sa;pwd=a;Initial Catalog=zbb;Data Source=(local)" '定义一个变量,连接字符串
Dim sqlstr As String = "" '定义一个变量,SQL语句
Dim mycon As New SqlConnection(constr) '定义一个变量,为一个连接对象,并给定连接字符串。
Dim mycom As New SqlCommand(sqlstr, mycon) '定义一个变量,为命令对象
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As TextReader = New StreamReader("c:\7.txt")
Dim line As String
Do
line = sr.ReadLine
mycom.CommandText = "insert into test values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.SelectedItem + "')";
mycom.Connection.ConnectionString = constr '为这个连接字符串重新赋值
mycon.Open() '重新打开这个联接
mycom.ExecuteNonQuery() '用不返回结果的查询语句来插入一条记录
Loop Until line Is Nothing
sr.Close()
mycon.Close()
End Sub
End Class
例如TextFile.txt内容如下:
id name
1 01
2 xing
C# code
string cnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + ";Extended Properties=\"text;HDR=Yes;FMT=Delimited;\";";
另外,在当前目录下加schema.ini文件,内容如下:
[TextFile.txt]
ColNameHeader=True
Format=TabDelimited
Col1=id Char
Col2=name Char
注意:TextFile.txt与字段名字与你的txt文件要对应。毕竟,直接读取还是有些限制的, --------------------编程问答-------------------- ??vb.net啊,那个啥改改应该就可以了哈
补充:.NET技术 , VB.NET