0

我想将数据从excel导入sql,在此操作中如果用户可以额外添加一列,我如何检查新添加的列,同时在sql表中还添加了具有相同列名的新列,例如excel表格...

我可以完成阅读我的编码下方的 excel 列计数

 protected void btn_upload_Click(object sender, EventArgs e)
    {
        //Add Path to Local system dynamically
        string path = string.Empty;

        path = Server.MapPath("~//files//") + Fup_Excel.PostedFile.FileName;

        Fup_Excel.SaveAs(path);
        //-----------------Execl Connection & Count Part-------------------------------------
        //create Oledb connection for Excel Fetch
        OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0");


        OleDbCommand olcmd = new OleDbCommand("select * from [sheet1$]", oconn);//create command for select excel file

        OleDbDataAdapter adapter = new OleDbDataAdapter();

        adapter.SelectCommand = olcmd;//adap the command

        DataSet ds = new DataSet();//collection of data

        adapter.Fill(ds);//data's are filled to dataset

        string count = ds.Tables[0].Columns.Count.ToString();//taking the Excel sheet column count



    }

在我完成从 sql 表中计算之后,代码也是

 SqlConnection sconn = new SqlConnection(@"Data Source=C07-PC\SQLEXPRESS;Initial Catalog=excel;User ID=sa;Password=**********");
        string asdf = null;
        SqlCommand scmd = new SqlCommand();
        scmd.CommandText = "select count(*) from information_schema.columns where table_name='table_1'";
        scmd.Connection = sconn;
        sconn.Open();
        SqlDataReader sr = null;
        sr = scmd.ExecuteReader();
        while (sr.Read())
        {

              asdf =sr[0].ToString();

        }

从这里开始,我不知道如何检查在 excel 中新添加的列?然后我想在 sql 表中插入列的同名..

4

1 回答 1

0

从 excel 中检索所有列后,您需要检查 SQL 数据库中的每一列是否存在该列。如果不存在,则添加到 sql 数据库。

于 2012-10-30T12:32:29.433 回答