用于将ms访问db表导入mssql 2008 R2的C#代码 [英] C# Code for Importing ms access db table to mssql 2008 R2

查看:63
本文介绍了用于将ms访问db表导入mssql 2008 R2的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好Guyz正在尝试使用c#代码以编程方式将ms访问表导入mssql,我需要代码,有人可以提供代码或链接。我尝试了一些功能不正常。

Hi Guyz am trying for c# code for importing ms access table to mssql programmatically, i need code ,could someone provide me the code or link . i tried some which was not functioning properly.

推荐答案

private void button2_Click(object sender,EventArgs e)

{

//创建sql连接字符串

string conString = @Data Source = MAXCLIENT01; Initial Catalog = dbs_Test1;;

SqlConnection sqlCon = new SqlConnection(conString) ;

sqlCon.Open();



SqlDataAdapter da = new SqlDataAdapter(select * from vegetable,sqlCon);

System.Data.DataTable dtMainSQLData = new System.Data.DataTable();

da.Fill(dtMainSQLData);

DataColumnCollection dcCollection = dtMainSQLData.Columns ;

//将数据导出到EXCEL表

/ *下面的行获取错误* /

Microsoft.Office.Interop.Excel。 ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel。 ApplicationClass();





ExcelApp.Application.Workbooks.Add(Type.Missing);

/ / ExcelApp.Cells.CopyFromRecordset(objRS);

for(int i = 1;我< dtMainSQLData.Rows.Count + 1; i ++)

{

for(int j = 1; j< dtMainSQLData.Columns.Count + 1; j ++)

{

if(i == 1)

ExcelApp.Cells [i,j] = dcCollection [j - 1] .ToString();

else

ExcelApp.Cells [i,j] = dtMainSQLData.Rows [i - 1] [j - 1] .ToString();

}

}

ExcelApp.ActiveWorkbook.SaveCopyAs(C:\\Users \\User \\Documents\\Vegetable.xlsx);

ExcelApp.ActiveWorkbook.Saved = true;

ExcelApp.Quit();

MessageBox.Show(数据成功导入Excel文件);



}



private void button1_Click(object sender,EventArgs e)

{

//为MS-Offi创建数据表ce 2007或2003

System.Data.DataTable dtExcel = new System.Data.DataTable();

dtExcel.TableName =MyExcelData;

string SourceConstr = @Provider = Microsoft.ACE.OLEDB.12.0; Data Source ='C:\\Users\\User\\Documents\\Vegetable.xlsx';扩展属性= 'Excel 8.0; HDR =是; IMEX = 1';

OleDbConnection con = new OleDbConnection(SourceConstr);

string query =Select * from [Vegetable
private void button2_Click(object sender, EventArgs e)
{
// Create sql connection string
string conString = @"Data Source = MAXCLIENT01; Initial Catalog = dbs_Test1;";
SqlConnection sqlCon = new SqlConnection(conString);
sqlCon.Open();

SqlDataAdapter da = new SqlDataAdapter("select * from vegetable", sqlCon);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
/* the foloowing line am getting Error*/
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();


ExcelApp.Application.Workbooks.Add(Type.Missing);
// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 1; i++)
{
for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
{
if (i == 1)
ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
else
ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\User\\Documents\\Vegetable.xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Data Exported Successfully into Excel File");

}

private void button1_Click(object sender, EventArgs e)
{
// Create Data Table for MS-Office 2007 or 2003
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel.TableName = "MyExcelData";
string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\Users\\User\\Documents\\Vegetable.xlsx';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Vegetable


;

OleDbDataAdapter data = new OleDbDataAdapter(query,con);

data.Fill(dtExcel);



MessageBox.Show(数据成功导入DataTable);

}

收到错误

类型或命名空间office不存在。帮帮我
";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);

MessageBox.Show("Data Imported Successfully into DataTable");
}
am getting Error as
type or namespace office does not exists.help me out


怎么样 Google [ ^ ]?有很多例子。
What about Google[^]? There are tons of examples.


这篇关于用于将ms访问db表导入mssql 2008 R2的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆