我有一个0.txt的文件,文件内容如下:202171000000 202171255255 宁夏 银川市
202172000000 202175255255 宁夏
202171000000 202171255255 宁夏 吴忠市
202172000000 202175255255 宁夏 银川/隆德市怎么样用C#写!能帮写一个详细完整的代吗?谢谢!~
目的:从文本文件提取数据到数据库批定的字段实现这个谢谢!~
202172000000 202175255255 宁夏
202171000000 202171255255 宁夏 吴忠市
202172000000 202175255255 宁夏 银川/隆德市怎么样用C#写!能帮写一个详细完整的代吗?谢谢!~
目的:从文本文件提取数据到数据库批定的字段实现这个谢谢!~
具体是什么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 );
{
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();
}
StreamReader srReader = new StreamReader( yourFile );
string strLine;
do
{
strLine = srReader.ReadLine();
// process every line using "strLine"
InsertDB( strLine );
}
while( strLine != null );
//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();
}
http://blog.csdn.net/knight94/archive/2006/04/15/664530.aspx
{
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): 应输入 }
做的快点