当前位置:编程学习 > C#/ASP.NET >>

如何将txt文本文档导入到数据中

 protected void Button1_Click1(object sender, EventArgs e)
    {


       // string fPath = this.fName.PostedFile.FileName; //获得要导入的文本文件 
        string fPath = this.fName.PostedFile.FileName;
        if (fPath == "")
        {
            Response.Write("<script language=javascript>alert('文件未选择!')</script>");
        }
        else
        {
            string extName = fPath.Substring(fPath.LastIndexOf(".") + 1);//获得文件的扩展名            
            SqlConnection con = new SqlConnection("Data source=.;Initial catalog=deptment;uid=sa;pwd=123456");//数据库连接对象
            con.Open();
            try
            {
                SqlCommand com = new SqlCommand("BULK INSERT deptment.dbo.tb_Class FROM '" + fPath + "' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR= '/n')", con);
                //其中的FIELDTERMINATOR=','指明字段间所使用的分隔符为逗号
                //其中ROWTERMINATOR= '/n'指明记录间所使用的分隔符为回车
                com.ExecuteNonQuery();
                Response.Write("<script language=javascript>alert('数据导入成功!')</script>");
            }
            catch (SqlException SQLexc)
            {
                Response.Write("导入数据时出错:" + SQLexc.ToString());
            }
            con.Close();
        }
    }
}
我这么写老是错误!!求指教 --------------------编程问答-------------------- 你文本的内容要有序排列,这样才可以分开存入,不然,就一块存入了!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,