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テーブルにも列を挿入したい..