Fill and append data dynamically from mutiple OledbConnections to one single OleDbDataAdapter - c#

I'm trying to dynamically add rows from some excel files located in a directory to a OleDbDataAdapter.
So far,I managed to add some data to the OleDbDataAdapter by dynamically generating OleDbConnections that each point to the respective excel files from which i want to read data. However, the code stops after reading the first excel file. I suspect that I have to clear some variables but I'm kinda stuck for now.
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApp35
{
class Program
{
static void Main(string[] args)
{
string dirALC_EDC = #"C:\Users\________\Desktop\Mission\Fichiers\ALC_EDC\";
var files = Directory.GetFiles(dirALC_EDC, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
String theConnString = (String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", file));
OleDbConnection excelConnection = new OleDbConnection(theConnString);
excelConnection.Open();
var dt = new DataTable();
var da = new OleDbDataAdapter();
var _command = new OleDbCommand();
_command.Connection = excelConnection;
_command.CommandText = "select * FROM [Sheet1$]";
da.SelectCommand = _command;
try
{
da.Fill(dt);
for (int i = 4; i <= dt.Rows.Count; i++)
{
Console.WriteLine(String.Join(", ", dt.Rows[i].ItemArray));
}
}
catch (Exception e)
{
// process error here
}
Console.ReadLine();
}
}
}
}

Related

ADO.NET connection to SharePoint List causing memory leak

I am connecting to a SharePoint list using the following code. The problem is that if I run a Command, the console application hangs and doesn't close. I can see that the application is still consuming memory. Without a Command, the debugger exits cleanly.
I am also looking for a way to connect to SharePoint with EntityFramework, or a more modern way that doesn't use the SharePoint API.
The sample uses several methods to connect to the same SharePoint list.
namespace TestADO
{
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using ADODB;
internal class Program
{
public static string ConnectionStringBase = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes;Pooling=false;DATABASE=https://.../sites/...;LIST={xxxxxxx-ac0a-4cb3-8a32-ecbdxxxxxxxx}";
public static string sql = "select top 10 * from [Call Distributions]";
static void Main(string[] args)
{
ADONetImplementationWithAdapter();
ADONetImplementation();
}
static void ADONetImplementationWithAdapter()
{
OleDbDataAdapter oledbAdapter;
using (OleDbConnection conn = new OleDbConnection(ConnectionStringBase))
{
DataSet ds = new DataSet();
conn.Open();
oledbAdapter = new OleDbDataAdapter(sql, conn);
oledbAdapter.Fill(ds);
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
Console.WriteLine(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
}
oledbAdapter.Dispose();
conn.Close();
}
}
static void ADONetImplementation()
{
using (OleDbConnection conn = new OleDbConnection(ConnectionStringBase))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetValue(0) + " - " + reader.GetValue(1) + " - " + reader.GetValue(2));
}
reader.Close();
cmd.Dispose();
}
conn.Close();
}
}
static void ADOImplementation()
{
Connection connection = new Connection();
connection.CursorLocation = CursorLocationEnum.adUseServer;
connection.ConnectionString = ConnectionStringBase;
connection.Open();
object res;
var rst = connection.Execute(sql, out res);
while (!rst.EOF)
{
rst.MoveNext();
}
rst.Close();
rst = null;
//Recordset recordset = new Recordset();
//recordset.Open(sql, connection, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic);
//recordset.Close();
//recordset = null;
connection.Close();
connection = null;
}
}
}
Please try to use the following code to connect to SharePoint:
using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Security;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string siteCollectionUrl = "https://globalsharepoint2020.sharepoint.com";
string userName = "YourSPOUserName#yourtenant.onmicrosoft.com";
string password = "YourSPOPassword";
Program obj = new Program();
try
{
obj.ConnectToSharePointOnline(siteCollectionUrl, userName, password);
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
}
}
public void ConnectToSharePointOnline(string siteCollUrl, string userName, string password)
{
//Namespace: It belongs to Microsoft.SharePoint.Client
ClientContext ctx = new ClientContext(siteCollUrl);
// Namespace: It belongs to System.Security
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
// Namespace: It belongs to Microsoft.SharePoint.Client
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);
// Namespace: It belongs to Microsoft.SharePoint.Client
Site mySite = ctx.Site;
ctx.Load(mySite);
ctx.ExecuteQuery();
Console.WriteLine(mySite.Url.ToString());
}
}
}

System.ArgumentException: „Column does not belong to table .” - how to fix this error (import data from csv file to mysql database)?

In this case I'd like to import data from csv file to mysql database. For now i've written method which inserts that file:
1) Select file with extension "*.csv"
2) After select it loads data by dividing commas and continuing if are empty cells.
private DataTable ImportFile()
{
DataTable imported_data = new DataTable();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open csv file";
ofd.DefaultExt = "*.csv";
ofd.Filter = "Documents (*.csv)|*.csv";
ofd.ShowDialog();
FileInfo fi = new FileInfo(ofd.FileName);
string FileName1 = ofd.FileName;
string excel = fi.FullName;
using(StreamReader sr = new StreamReader(excel))
{
string header = sr.ReadLine();
if (string.IsNullOrEmpty(header))
{
MessageBox.Show("Not found or loaded not correct file.");
return null;
}
string[] header_columns = header.Split(',');
foreach(string header_column in header_columns)
{
imported_data.Columns.Add(header_column);
}
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (string.IsNullOrEmpty(linia)) continue;
string[] fields = line.Split(',');
DataRow imported_row = imported_data.NewRow();
for (int i = 0; i < fields.Count(); i++)
{
imported_row[i] = fields[i];
}
imported_data.Rows.Add(imported_row);
}
}
return imported_data;
}
Then the second method is when it has a connection with mysql database it inserts values to that database:
private void save_modules(DataTable imported_data)
{
string connection = "datasource=localhost;port=3306;username=root;password=";
using (MySqlConnection conn = new MySqlConnection(connection))
{
conn.Open();
foreach (DataRow importRow in imported_data.Rows)
{
string query3 = #"INSERT IGNORE INTO try1.modules (NAME, ID_PROJECT) SELECT #NAME, projekty.ID
FROM try1.projects WHERE projects.PROJECT_NAME = #PROJECT_NAME;";
MySqlCommand cmd = new MySqlCommand(query3, conn);
cmd.Parameters.AddWithValue("#NAME", importRow["NAME"]);
cmd.Parameters.AddWithValue("#PROJECT_NAME", importRow["PROJECT_NAME"]);
cmd.ExecuteNonQuery();
}
conn.Close();
}
MessageBox.Show("Imported to database");
}
And event when clicks to btn_import_projects.
private void btn_import_projects_Click(object sender, EventArgs e)
{
DataTable imported_data = ImportFile();
save_modules(imported_data);
frm2.loaddataalldatagridview();
}
when I compiled connected from 3 codes lines to 1 code i had an exception error:
System.ArgumentException: „Column NAME does not belong to table .”
And shows at the line of code:
cmd.Parameters.AddWithValue("#NAME", importRow["NAME"]);
I have a question how to fix this error? Should i change the code lines form cmd.Parameters.AddWithValue to cmd.Parameters.Add? Any ideas? Thx in advance.
UPDATE
for the case for only 1 column the code:
private void save_projects(DataTable imported_data)
{
string connection = "datasource=localhost;port=3306;username=root;password=";
using (MySqlConnection conn = new MySqlConnection(connection))
{
conn.Open();
foreach (DataRow importRow in imported_data.Rows)
{
string query2 = "INSERT IGNORE INTO try1.projects(PROJECT_NAME) VALUES (#PROJECT_NAME);";
MySqlCommand cmd = new MySqlCommand(query2, conn);
cmd.Parameters.AddWithValue("#PROJECT_NAME", importRow["PROJECT_NAME"]);
cmd.ExecuteNonQuery();
}
conn.Close();
}
MessageBox.Show("Imported to database");
}
It works without any small error.
There is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace ControlDataBase
{
public partial class Import_data_mysql : Form
{
public Import_data_mysql()
{
InitializeComponent();
}
New_Tables frm2 = (New_Tables)Application.OpenForms["New_Tables"];
private DataTable ImportFile()
{
DataTable imported_data = new DataTable();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open csv file";
ofd.DefaultExt = "*.csv";
ofd.Filter = "Documents (*.csv)|*.csv";
ofd.ShowDialog();
FileInfo fi = new FileInfo(ofd.FileName);
string FileName1 = ofd.FileName;
string excel = fi.FullName;
using(StreamReader sr = new StreamReader(excel))
{
string header = sr.ReadLine();
if (string.IsNullOrEmpty(header))
{
MessageBox.Show("Not found or loaded not correct file.");
return null;
}
string[] header_columns = header.Split(',');
foreach(string header_column in header_columns)
{
imported_data.Columns.Add(header_column);
}
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (string.IsNullOrEmpty(line)) continue;
string[] fields = line.Split(',');
DataRow imported_row = imported_data.NewRow();
for (int i = 0; i < fields.Count(); i++)
{
imported_row[i] = fields[i];
}
imported_data.Rows.Add(imported_row);
}
}
return imported_data;
}
private void save_modules(DataTable imported_data)
{
string connection = "datasource=localhost;port=3306;username=root;password=";
using (MySqlConnection conn = new MySqlConnection(connection))
{
conn.Open();
foreach (DataRow importRow in imported_data.Rows)
{
string query3 = #"INSERT IGNORE INTO try1.modules (NAME, ID_PROJECT) SELECT #NAME, projekty.ID
FROM try1.projects WHERE projects.PROJECT_NAME = #PROJECT_NAME;";
MySqlCommand cmd = new MySqlCommand(query3, conn);
cmd.Parameters.AddWithValue("#NAME", importRow["NAME"]);
cmd.Parameters.AddWithValue("#PROJECT_NAME", importRow["PROJECT_NAME"]);
cmd.ExecuteNonQuery();
}
conn.Close();
}
MessageBox.Show("Imported to database");
}
private void btn_import_projects_Click(object sender, EventArgs e)
{
try
{
DataTable imported_data = ImportFile();
save_modules(imported_data);
frm2.loaddataalldatagridview();
}
catch
{
MessageBox.Show("Select invalid file to import data.");
}
}
}
}
Ok. Responding to #soohoonigan advice I had to change only two lines of code:
string[] header_columns = header.Split(',');
string[] fields = line.Split(',');
After finding out that in .csb file is seperated with these ";" signs so i've changed these lines to this:
string[] header_columns = header.Split(';');
string[] fields = line.Split(';');
And works correctly. Thanks a alot mates. :)

compare the length of two variable

I am a beginner programmer.
I wrote following code and I want to compare the lenght of the Track ID (which is in my data set) with the Track ID (which is extracted from the web page) which both are highlighted.(my data set has two fields(user id-track id), i want to compare this track id with the track id which is return from web page)
I want to to it like:
if (client.Contains(Convert.ToString(TrackID))&&row["TrackID"].lenght=Track ID.lenght)
Thank you
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Net;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\hidden.accdb";
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "Select * from hidden.accdb";
DataTable dt = new DataTable();
//To read data from dataset
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
//Store the UserID
adapter.Fill(dt);
int UserID=0,TrackID=0;
int counter=0;
foreach(DataRow row in dt.Rows)
{
string url = "http://abcd/<userid>?groups=<userid>";
var test = url.Replace("<userid>", Convert.ToString(row["UserID"]));
System.Diagnostics.Process.Start(url);
string client = (new WebClient()).DownloadString("http://abcd/UserID?groups=UserID");
if (client.ToLower() == (Convert.ToString(TrackID).ToLower()))
{
counter++;
}
int ave = counter / 2916;
MessageBox.Show("Average" + counter);
}
conn.Close();
}
}
}
Why not do this?
foreach(DataRow row in dt.Rows)
{
string url = "http://abcd/<userid>?groups=<userid>";
var test = url.Replace("<userid>", Convert.ToString(row["UserID"]));
System.Diagnostics.Process.Start(url);
string client = (new WebClient()).DownloadString("http://abcd/UserID?groups=UserID");
if (client.ToLower() == Convert.ToString(TrackID).ToLower())
{
counter++;
}

Error in adapter.fill function

I am a beginner programmer.
I wrote the following code that shows an error in "adapter.fill(dt) ??????
this code must do the following steps:
1-connect to my dataset that has two fields :UserID,TrackID (2916 fields) 2-Read the dataset line by line and put the UserId of each recored to a url(instead of ). 3-Search through the webpage 4-if it finds the TrackId (exactly the same) which is related to UserId, add 1 to counter.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Net;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\hidden.accdb";
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "Select * from hidden.accdb";
DataTable dt = new DataTable();
//To read data from dataset
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
//Store the UserID
adapter.Fill(dt);
int UserID=0,TrackID=0;
int counter=0;
foreach(DataRow row in dt.Rows)
{
string url = "http://abcd/<userid>?groups=<userid>";
var test = url.Replace("<userid>", Convert.ToString(row["UserID"]));
System.Diagnostics.Process.Start(url);
string client = (new WebClient()).DownloadString("http://abcd/UserID?groups=UserID");
if (client.ToLower() == (Convert.ToString(TrackID).ToLower()))
{
counter++;
}
int ave = counter / 2916;
MessageBox.Show("Average" + counter);
}
conn.Close();
}
}
}
This is invalid:
"Select * from hidden.accdb"
hidden.accdb should be table name that contains the UserID and TrackID columns. not access database file name

Export multiple tables from SQL Server to XML for restore

I have a web application in C# / ASP.NET. I want to export all the database table's data, with the same unique key (Company ID). All the tables have this key (different for every company). I want to do that for future backup. Is it possible with Linq or classic C#? How can achieve this backup and restore?
One of the example to the solution is
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var connStr = "Data Source=MOHSINWIN8PRO\\SQLEXPRESS;Initial Catalog=AB2EDEMO;Integrated Security=True";
var xmlFileData = "";
DataSet ds = new DataSet();
var tables = new[] {"Hospital", "Patient"};
foreach (var table in tables)
{
var query = "SELECT * FROM "+ table +" WHERE (Hospital_Code = 'Hosp1')";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
conn.Dispose();
xmlFileData+=ds.GetXml();
}
File.WriteAllText("D://SelectiveDatabaseBackup.xml",xmlFileData);
}
}
}
this will create SelectiveDatabaseBackup.xml which can be later used to restore the backup
SaveFileDialog SD = new SaveFileDialog();
if (SD.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
System.IO.StreamWriter SW = new System.IO.StreamWriter(SD.FileName);
foreach (string str in checkedListBox1.CheckedItems)
{
string XMLSTR = //Heads.funtions.ConvertDatatableToXML(
Heads.funtions.fire_qry_for_string(string.Format("Select * from {0} FOR XML AUTO,TYPE, ELEMENTS ,ROOT('{0}')", str)); // , str);
SW.Write(XMLSTR);
SW.WriteLine("###TABLEEND###");
}
SW.Close();
//_CommonClasses._Cls_Alerts.ShowAlert("Clean Up Completed...!!!", "CleanUP", MessageBoxIcon.Information);
}

Categories