【问题标题】:The type or namespace name 'SqlBulkCopy' could not be found找不到类型或命名空间名称“SqlBulkCopy”
【发布时间】: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


【解决方案1】:

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

using System.Data.SqlClient;

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

要在 Visual Studio 中添加引用,您可以在解决方案资源管理器中右键单击“Reference”,然后单击Add Reference

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

MSDN 中查看有关 How to: Add or Remove References By Using the Add Reference Dialog Box 的更多信息。

【讨论】:

  • @Nejthe 那么bulkCopyOleDbConn 的类型是什么?
  • @Nejthe bulkCopyOleDbConn 是否在您的方法中定义?
【解决方案2】:

您的项目中是否引用了System.Data.dll,您的文件中是否有using System.Data.SqlClient 语句?

【讨论】:

  • 是的,我有一个 using System.Data.SqlClient 语句但是我如何检查我是否有对 System.Data.dll 的引用?抱歉,我是 C# 新手
  • 在 Visual Studio 中,展开您的项目,您应该会看到一个 References 文件夹。展开它并检查您是否可以在列表中看到System.Data
  • @Nejthe 那么我只能建议您使用完整的类定义(即包括您的 using 语句、类声明等)更新您的答案。还请确保您格式化您的代码,使其更具可读性。
  • 对不起,如果它的可读性不好..你现在检查问题,我添加了 using 语句
  • 我将您的代码复制到一个新类中,并得到以下构建错误:Cannot resolve symbol dataGrid1Cannot resolve symbol sSqlConnectionString。我不能再帮忙了,因为很明显这不是所有的代码。
【解决方案3】:

安装 NuGet 包:System.Data.SqlClient

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 2011-05-13
    • 2013-03-25
    • 2012-06-19
    • 2017-11-29
    • 2012-09-27
    • 2011-05-06
    相关资源
    最近更新 更多