Add all Excel sheet to Sql database

  • Thread starter Thread starter alianware jack
  • Start date Start date
A

alianware jack

Guest
private void importExcelDatabase()
{
string Text = Interaction.InputBox("Please Keyin Database name", "Create Database", FullName);
if (Text != string.Empty)
{
SqlConnection EFSCD = new SqlConnection("Server=localhost;Integrated Security=SSPI;database=master;");
string com = "CREATE DATABASE " + Text + " ON PRIMARY(Name=" + Text + ",FILENAME='D:\\" + Text + ".mdf',SIZE=2MB,MAXSIZE=25MB,FILEGROWTH=5%)" +
"LOG ON(NAME=" + Text + "_Log,FILENAME='D:\\" + Text + ".ldf',SIZE=1MB,MAXSIZE=25MB,FILEGROWTH=5%)";
using (SqlCommand SC = new SqlCommand(com, EFSCD))
{
SC.ExecuteNonQuery();
}

using (SqlBulkCopy SBC = new SqlBulkCopy(EFSCD))
{
//what should i do on this way to import all excel sheet to sqldatabase
}
}
else
{
return;
}
}

//many component is base on devexpress component

//openfildialog to hold excel file
private void LoadExcelToGridview()
{
comboBoxEdit2.Properties.Items.Clear();
OpenFileDialog OFD = new OpenFileDialog();
OFD.Filter = "Excel files (*.xlsx,*.xlsm*.xltm,*.xltm)|*.xlsx;*.xlsm;*.xltm;*.xltm" +
"|Word files(*.docx*.docm,*.dotx,*.dotm,*.docd)|*.docx;*.docm;*.dotx;*.dotm;*.docd" +
"|All files (*.*)|*.*";
OFD.FilterIndex = 1;
if (OFD.ShowDialog() == DialogResult.OK)
{
//create a string to hold the file name
string DialogFileName = OFD.FileName;
FullName = OFD.SafeFileName;
//to check is the file name isnullorempty or isnullorwhitespace
if (!string.IsNullOrEmpty(DialogFileName))
{
//declare a varable to hold the file open
using (var OpenFile = File.Open(OFD.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (IExcelDataReader FileReader = ExcelReaderFactory.CreateReader(OpenFile))
{
do//loop
{ while (FileReader.Read()) { } } while (FileReader.NextResult());
ExcelDataSetConfiguration ExcelDSC = new ExcelDataSetConfiguration()
{
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
};
//to hold the data set
DataSet OpenResult = FileReader.AsDataSet(ExcelDSC);
TableC = OpenResult.Tables;
}
}
foreach (DataTable DT in TableC)
comboBoxEdit2.Properties.Items.Add(DT.TableName);
textEdit1.Text = OFD.FileName;
}
else
{
MessageBox.Show(DialogFileName);
return;
}
}
else
{
return;
}
}

hi, i want to add all excel sheet from openfiledialog to sql database, but how to add it by using sqlbulkcopy or other code or have any code need to change? TQ

Continue reading...
 
Back
Top