0

ExcelからSQLにデータをインポートしたいのですが、この操作でユーザーが1列余分に追加できる場合、どの列が新しく追加され、同時にSQLテーブルにも同じ列名の新しい列が追加されたかを確認するにはどうすればよいですか?エクセルシート・・・

コーディングの下にあるように、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 に答える