公司要写个软件,给我安排排了一部分
就是在VS2005下C#实现文本TXT文件导入SQL,很少接触编程,大家帮帮忙,谢谢了,越详细越好,最好带注释

解决方案 »

  1.   


           private void SaveFile()
            {
                Form1 frm = new Form1();
                FileStream filestream = new FileStream(Application.StartupPath + "\\a.txt", FileMode.Open, FileAccess.Read);
                BinaryReader filerd = new BinaryReader(filestream, Encoding.Default);
                byte[] filebyte = new byte[filestream.Length];
                filerd.Read(filebyte, 0, (int)filestream.Length);            frm.OpenConn();            OleDbCommand comm = new OleDbCommand("insert into file  (id,file) Values(@fid,@file) ", frm.dbconn);
                comm.Parameters.AddWithValue("@fid", "0001");
                comm.Parameters.AddWithValue("@file", DBNull.Value);
                comm.Parameters["@file"].Value = filebyte;
                comm.ExecuteNonQuery();        }
            private void ReadFile()
            {
                Form1 frm = new Form1();
                frm.OpenConn();
                OleDbCommand comm = new OleDbCommand("select * from file", frm.dbconn);
                OleDbDataAdapter da = new OleDbDataAdapter(comm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                byte[] filebyte = (byte[])ds.Tables[0].Rows[0]["File"];
                //  System.Text.Encoding mycode = new System.Text.Encoding();
                this.richTextBox1.AppendText(Encoding.Default.GetString(filebyte));
            }
      

  2.   

    不需要c#,sql本来就支持文本导入用bcp命令就可以了
    EXEC  master..xp_cmdshell  'bcp  "dbname..tablename"  in  c:\DT.txt  -c  -Sservername  -Usa  -Ppassword'  
      

  3.   

    列子是Access,转Sql,您应该没问题把?
    将File字段设置为color=#FF0000]image,[[/color],应为image可以存储超过8000字节;
    Form1 frm = new Form1();
    frm.OpenConn();
    frm.dbconn
    是因为我在Form1中已经定义了连接数据库,换成您自己的连接;