【发布时间】:2013-01-07 00:15:48
【问题描述】:
谁能帮我解决这个错误。 这是我的代码:
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);
}
错误:
-找不到类型或命名空间名称“bulkCopy”(您是否缺少 using 指令或程序集引用?)
-找不到类型或命名空间名称“SqlBulkCopy”(您是否缺少 using 指令或程序集引用?)
-找不到类型或命名空间名称“OleDbConn”(您是否缺少 using 指令或程序集引用?)
【问题讨论】:
-
你包括Using System.Data.SqlClient;在你的类定义的顶部?
-
错误说明了一切。找不到类型,因为您缺少
using指令或程序集引用。添加指定正确命名空间的using子句,和/或添加适当的程序集引用。如果您查看documentation,您会看到SqlBulkCopyclass 在System.Data.dll程序集的System.Data.SqlClient命名空间中。 -
我向你保证,我确实写了所有使用的东西......
-
@Nejthe 很高兴在您的问题中阅读。您是否还添加了对
System.Data.dll的引用?该项目是否出现其他构建错误? -
如何添加对 System.Data.dll 的引用?不,这是唯一的错误非常感谢您
标签: c# excel namespaces sqlbulkcopy