private DataSet ReadExcel(string strFileName, string sheetName)
{ string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strFileName + ";Extended Properties = Excel 8.0";
OleDbConnection oleConnection = new OleDbConnection(strConnection);
try
{
oleConnection.Open();
DataSet dsRead = new DataSet();
OleDbDataAdapter oleAdper = new OleDbDataAdapter(" SELECT * FROM [" + sheetName + "$]", oleConnection);
oleAdper.Fill(dsRead);
return dsRead;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
finally
{
oleConnection.Close();
}
}private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
try
{
if (diaOpenFile.ShowDialog() == DialogResult.OK)
{
if (diaOpenFile.OpenFile() != null)
{
LinkDataBase link = new LinkDataBase();
//ds = ReadExcel(@diaOpenFile.FileName, "Sheet1");
ds=link.UpdateDataBase(ReadExcel(@diaOpenFile.FileName, "Sheet1"), "teachingmaterial_info");
this.Visible = true;
}
else
{
this.Visible = true;
}
}
}
catch (Exception Error)
{
MessageBox.Show("导入失败!" + Error.Message, "提示");
this.Visible = true;
}
}public class LinkDataBase
{
private string strSQL;
private string connectionString = "server=(local);database=TeachingMaterialManage;integrated security=sspi";
private SqlConnection myConnection;
private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public LinkDataBase() { } //数据库更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet,string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(strSQL,myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet,tableName);
return changedDataSet;
}
{ string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strFileName + ";Extended Properties = Excel 8.0";
OleDbConnection oleConnection = new OleDbConnection(strConnection);
try
{
oleConnection.Open();
DataSet dsRead = new DataSet();
OleDbDataAdapter oleAdper = new OleDbDataAdapter(" SELECT * FROM [" + sheetName + "$]", oleConnection);
oleAdper.Fill(dsRead);
return dsRead;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
finally
{
oleConnection.Close();
}
}private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
try
{
if (diaOpenFile.ShowDialog() == DialogResult.OK)
{
if (diaOpenFile.OpenFile() != null)
{
LinkDataBase link = new LinkDataBase();
//ds = ReadExcel(@diaOpenFile.FileName, "Sheet1");
ds=link.UpdateDataBase(ReadExcel(@diaOpenFile.FileName, "Sheet1"), "teachingmaterial_info");
this.Visible = true;
}
else
{
this.Visible = true;
}
}
}
catch (Exception Error)
{
MessageBox.Show("导入失败!" + Error.Message, "提示");
this.Visible = true;
}
}public class LinkDataBase
{
private string strSQL;
private string connectionString = "server=(local);database=TeachingMaterialManage;integrated security=sspi";
private SqlConnection myConnection;
private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public LinkDataBase() { } //数据库更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet,string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(strSQL,myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet,tableName);
return changedDataSet;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货