2013-01-23 211 views
0

有人可以帮助我,请以修复错误。 这是我的代码:类型或命名空间名称“SqlBulkCopy的”找不到

using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.Data.OleDb; 
using System.Data; 
using Microsoft.ApplicationBlocks.Data; 
using System.Configuration; 

OleDbConnection ExcelCon = new OleDbConnection(); 
ExcelCon.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Users\\pc\\Documents\\ExcellTest.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes\""; 
SqlConnection SqlCon = new SqlConnection(); 
SqlCon.ConnectionString = @"workstation id = PC-PC; user id=sa;Password=sapassword; data source=pc-pc; persist security info=True; initial catalog=CleanPayrollTest2"; 
string sSQLTable = "TestExcell"; 
string sClearSQL = "DELETE FROM " + sSQLTable; 
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlCon); 
SqlCon.Open(); 
SqlCmd.ExecuteNonQuery(); 
SqlCon.Close(); 
DataTable dtSchema; 
dtSchema = ExcelCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 
OleDbCommand Command = new OleDbCommand ("select * FROM [" + dtSchema.Rows[0]["TABLE_NAME"].ToString() + "]", ExcelCon); 
OleDbDataAdapter da = new OleDbDataAdapter(Command); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
dataGrid1.DataSource = ds.Tables[0]; 
    OleDbDataReader dr = Command.ExecuteReader(); 
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString); 
bulkCopy.DestinationTableName = sSQLTable; 
while (dr.Read()) 
{ 
    bulkCopy.WriteToServer(dr); 
} 

错误:

-The类型或命名空间名称bulkCopy“找不到(?是否缺少using指令或程序集引用)

- 无法找到类型或名称空间名称'SqlBulkCopy'(您是否缺少使用指令或程序集引用?)

- 找不到类型或名称空间名称'OleDbConn'(您是否缺少using指令或一个程序集引用? )

+0

你包括使用System.Data.SqlClient的;在你的类定义的顶部? – Matt

+1

错误说明了这一切。无法找到类型,因为您要么缺少'using'指令,要么缺少程序集引用。添加指定正确名称空间的'using'子句,和/或添加适当的程序集引用。如果你将采取一看[文件](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx),你就已经看到了'SqlBulkCopy'class位于'System.Data.dll'程序集的'System.Data.SqlClient'命名空间中。 – CodeCaster

+0

我向你保证,我没有写的东西使用所有... – Nejthe

回答

2

SqlBulkCopy类属于对System.Data.SqlClient命名空间。将您的代码添加为它的名称空间;

using System.Data.SqlClient; 

这个命名空间包含在System.Data.dll

对于添加在Visual Studio中的参考,你可以右键点击 “Reference” 在解决方案资源管理器,然后单击Add Reference

enter image description here

在搜索框中搜索System.Data,顶部结果System.Data DLL添加到您的解决方案。

enter image description here

MSDN退房为How to: Add or Remove References By Using the Add Reference Dialog Box更多信息。

+0

我已经做到了 – Nejthe

+0

@Nejthe什么是'bulkCopy'的类型和'OleDbConn'那么唯一的错误? –

+0

@Nejthe在同一个方法中定义了'bulkCopy'和'OleDbConn'吗? –

1

您是否在项目中引用了System.Data.dll,并且在文件中是否有using System.Data.SqlClient声明?

+0

是的,我有一个使用System.Data.SqlClient语句 但是我如何检查如果我有referenxe到System.Data.dll? 对不起,我是新来的c# – Nejthe

+0

在Visual Studio中,展开你的项目,你应该看到一个'References'文件夹。展开,并检查你可以在列表中看到'System.Data'。 –

+0

是的,我可以看到它 – Nejthe

相关问题