我再c#中写程序用SQLServer2000,需要把一个TXT文本写入到数据库中,请问我怎么办,请高手帮忙

解决方案 »

  1.   

    //插入操作结果
    StreamReader reader = new StreamReader(sFileName); string strLine="";
    string Mobile="";
    string UserMobile=""; //int i=1;
    if(heimingdan.Length > 0)
    {
    while((strLine = reader.ReadLine()) != null)// && i<=1000)
    {
    if(Array.IndexOf(heimingdan,strLine) == -1)
    {
    Mobile=Mobile+strLine.ToString()+",";
    //i++;
    }
    }
    }
      

  2.   

    和普通的操作数据库一样啊
    可能文本比较大的话你就转换成Byte[]数组 存进去
    sqlserver2000的表字段类型是image等类型
    string也可以。
    剩下的就是sqlconnection,sqlcommand的事情了 。
      

  3.   

    1、读入文件:如果文件不大,可以一次读入,否则就一行一行读入。
    2、可以构建一个DataTable,将读入的数据插入到DataTable中。
    3、使用SqlBulkCopy类就可以批量插入(也可以使用SqlDataAdapter进行批量更新)如果不构建DataTable,可以每读入一行就往数据库插一行,但这样数据库的连接次数就会大大增加。
    另外你的做好TXT文件数据和数据库表的映射。
      

  4.   

    可以阿两种方式:
    1 把txt/text文本内容用流读取成为Text/Plain,在数据库中设置一个保存字符串的列,
    2 把txt/text本体用流读取成为application/octet-stream,在数据库中设置一个保存二进制的列然后将数据传入数据库.
      

  5.   

    不是这样简单的
    比方我的txt文本里有四行:
    01964315:402008052691
    01964315:402008052692
    01964315:402008052693
    01964315:402008052694则我写入表KQ(KQ000,KQ001)中的KQ001字段上,而KQ000则是自动递增的,其运行结果应该是:
    KQ000    KQ001
    1        01964315:402008052691
    2        01964315:402008052692
    3        01964315:402008052693
    4        01964315:402008052694
    怎么办,求救~~~
      

  6.   

    我运用:[/color]
    BULK INSERT KQ
       FROM 'c:\KQREC.txt'
       WITH 
          (
             FIELDTERMINATOR = ' ',
             ROWTERMINATOR = '\n'
          )[/code]
    语句,但老是出错:未能进行大容量插入。文件 'c:\KQREC.txt' 不存在。
      

  7.   

    BULK INSERT KQ
       FROM 'c:\KQREC.txt'
       WITH 
          (
             FIELDTERMINATOR = ' ',
             ROWTERMINATOR = '\n'
          )
      

  8.   

    以Access为例:将字段设置为OLE对象. 
    然后:  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));
                }       
      

  9.   

    我运用: 
    BULK INSERT KQ 
       FROM 'c:\KQREC.txt' 
       WITH  
          ( 
             FIELDTERMINATOR = ' ', 
             ROWTERMINATOR = '\n' 
          ) 语句,但老是出错:未能进行大容量插入。文件 'c:\KQREC.txt' 不存在。
      

  10.   

    将OleDb换成Sql
    将file字段换成image
    frm.OpenConn();
    frm.dbconn 换成你自己的连接数据库代码.这个你应该会把?
    测试通过.
      

  11.   

    StreamReader reader = new StreamReader("C:\\test.txt");
    string sql = "";
    string line = reader.ReadLine();
    while(line!=null)
    {
    sql += "insert into KQ(KQ001) values('"+ line +"');";
    line = reader.ReadLine();
    }
    reader.Close();
    //执行sql
    //注:如果文本量过大,需要分批插入
      

  12.   

    说这句有错误啊,怎么改: FileStream filestream = new FileStream(Application.StartupPath + "\\a.txt", FileMode.Open, FileAccess.Read);
    错误 1 与“System.Drawing.Icon.Icon(string, int, int)”最匹配的重载方法具有一些无效参数 D:\KQJ\KQJ\Form2.cs 24 37 KQJ
      

  13.   

    将你的文件"a.txt"放在你可执行(.exe)目录下:
    也就是bin\debug下.
      

  14.   

    是啊,我是再那里放着的啊,那个问题解决了,又有个问题了啊
    就是这一句: byte[] filebyte = (byte[])ds.Tables[0].Rows[0]["File"];说是:Unable to cast object of type 'System.String' to type 'System.Byte[]'.好像变量“filebyte”总是null,是什么原因啊,读不到值
      

  15.   

    将字段File设置为image类型看看.
      

  16.   


    我改后的代码是: private void Savefile()
            {
                Form2 frm = new Form2();
                FileStream filestream = new FileStream(Application.StartupPath + "\\KQREC.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);            SqlConnection con = new SqlConnection("Data Source=ERP;Initial Catalog=YS999;User ID=sa;Password=561951221011@");
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into KQ (KQ001) VALUES(@file)", con);
                cmd.Parameters.AddWithValue("@file", DBNull.Value);
                cmd.Parameters["@file"].Value = filebyte;
                cmd.ExecuteNonQuery();
                con.Close();
            }
            private void ReadFile()
            {
                Form2 frm = new Form2();
                SqlConnection con = new SqlConnection("Data Source=ERP;Initial Catalog=YS999;User ID=sa;Password=561951221011@");
                SqlCommand cmd = new SqlCommand("select * from KQ", con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, "x");
                DataView dv = ds.Tables["x"].DefaultView;
                byte[] filebyte = (byte[])ds.Tables["x"].Rows[0]["KQ001"];
                textBox1.AppendText(Encoding.Default.GetString(filebyte));
            }
      

  17.   


            private void ReadFile()
            {
                //Form2 frm = new Form2();//这里没有用到注释掉
                SqlConnection con = new SqlConnection("Data Source=ERP;Initial Catalog=YS999;User ID=sa;Password=561951221011@");
                con.Open();//这里少拉.
                SqlCommand cmd = new SqlCommand("select * from KQ", con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, "x");
                //DataView dv = ds.Tables["x"].DefaultView;//这里也没有用到把?
                byte[] filebyte = (byte[])ds.Tables["x"].Rows[0]["KQ001"];
                textBox1.AppendText(Encoding.Default.GetString(filebyte));
            }
           先打开你的数据库看看,KQ001里面有内容吗?KQ001字段类型是什么?
      

  18.   


    SqlDataAdapter sda = new SqlDataAdapter(cmd,con);
      

  19.   

    其实,再用到SqlDataReader的时候,“con.Open()”是可以不用要的,因为SqlDataReader自己的连接是打开这的,其他的是多余但也不能解决问题,不过还是很感谢你滴帮助