把txt文件里用逗号隔开的3列*n行数据(n很大)导入数据库进行更新,我用的代码如下:
Dim sr As System.IO.StreamReader
                sr = New StreamReader("c:\1.txt")                Do While True
                    linstr = sr.ReadLine
                    If Trim(linstr) = "$" Then
                        Exit Do
                    End If
                 temp1 = Split(linstr, ",")
                         strName=temp1(0)                        
                        strPrice =temp1(1)
                       strType= temp1(2)
                     Dim sql As String
                      
                        sql = "update table1 set price='" & strPrice & " ', type='" & strType & " ' where name= '" & strName & "'"
                        Dim myCommand1 As SqlCommand = New SqlCommand(sql, myConn)
                        myCommand1.Connection.Open()
                        myCommand1.ExecuteNonQuery()
                        myCommand1.Connection.Close()
                 Loop
                sr.Close()这样相当于处理每行数据都需要进行一次update操作,在行数n非常大的时候,速度非常慢,不知道有什么比较快的读取并更新数据库的方法.
谢谢!

解决方案 »

  1.   

    主要是这个循环update的过程耗时太长,在很多人一起操作的时候容易在这里发生死锁(主程序中还有很多对table1的查询和update操作)
      

  2.   

    先 insert到临时表 再表与表之间update
      

  3.   

    根据你的代码,我建议的方法:
    使用bcp in导入到一张临时表中table1_bak;然后根据临时表table1_bak的字段strName先删除原表中的记录,然后再将临时表table1_bak中的数据插入到table1
    delete from table1 where exists (select 1 from table1_bak where table1_bak.strMame=table1.strMame)
    然后
    insert into table1
    select * from table1_bak通过你的代码,在入库前好像还有个简单的数据清洗的过程。 建议用文本替换工具或者十六进制编辑器将源文件中的字符$ 去掉。
      

  4.   

    把txt轉換成excel格式,再導入到數據庫中
      

  5.   

    不,懂;帮,顶;学,习;赚,分。
    虽然这么回贴可能会被删除。
    谁他妈的删除了就是个王八蛋。
    强烈抗议CSDN这种即当婊子又想立贞洁牌坊的恶劣行为!!!
    有本事,就封杀我!!!理由在这里:
    他妈的一帮管理员有神经病啊???
    http://topic.csdn.net/u/20110422/10/1a381057-7a7b-41d9-8969-ad1f5b77f24a.html?38665
    最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
    如果有多表,表之间如何关联?
    如何更有效地在SQL Server论坛上提问
    http://topic.csdn.net/u/20100716/19/6f132f16-20e4-418c-8dee-b99d5f86d320.html?75910
    [code=SQL]