各位大虾有谁知道怎样把文件导入到数据库里面
比如:文件里面有很多文件每一个文件都要对应到数据库表里面,相当于树结构
如果我没解释清楚的请加我QQ654982114

解决方案 »

  1.   

    String tempStr=inputFile.PostedFile.FileName; String fName=tempStr.Substring(tempStr.LastIndexOf('\\')+1);
    int length =tempStr.LastIndexOf('\\')+1;

    //另存到本地
    inputFile.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["CsvPath"])+fName); //以行为单位
    ArrayList Al=new ArrayList();
    ArrayList Sl=new ArrayList();

    //读取副本得到记录数
    String line="";
    StreamReader sr=new StreamReader(System.Web.HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["CsvPath"])+fName,System.Text.Encoding.Default);
    while(((line = sr.ReadLine()) != null)) 
    {
    if(line.StartsWith("型号")!=true)
    {
    String newline=line.Replace('\'',' ');
    Al.Add(newline);
    }
    }
    //存入文件表并返回文件ID

    SqlDataLayer dl=new SqlDataLayer();
    int fileid=dl.AddDataFile(fName,Al.Count,UserID,tempStr); //建立插入语句
    try
    {

    for(int i=0;i<Al.Count;i++)
    {
    //以列为单位
    string frontStr="insert into stock (";
    string blackStr=") values ('"; String[] C=Al[i].ToString().Split(',');

    ArrayList newal=new ArrayList();
    if(C[0].Trim()==""||C[0]=="型号")
    Al.RemoveAt(0); //处理双引号行
    Boolean flag=false;
    StringBuilder sb=new StringBuilder();
    Boolean addflag=false; for(int j=0;j<C.Length;j++)
    {

    if(C[j].StartsWith("\"")||C[j].EndsWith("\""))
    {
    if(flag==true) flag=false;
    else flag=true;
    addflag=false;
    } if(addflag==true) newal.Add(C[j]);
    else
    {
    if(flag==true)
    {
    sb.Append(C[j].TrimStart('\"'));
    sb.Append(",");
    addflag=false;
    }
    else
    {
    sb.Append(C[j].TrimEnd('\"'));
    newal.Add(sb.ToString());
    sb.Remove(0,sb.Length);
    addflag=true;
    }
    }
    }   String[] S=new String[14];
    for(int k=0;k<newal.Count;k++)
    {
    S[k]=newal[k].ToString();
    }
    if(newal.Count<14)
    {


    //int itemcount=0;
    if(one)
    {
    frontStr+="partno,";
    //blackStr+=""+S[itemcount++]+"',";
    blackStr+=""+S[0]+"',";
    }
    if(two)
    {
    frontStr+="qty,";
    try
    {
    blackStr+=""+Convert.ToInt32(S[1].Replace(",",""))+","; }
    catch
    {
    blackStr+="0,"; }
    }
    if (three)
    {
    frontStr+="brand,";
    blackStr+="'"+S[2]+"',";
    }
    if(four)
    {
    frontStr+="[year],";
    blackStr+="'"+S[3]+"',";
    }
    if(five)
    {
    frontStr+="inprice,";
    blackStr+="'"+S[4]+"',";
    }
    if(six)
    {
    frontStr+="price,";
    blackStr+="'"+S[5]+"',";
    }
    if(sever)
    {
    frontStr+="zip,";
    blackStr+="'"+S[6]+"',";
    }
    if(eight)
    {
    frontStr+="pack,";
    blackStr+="'"+S[7]+"',";
    }
    if(night)
    {
    frontStr+="descript,";
    blackStr+="'"+S[8]+"',";
    }
    if(ten)
    {
    frontStr+="uploads,";
    try
    {
    blackStr+=""+Convert.ToInt32(S[9].Replace(",",""))+",";
    }
    catch
    {
    blackStr+="0,"; }
    }
    if(eleven)
    {
    frontStr+="layout,";
    blackStr+="'"+S[10]+"',";
    }
    if(twelve)
    {
    frontStr+="res,";
    blackStr+="'"+S[11]+"',";
    }
    if(thirteen)
    {

    frontStr+="companyno,";
    blackStr+="'"+S[12]+"',";
    }
    if(fourteen)
    {
    frontStr+="indate,";
    try
    {
    if(S[13]!=null)
    {
    blackStr+="'"+Convert.ToDateTime(S[13])+"',";
    }
    else
    {
    blackStr+="'"+DateTime.Now+"',";
    } }
    catch
    {
    blackStr+="'"+DateTime.Now+"',";
    }
    }
    }
      

  2.   

    flyzph(Darky) ( ) 信誉:100 
    不好意思  上面的没看懂
    有asp.net写的没的
    整清楚点  谢谢拉
      

  3.   

    这些是asp.net(C#)的代码啊。这是一个CSV文件导入数据库的方法。要不你加我QQ87131435再聊吧