如何将客户端的文本文件内容导入到服务器数据库中,在线等...
问题如题,文本文件的路径是可以改变的private void Button1_Click(object sender, System.EventArgs e)
{
string sLine= " ";
string strCmdText= " ";
string[] splitData=new string[5];
//此處寫入你要寫入文本的完整路徑
string txtFileName= "D:\\TEST.txt ";
StreamReader TxtReader = new StreamReader(txtFileName);
//此處寫入自己數據庫的連接字符串
SqlConnection conn =new SqlConnection( "server=(服务器端ip);user=sa;password=;database=YourDateabase ");
conn.Open();
try
{
do
{
//Read the txt file
sLine = TxtReader.ReadLine();
if (sLine == null)
break;
splitData=sLine.Split( '\t ');
//Insert Datas to the table
//此處可按照實際表的格式調整SQL語句
strCmdText= "Insert Test Values( ' "+splitData[0].Trim()+ " ', ' "+splitData[1].Trim()+ " ', ' "+splitData[2].Trim()+ " ', "+splitData[3].Trim()+ ", "+splitData[4].Trim()+ ") ";
SqlCommand SqlCmd=new SqlCommand(strCmdText,conn);
SqlCmd.ExecuteNonQuery();
}
while(true);
conn.Close();
TxtReader.Close();
}
catch
{
//Response.Write( " <script language= 'javascript '> alert( 'Upload file format error ') </script> ");
}
finally
{
if (conn.State.ToString()== "Open ")
conn.Close();
if (TxtReader != null)
TxtReader.Close();
}
}
该程序在本地倒是可以执行,但用FileUpload1改变路径(即改变txtFileName= "D:\\TEST.txt ";)放在服务器出,就出现了找不到路径的现象,
不知道我说清楚了没有,大家帮帮忙呀
------解决方案--------------------最简单的方法, 你将客户端的东西传到服务器上, 然后读到数据库中,一切OK