我想做一個excel文件的映射,但在一定的方式列Excel文件的映射。 如實施例我有一個EXEL文件,其中(Excel文件的)第2列必須被引用的有SQL數據庫的第1列。如何做到使用C#
我已經有了SQL搜索做我只是想知道我可以在組合框中把名字列在Excel文件。
這是我使用搜索Excel文件中的代碼:
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
{
DataSet result;
if (ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
reader.IsFirstRowAsColumnNames = true;
result = reader.AsDataSet();
comboBox1.Items.Clear();
foreach (DataTable dt in result.Tables) comboBox1.Items.Add(dt.TableName);
reader.Close();
string ConecçãoDB = ConfigurationManager.ConnectionStrings["ConecçaoDB"].ConnectionString;
string Table = ConfigurationManager.AppSettings["table"];
string ssqltable = Table;
string ssqlconnectionstring = ConecçãoDB;
filename = ofd.FileName;
MessageBox.Show(Convert.ToString(filename));
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
var conexao = new System.Data.OleDb.OleDbConnection(connectionString);
var sql = "SELECT * FROM [" + comboBox1.SelectedText + "$]";
string sclearsql = "delete from " + ssqltable;
}
}
具體什麼你有問題 - 實際數據綁定到組合框本身?你到底採取了哪些步驟來嘗試這些,或者你卡住了? – LightCC
@LightCC我卡上添加數據,組合框 –