Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'SqlBulkCopy' could not be found

can someone help me please to fix that error. this is my code :

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);
}

Errors:

-The type or namespace name 'bulkCopy' could not be found (are you missing a using directive or an assembly reference?)

-The type or namespace name 'SqlBulkCopy' could not be found (are you missing a using directive or an assembly reference?)

-The type or namespace name 'OleDbConn' could not be found (are you missing a using directive or an assembly reference?)

like image 650
Nejthe Avatar asked Dec 12 '25 14:12

Nejthe


2 Answers

SqlBulkCopy class belongs on System.Data.SqlClient namespace. Add your code as a namespace it like;

using System.Data.SqlClient;

This namespace contains in System.Data.dll

For adding reference in Visual Studio, you can right click "Reference" in Solution Explorer and click Add Reference.

enter image description here

Search System.Data in search box, and Add the top result System.Data dll to your solution.

enter image description here

Check out for more information for How to: Add or Remove References By Using the Add Reference Dialog Box from MSDN.

like image 162
Soner Gönül Avatar answered Dec 14 '25 02:12

Soner Gönül


Install NuGet package: System.Data.SqlClient

enter image description here

like image 37
Drilon Ahmetaj Avatar answered Dec 14 '25 04:12

Drilon Ahmetaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!