要在导入该字段后在gridview中插入一个字段 [英] want to insert one field in gridview after i import that field

查看:74
本文介绍了要在导入该字段后在gridview中插入一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button2_Click(object sender, EventArgs e)
        {
            string srvFileLocation = "";
            string fileName = "", fileExtension = "";
            string connectionString = "";
            if (FileUpload1.HasFile)
            {
                fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                string fileLocation = Server.MapPath("~/App_Data/" + fileName);
                FileUpload1.SaveAs(fileLocation);
                Label1.Text = "File Uploaded: " + FileUpload1.FileName;
                //Check whether file extension is xls or xslx

                if (fileExtension == ".xls")
                {
                    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                }
                else if (fileExtension == ".xlsx")
                {
                    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                }
                //  String cn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=C:\\Users\\Charu\\Documents;" +"Extended Properties=Excel 8.0;";
                //Create OleDB Connection and OleDb Command

                OleDbConnection con = new OleDbConnection(connectionString);
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = con;
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                DataTable dtExcelRecords = new DataTable();
                con.Open();
                DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
                dAdapter.SelectCommand = cmd;
                dAdapter.Fill(dtExcelRecords);
                con.Close();
                GridView1.DataSource = dtExcelRecords;
                GridView1.DataBind();

            }
            else
            {
                Label1.Text = "No File Uploaded.";
            }

            if (FileUpload1.HasFile)
            {
                fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                srvFileLocation = Server.MapPath("~/App_Data/" + fileName);
                FileUpload1.SaveAs(srvFileLocation);

            }
            string excelConnectionString = "";
            if (fileExtension == ".xls")
            {
                excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srvFileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (fileExtension == ".xlsx")
            {
                excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srvFileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            //string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srvFileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

            // Create Connection to Excel 
            using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
            {
                OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);
                connection.Open();
                // Create DbDataReader to Data Worksheet
                using (DbDataReader dr = command.ExecuteReader())
                {
                    // SQL Server Connection String
                    string sqlConnectionString = "Data Source=SNSWAMY\\SQLEXPRESS;Initial Catalog=payroll;Persist Security Info=True;User ID=avoc;Password=avoc123";

                    // Bulk Copy to SQL Server
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                    {
                        //bulkCopy.ColumnMappings.Add("process_datetime", "process_datetime");
                        bulkCopy.DestinationTableName = "leave_tran";
                        bulkCopy.WriteToServer(dr);
                    }
                }
            }
            }


此代码正确导入文件..但是在此我想插入一个字段表单文本框并与导入文件一起显示在gridview中...
如何结合该文本框字段和导入字段....
是否有人帮助我...


this code imports files correctly.. But in this i want to insert one field form text box and displays in gridview along with import file...
how to combine that textbox field and import fields....
Whether any one help me...

推荐答案

",连接); connection.Open(); // 创建DbDataReader到数据工作表 使用(DbDataReader dr = command.ExecuteReader()) { // SQL Server连接字符串 字符串 sqlConnectionString = " // 批量复制到SQL Server 使用(SqlBulkCopy bulkCopy = SqlBulkCopy(sqlConnectionString)) { // bulkCopy.ColumnMappings.Add("process_datetime","process_datetime"); bulkCopy.DestinationTableName = " ; bulkCopy.WriteToServer(dr); } } } }
", connection); connection.Open(); // Create DbDataReader to Data Worksheet using (DbDataReader dr = command.ExecuteReader()) { // SQL Server Connection String string sqlConnectionString = "Data Source=SNSWAMY\\SQLEXPRESS;Initial Catalog=payroll;Persist Security Info=True;User ID=avoc;Password=avoc123"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { //bulkCopy.ColumnMappings.Add("process_datetime", "process_datetime"); bulkCopy.DestinationTableName = "leave_tran"; bulkCopy.WriteToServer(dr); } } } }


此代码正确导入文件..但是在此我想插入一个字段表单文本框并与导入文件一起显示在gridview中...
如何结合该文本框字段和导入字段....
是否有人帮助我...


this code imports files correctly.. But in this i want to insert one field form text box and displays in gridview along with import file...
how to combine that textbox field and import fields....
Whether any one help me...


这篇关于要在导入该字段后在gridview中插入一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆