我有一个0.txt的文件,文件内容如下:202171000000 202171255255 宁夏 银川市
202172000000 202175255255 宁夏 
202171000000 202171255255 宁夏 吴忠市
202172000000 202175255255 宁夏 银川/隆德市怎么样用C#写!能帮写一个详细完整的代吗?谢谢!~
目的:从文本文件提取数据到数据库批定的字段实现这个谢谢!~

解决方案 »

  1.   

    to 从文本文件提取数据到数据库批定的字段实现这个
    具体是什么sample code as follows:using System.IO;StreamReader srReader = new StreamReader( yourFile );
    string strLine;
    do
    {
        strLine = srReader.ReadLine();
        // process every line using "strLine"
    }
    while( strLine != null );
      

  2.   

    Add "srReader.Close();"  in the end
      

  3.   

    private void InsertDB( string strLine )
    {
        string[] strData = strLine.Split( ' ' );// Separate every line into pieces
        string strQuery = "INSERT INTO yourTable "
              + "( yourField1,yourField2,yourField3,yourField4) "
              + " VALUES "
              + "( @yourField1,@yourField2,@yourField3,@yourField4)";
        DBCommand myCommand = new DBCommand( strQuery, yourCnn);
        myCommand.Paramters.Add( "@yourField1", strData[0] );
        myCommand.Paramters.Add( "@yourField2", strData[1] );
        myCommand.Paramters.Add( "@yourField3", strData[2] );
        myCommand.Paramters.Add( "@yourField4", strData.Length > 3? strData[3]:"" );
        myCommand.ExeNonQuery();
        myCommand.Close();
    }
      

  4.   

    to 这没有从文本读的代码????这个怎么写?前面不是给了吗
    StreamReader srReader = new StreamReader( yourFile );
    string strLine;
    do
    {
    strLine = srReader.ReadLine();
    // process every line using "strLine"
    InsertDB( strLine );
    }
    while( strLine != null );
      

  5.   

    sample code as follows:using System.IO;
    //Read data from file
    private void ReadDataFromFile( string strFileName )
    {
          StreamReader srReader = new StreamReader( yourFile );
          string strLine;
          do
          {
               strLine = srReader.ReadLine();
               // process every line using "strLine"
               InsertDB( strLine );
           }
          while( strLine != null );
          srReader.Close();
    }// Insert into DB
    private void InsertDB( string strLine )
    {
         string[] strData = strLine.Split( ' ' );// Separate every line into pieces
         string strQuery = "INSERT INTO yourTable "
             + "( yourField1,yourField2,yourField3,yourField4) "
             + " VALUES "
             + "( @yourField1,@yourField2,@yourField3,@yourField4)";
         DBCommand myCommand = new DBCommand( strQuery, yourCnn);
         myCommand.Paramters.Add( "@yourField1", strData[0] );
         myCommand.Paramters.Add( "@yourField2", strData[1] );
         myCommand.Paramters.Add( "@yourField3", strData[2] );
         myCommand.Paramters.Add( "@yourField4", strData.Length > 3? strData[3]:"" );
         myCommand.ExeNonQuery();
         myCommand.Close();
    }
      

  6.   

    数据库方面操作,参看
    http://blog.csdn.net/knight94/archive/2006/04/15/664530.aspx
      

  7.   

    static void Main(string[] args)
    {
    private void ReadDataFromFile( string strFileName )
    {
    StreamReader srReader = new StreamReader( "c:\\1.txt" );
    string strLine;
    do
    {
    strLine = srReader.ReadLine();
    // process every line using "strLine"
    InsertDB( strLine );
    }
    while( strLine != null );
    srReader.Close();
    }

    // Insert into DB
    private void InsertDB( string strLine )
    {
    string[] strData = strLine.Split( ',' );// Separate every line into pieces
    string myConnectionString = "user id=sa;password=..;initial catalog=TestData;data source=l";
    SqlConnection myConnection=new SqlConnection(myConnectionString);
    myConnection.Open(); string strQuery = "INSERT INTO yy201_dl_info "
    + "( company_name,medicine_name,medicine_type,agent_province,agent_area,agent_message,once_agent,link_man,telephone,cellphone,fax,email,agent_company_name,agent_address,send_time) values (@company_name,@medicine_name,@medicine_type,@agent_province,@agent_area,@agent_message,@once_agent,@link_man,@telephone,@cellphone,@fax,@email,@agent_company_name,@agent_address,@send_time)"; SqlCommand myCommand = new SqlCommand( strQuery);
    //myCommand.Connection = myConnection;
     
    myCommand.Parameters.Add( "@link_man", strData[0] );
    myCommand.Parameters.Add( "@agent_message", strData[1] );
    myCommand.Parameters.Add( "@telephone", strData[2] );
    myCommand.Parameters.Add("@agent_address",strData[3]);
    myCommand.Parameters.Add( "@agent_address", strData.Length > 4? strData[4]:"" );
    //System.Data.SqlClient.SqlDataReader sqldr;
    myCommand.ExecuteNonQuery();
    //myCommand.Close();
    myConnection.Close();
    }}这样了!~~~提示E:\workCodes\ConsoleCodes\DistillDlInfo_SqlData\GetURLID.cs(24): 应输入 }
      

  8.   

    Knight94(愚翁) 真的很有耐心了,如此帮忙!我也帮你顶!
      

  9.   

    还是打包给Knight94(愚翁) 吧!
    做的快点