I have written a simple code in arcobjects so if you have installed ArcGIS Basic (ArcView) or ArcGIS Engine you can create a console application using this code c#. Change in code the enum for your license (esriLicenseProductCode).
//author: nicogis
private static LicenseInitializer m_AOLicenseInitializer = new shape2SQL.LicenseInitializer();
[STAThread()]
static void Main(string[] args)
{
//ESRI License Initializer generated code.
if (!m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeBasic },
new esriLicenseExtensionCode[] { }))
{
System.Console.WriteLine(m_AOLicenseInitializer.LicenseMessage());
System.Console.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown.");
m_AOLicenseInitializer.ShutdownApplication();
return;
}
try
{
//append the shapefile in a table of sql server
//with a field type sqlGeometry
//in this section set your parameters
/******************************************************************************/
//path full of shapefile import in sql server
string shapefileLocation = @"C:\Temp\Varie\shape2SQL\ImportShape.shp";
//name table in sql server
string nameTable = "TestImport";
//name field geometry in sql server
string nameFieldShape = "Geom";
//insert the fields of shapefile without add shape field. These fields also must be exists in the table of sql server
List<string> lstFields = new List<string>();
lstFields.Add("fieldName1");
lstFields.Add("fieldName2");
lstFields.Add("fieldName3");
//..
/******************************************************************************/
lstFields.Add(nameFieldShape);
//ShapefileWorkspaceFactory is singleton so use activator
Type t = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
System.Object obj = Activator.CreateInstance(t);
IWorkspaceFactory workspaceFactory = obj as IWorkspaceFactory;
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0);
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));
using (ComReleaser comReleaser = new ComReleaser())
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlServerConnectionString"].ConnectionString))
{
conn.Open();
string sqlCommandText = string.Format("insert into [{0}]({1}) Values({2})", nameTable, string.Join(",", lstFields.ToArray()), string.Join(",", lstFields.ConvertAll(s => "@p" + s).ToArray()));
IFeatureCursor featureCursor = featureClass.Search(null, true);
comReleaser.ManageLifetime(featureCursor);
Dictionary<string, int> dctIndexFields = new Dictionary<string, int>();
foreach (string s in lstFields)
{
if (s == nameFieldShape)
{
continue;
}
dctIndexFields.Add(s, featureClass.FindField(s));
}
IFeature feature = null;
while ((feature = featureCursor.NextFeature()) != null)
{
IGeometry geometry = feature.ShapeCopy as IGeometry;
IGeometryFactory3 geometryFactory = new GeometryEnvironment() as IGeometryFactory3;
byte[] wkb = geometryFactory.CreateWkbVariantFromGeometry(geometry) as byte[];
SqlBytes polygonWKB = new SqlBytes(wkb);
SqlGeometry sqlGeometry = SqlGeometry.STGeomFromWKB(polygonWKB, geometry.SpatialReference.FactoryCode);
using (SqlCommand sqlCommand = new SqlCommand(sqlCommandText, conn))
{
sqlCommand.CommandType = CommandType.Text;
foreach (string i in dctIndexFields.Keys)
{
sqlCommand.Parameters.Add(new SqlParameter("@p" + i, feature.get_Value(dctIndexFields[i])));
}
sqlCommand.Parameters.Add(new SqlParameter("@p" + nameFieldShape, sqlGeometry) { UdtTypeName = "Geometry" });
sqlCommand.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
finally
{
//ESRI License Initializer generated code.
//Do not make any call to ArcObjects after ShutDownApplication()
m_AOLicenseInitializer.ShutdownApplication();
}
}
}
}