@VAer-4038 , If you want to use ODBC connection, we need to use OdbcConnection class to insert the data to MS SQL.
Here is a code example you could refer to.
The method needs to install other app:
private void button1_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.Filter = "Excel Files|*.xlsx*"; //Filter for excel file
OpenFile.Title = "Select your Test Score file";
OpenFile.FilterIndex = 2;
OpenFile.RestoreDirectory = true;
if (OpenFile.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = OpenFile.FileName;
}
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
string sheetName = xlWorksheet.Name;
xlWorkbook.Close();
xlApp.Quit();
string odbcstr = "Dsn=my sql;description=SQL SERVER;trusted_connection=Yes;app=Microsoft® Visual Studio®;wsid=Your DeviceName";
using (OdbcConnection con = new OdbcConnection(odbcstr))
{
con.Open();
string sqltext = string.Format("insert into StudentDetail SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0 Xml;HDR=YES;IMEX=1;Database={0}', 'SELECT * FROM [{1}$]')", filePath, sheetName);
OdbcCommand cmd = new OdbcCommand(sqltext, con);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Your data imported sucessfully");
}
I make a method from the link ODBC Bulk Copy C#
Updated method for not installing any apps:
First, Please install the following nuget-package:
Second, you could try the following code to insert the data from excel to database:
private void button1_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.Filter = "Excel Files|*.xlsx*"; //Filter for excel file
OpenFile.Title = "Select your Test Score file";
OpenFile.FilterIndex = 2; //Don't know what it mean
OpenFile.RestoreDirectory = true;
if (OpenFile.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = OpenFile.FileName;
}
DataTable table = new DataTable();
string odbcstr = "Dsn=my sql;description=SQL SERVER;trusted_connection=Yes;app=Microsoft® Visual Studio®;wsid=LANGUAGE-TEAM";
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
IExcelDataReader reader;
reader = ExcelDataReader.ExcelReaderFactory.CreateReader(stream);
var conf = new ExcelDataSetConfiguration
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration
{
UseHeaderRow = true
}
};
var dataSet = reader.AsDataSet(conf);
// Now you can get data from each sheet by its index or its "name"
table = dataSet.Tables[0];
//...
}
try
{
using (OdbcConnection con = new OdbcConnection(odbcstr))
{
con.Open();
foreach (DataRow item in table.Rows)
{
Console.WriteLine(item[0]);
Console.WriteLine(item[1]);
Console.WriteLine(item[2]);
string sqlinsert = string.Format("Insert into StudentDetail(ExamID,StudentID,Score) values('{0}','{1}','{2}')", item[0], item[1], item[2]);
OdbcCommand command = new OdbcCommand(sqlinsert, con);
command.ExecuteNonQuery();
}
con.Close();
}
MessageBox.Show(": Your data imported sucessfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.