Add these 2 namespaces :
using System.Data.SqlClient;
using Microsoft.SharePoint;
Add this code and call GetData method
private void GetData()
{
try
{
string query = "SELECT * FROM [DATABASENAME].[dbo].[TABLENAME] WHERE [ID] IS NOT NULL";
DataSet ds = getDataFromSQL(query, CommandType.Text);
if (ds != null && ds.Tables.Count > 0)
{
if (ds.Tables[0] != null & ds.Tables[0].Rows.Count > 0)
{
using(SPSite site = new SPSite("siteUrl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("");
if (list != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
SPListItem item = list.AddItem();
item["colName1"] = Convert.ToString(dr["colName"]);
item["colName2"] = Convert.ToString(dr["colName"]);
//... And so on
item.Update();
}
}
}
}
}
}
}
catch { }
}
private DataSet getDataFromSQL(string dbQuery, CommandType dbType)
{
DataSet dsToReturn = null;
try
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "your connection string here";
using (SqlConnection dsConnection = connection)
{
try { dsConnection.Open(); }
catch { }
using (SqlCommand dsCommand = new SqlCommand())
{
dsCommand.CommandType = dbType;
dsCommand.CommandText = dbQuery;
dsCommand.Connection = dsConnection;
SqlDataAdapter daToFillDataSet = new SqlDataAdapter(dsCommand);
dsToReturn = new DataSet();
daToFillDataSet.Fill(dsToReturn);
//dsConnection.Close();
//dsCommand.Dispose();
}
}
}
catch(Exception){
return dsToReturn;
}
return dsToReturn;
}
You will need to update connection string, database name, table name, column name