How to read data from database & write it on a txt file? - c#

I want to retrieve data from the server database using windows services on a text file?Can i do that?How will i do that?I mean i know to create a window service but i am confused with the SQL connection and file writing part where to execute it?i have tried something
class Program
{
static void Main(string[] args)
{
RunSchedule();
}
public static void RunSchedule()
{
string path = Path.GetFullPath("d:\\MyTest") + "\\" + DateTime.Now.ToString("MM_dd_yyyy_HH_mm") + "_Log.txt";
try
{
if (!File.Exists(path))
{
File.Create(path);
SqlConnection conn = new SqlConnection("Data Source=...;Initial Catalog=Test;User ID=s_a;Password=sa56ta112;");
String sql = #"SELECT Id,UserName, Email,Password,CreatedDate
FROM Register";
SqlCommand com = new SqlCommand();
com.CommandText = sql;
//com.Connection = conn;
conn.Open();
StreamWriter tw = File.AppendText("d:\\MyTest");
SqlDataReader reader = com.ExecuteReader();
tw.WriteLine("Id,UserName,Email,Password,CreatedDate");
while (reader.Read())
{
tw.Write(reader["Id"].ToString());
tw.Write(" , " + reader["UserName"].ToString());
tw.Write(" , " + reader["Email"].ToString());
tw.Write(" , " + reader["Password"].ToString());
tw.Write(" , " + reader["CreatedDate"].ToString());
}
tw.WriteLine(DateTime.Now);
tw.WriteLine("---------------------------------");
tw.Close();
reader.Close();
conn.Close();
}
}
catch (Exception ex)
{
string errorLogPath = #"D:\MyTest.txt";
File.AppendAllText(errorLogPath, Environment.NewLine + ex.Message);
}
}
}
am i doing it correct? Please guide me.

while (reader.Read())
{
row = new DataRow();
row.ItemArray = new object[reader.FieldCount];
reader.GetValues(row.ItemArray);
foreach (object item in row.ItemArray)
{
streamWriter.Write((string)item + "\t");
}
streamWriter.WriteLine();
}

If you get "Access to the path 'd:\MyTest' is denied." error than go to the txt file properties in the solution explorer and than change the Copy to output directory property from Do not copy into Copy if newer or Copy always

Related

How to fix “The ConnectionString property has not been initialized”

Hi everyone ı hade a bug about this methots ı use connection string a xml file. firt line is my xml file when ı run to code it's not
working ı think this code right but ı can't find where is the bug
this is connection string
<?xml version="1.0" encoding="utf-8" ?>
<conn>
<Dbconn>
<cstring>Server=DESKTOP-DSGBABB;Trusted_Connection=True ;database=master</cstring>
</Dbconn>
</conn>
this is my code
private void materialFlatButton1_Click(object sender, EventArgs e)
{
CreateDB();
}
public void CreateDB()
{
XmlTextReader reader = null;
reader = new XmlTextReader(filename);
reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "cstring")
{
conn = reader.ReadString();
}
}
using (connection = new SqlConnection(conn))
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
str = "CREATE DATABASE uc ON PRIMARY " +
"(NAME = uc, " +
"FILENAME = 'C:\\uc.mdf', " +
"SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = uc_Log, " +
"FILENAME = 'C:\\uc.ldf', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";
connection.Close();
SqlCommand myCommand = new SqlCommand(str, connection);
try
{
connection.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("Veritabanı Başarıyla Oluşturuldu");
connection.Close();
}
catch (System.Exception ex)
{
lbls.Text = #"Error occured.
+ " + ex.Message.ToString();
}
connection.Close();
}
}
}
ı will wait for your help
Please try to change your connection string as follows:
Data Source=DESKTOP-DSGBABB;Initial Catalog=master;Integrated Security=True;
So the XML will be like below:
<?xml version="1.0" encoding="utf-8"?>
<conn>
<Dbconn>
<cstring>Data Source=DESKTOP-DSGBABB;Initial Catalog=master;Integrated Security=True;</cstring>
</Dbconn>
</conn>
It is much easier to use LINQ to XML API while dealing with XML.
Here is how to retrieve a connection string from the XML file.
Additionally, the SqlConnectionStringBuilder type will validate correctness of the connection string.
c#
void Main()
{
const string configFile = #"e:\Temp\configXMLFile.xml";
XDocument xdoc = XDocument.Load(configFile);
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.ConnectionString = xdoc.Descendants("Dbconn").Elements("cstring").FirstOrDefault().Value;
using (connection = new SqlConnection(conn))
{
...
}
}

How to retrieve column names when saving a SQL query to a text file

I wrote a method which saves the content of a specific table to a text file. Unfortunately the names of the columns are not retrieved. "Only" the data of each cell is written to the text file.
How do i need to adapt my code to also include the name of the columns?
private void WriteSQLQueryOutputToTextFile(string DBUser, string DBUserPassword, string sqlQuery, string databaseName, string nameOfOutputFile, string nameOfRow0, string nameOfRow1, string nameOfRow2)
{
StreamWriter outputFile = new StreamWriter(dWTestResult + "\\DatabaseUpgradeCheck\\" + nameOfOutputFile);
using (SqlConnection sqlCon = new SqlConnection("Data Source=" + GetEnvironmentVariable.MachineName + "; Initial Catalog=" + databaseName + "; User ID=" + DBUser + "; Password=" + DBUserPassword + ";"))
{
SqlCommand command = new SqlCommand(sqlQuery, sqlCon);
sqlCon.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
outputFile.WriteLine(String.Format("{0}, {1}, {2}",
reader[nameOfRow0], reader[nameOfRow1], reader[nameOfRow2]));
}
}
catch (Exception ex)
{
logger.Debug(ex, "Writing Database Output to the text file failed");
}
finally
{
reader.Close();
outputFile.Close();
}
}
}
Add a count variable, and if count == 0 add the column names. It looks like you know the names of the columns already so you have a couple of options.
First option: Just write the name.
try
{
int count = 0;
while (reader.Read())
{
if (count == 0)
{
outputFile.WriteLine(String.Format("{0}, {1}, {2}",
nameOfRow0, nameOfRow1, nameOfRow2));
}
outputFile.WriteLine(String.Format("{0}, {1}, {2}",
reader[nameOfRow0], reader[nameOfRow1], reader[nameOfRow2]));
count++;
}
}
Or (if you don't know the column names) use reader.GetName(i):
try
{
int count = 0;
while (reader.Read())
{
// if this is the first row, read the column names
if (count == 0)
{
outputFile.WriteLine(String.Format("{0}, {1}, {2}",
reader.GetName(0), reader.GetName(1), reader.GetName(2)));
}
// otherwise just the data (including 1st row)
outputFile.WriteLine(String.Format("{0}, {1}, {2}",
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2)));
count++;
}
}
hi i think you can use : reader.GetName('index') for example for first column : reader.GetName(0)
See more at this link : https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader.getname?view=netframework-4.7.2
Please try this and set data row and column name as your need.
using (SqlConnection sqlCon = new SqlConnection("Data Source=" + GetEnvironmentVariable.MachineName + "; Initial Catalog=" + databaseName + "; User ID=" + DBUser + "; Password=" + DBUserPassword + ";"))
{
SqlCommand command = new SqlCommand(sqlQuery, sqlCon);
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.fill(dt);
try
{
if(dt != null && dt.Rows.Count > 0)
{
string columnName = dt.Columns[0].ToString();
DataRow dr = dt.Rows[0];
}
}
catch (Exception ex)
{
logger.Debug(ex, "Writing Database Output to the text file failed");
}
finally
{
reader.Close();
outputFile.Close();
}
}

Deleting empty rows and columns from a CSV file - C#

I would like to "clean" a CSV file:
deleting empty rows
deleting empty columns
The rows or columns are not completely empty, they have, for example:
"","","","","","","","","","","","","","",
(in a row form)
OR
"","","","","","","","","","",
(in a row form)
OR
"",
"",
"",
"",
"",
"",
"",
(in a columns form)
These rows or columns can be anywhere in the CSV file.
What I have so far:
private void button1_Click(object sender, EventArgs e)
{
string sourceFile = #"XXXXX.xlsx";
string worksheetName = "Sample";
string targetFile = #"C:\Users\xxxx\xls_test\XXXX.csv";
// Creates the CSV file based on the XLS file
ExcelToCSVCoversion(sourceFile, worksheetName, targetFile);
// Manipulate the CSV: Clean empty rows
DeleteEmptyRoadFromCSV(targetFile);
}
static void ExcelToCSVCoversion(string sourceFile, string worksheetName,
string targetFile)
{
string connectionString = #"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile
+ #";Extended Properties=""Excel 12.0 Xml;HDR=YES""";
OleDbConnection connection = null;
StreamWriter writer = null;
OleDbCommand command = null;
OleDbDataAdapter dataAdapter = null;
try
{
// Represents an open connection to a data source.
connection = new OleDbConnection(connectionString);
connection.Open();
// Represents a SQL statement or stored procedure to execute
// against a data source.
command = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]",
connection);
// Specifies how a command string is interpreted.
command.CommandType = CommandType.Text;
// Implements a TextWriter for writing characters to the output stream
// in a particular encoding.
writer = new StreamWriter(targetFile);
// Represents a set of data commands and a database connection that are
// used to fill the DataSet and update the data source.
dataAdapter = new OleDbDataAdapter(command);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
for (int row = 0; row < dataTable.Rows.Count; row++)
{
string rowString = "";
for (int column = 0; column < dataTable.Columns.Count; column++)
{
rowString += "\"" + dataTable.Rows[row][column].ToString() + "\",";
}
writer.WriteLine(rowString);
}
Console.WriteLine();
Console.WriteLine("The excel file " + sourceFile + " has been converted " +
"into " + targetFile + " (CSV format).");
Console.WriteLine();
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
Console.ReadLine();
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
connection.Dispose();
command.Dispose();
dataAdapter.Dispose();
writer.Close();
writer.Dispose();
}
}
static void DeleteEmptyRoadFromCSV(string fileName)
{
//string nonEmptyLines = #"XXXX.csv";
var nonEmptyLines = File.ReadAllLines(fileName)
.Where(x => !x.Split(',')
.Take(2)
.Any(cell => string.IsNullOrWhiteSpace(cell))
// use `All` if you want to ignore only if both columns are empty.
).ToList();
File.WriteAllLines(fileName, nonEmptyLines);
}
Finally, I tried to use the ideas from:
Remove Blank rows from csv c# . But my ouput is not changing at all.
Any help is welcome!
Thank you.
You could delete columns/rows from table before saving csv.
Method is not tested, but you should get the concept.
static void ExcelToCSVCoversion(string sourceFile, string worksheetName,
string targetFile)
{
string connectionString = #"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile
+ #";Extended Properties=""Excel 12.0 Xml;HDR=YES""";
OleDbConnection connection = null;
StreamWriter writer = null;
OleDbCommand command = null;
OleDbDataAdapter dataAdapter = null;
try
{
// Represents an open connection to a data source.
connection = new OleDbConnection(connectionString);
connection.Open();
// Represents a SQL statement or stored procedure to execute
// against a data source.
command = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]",
connection);
// Specifies how a command string is interpreted.
command.CommandType = CommandType.Text;
// Implements a TextWriter for writing characters to the output stream
// in a particular encoding.
writer = new StreamWriter(targetFile);
// Represents a set of data commands and a database connection that are
// used to fill the DataSet and update the data source.
dataAdapter = new OleDbDataAdapter(command);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
var emptyRows =
dataTable.Select()
.Where(
row =>
dataTable.Columns.Cast<DataColumn>()
.All(column => string.IsNullOrEmpty(row[column].ToString()))).ToArray();
Array.ForEach(emptyRows, x => x.Delete());
var emptyColumns =
dataTable.Columns.Cast<DataColumn>()
.Where(column => dataTable.Select().All(row => string.IsNullOrEmpty(row[column].ToString())))
.ToArray();
Array.ForEach(emptyColumns, column => dataTable.Columns.Remove(column));
dataTable.AcceptChanges();
for (int row = 0; row < dataTable.Rows.Count; row++)
{
string rowString = "";
for (int column = 0; column < dataTable.Columns.Count; column++)
{
rowString += "\"" + dataTable.Rows[row][column].ToString() + "\",";
}
writer.WriteLine(rowString);
}
Console.WriteLine();
Console.WriteLine("The excel file " + sourceFile + " has been converted " +
"into " + targetFile + " (CSV format).");
Console.WriteLine();
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
Console.ReadLine();
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
connection.Dispose();
command.Dispose();
dataAdapter.Dispose();
writer.Close();
writer.Dispose();
}
}
Please check if the following query is working.I am getting all the rows:
var nonEmptyLines = File.ReadAllLines(FileName)
.Where(x => !x.Split(',')
.Take(2)
.Any(cell => string.IsNullOrWhiteSpace(cell))
// use `All` if you want to ignore only if both columns are empty.
).ToList();
I think you can use something as:
var nonEmptyLines = File.ReadAllLines(File).
SkipWhile(cell=>{var arr=cell.Split(',');if(string.IsNullOrWhiteSpace(cell)){
return true;
}
else
{
return false;
}
});

Program won't load to selected database, makes a copy in debug folder

This program is supposed to take a csv file and load it to a sqlite database that the use specifies a path to. Rather than trying to load the values to the selected database it creates a copy of the database in the debug folder and then throws an error because the table doesn't exist in the new db. I can't find where it's deciding to make a new file rather than using the existing one.
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=" + db3FilePath + "; Synchronous=Off"))
{
using (System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(conn))
{
conn.Open();
foreach (KeyValuePair<string, CSVRecord> kvp in csvDictionary.Skip(1))
{
//checks for duplicate records
cmd.CommandText = "SELECT COUNT(*) FROM Accounts WHERE SDM_ACCT='" + kvp.Value.sdmacct + "'";
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count < 1)
{
WritetoDatabase.WritetoAccountsTable(kvp.Value.description, kvp.Value.priority, db3FilePath);
WritetoDatabase.WritetoDirectoryTable(kvp.Value.number, kvp.Value.active, db3FilePath);
recordCount++;
//updates the progress bar and text field showing %
int progress = y++ * 100 / (csvDictionary.Keys.Count -1);
progressBar1.Invoke((MethodInvoker)(() => progressBar1.Value = progress));
progressBar1.Invoke((MethodInvoker)(() => progressBar1.Update()));
lblStatus.Invoke((MethodInvoker)(() => lblStatus.Text = "Writing records to database: " + progress.ToString() + "% Complete"));
lblStatus.Invoke((MethodInvoker)(() => lblStatus.Update()));
}
else
{
WritetoDatabase.WritetoDirectoryTable(kvp.Value.number, kvp.Value.active, db3FilePath);
y++;
}
}
conn.Close();
}
}
Here is a copy of the method that writes to the database.
public static int WritetoAccountsTable(string Comment, int PriorityInt, string filePath)
{
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=" + filePath + "; Synchronous=Off"))
{
using (System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(conn))
{
conn.Open();
cmd.CommandText = #"INSERT INTO Accounts(Description,Priority)
values(#Comment,#PriorityInt)";
cmd.Parameters.AddWithValue("#Comment", Comment);
cmd.Parameters.AddWithValue("#PriorityInt", PriorityInt);
return cmd.ExecuteNonQuery();
}
}
}
I've changed everything I can think of trying to find what's causing the problem. Maybe you will see something I can't.
One of the all-time dumbest things I have seen break a program. The Connection String using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=" + filePath + "; Synchronous=Off")) has "data source". If it is not capitalized, "Data Source", it doesn't recognize it and goes to a default setting.

Pass next line as anew command

I want to print the output of the stored procedure in a .csv file.
When I insert a single stored procedure such as exec spGet Table 5 1,null,null,null,111,null,null,null,61,null,null,3;
Along with its parameters it executes. But when I pass the same procedure multiple times with different parameters, It only executes the first Stored procedure and the remaining are ignored. In the CSV file i only get the first SP Output.
My code is as follows
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGetSku_Click(object sender, EventArgs e)
{
Stopwatch swra = new Stopwatch();
swra.Start();
StreamWriter CsvfileWriter = new StreamWriter(#"D:\testfile.csv");
string connectionString = null;
SqlConnection cnn;
connectionString = "Data Source=My-PC-Name;Initial Catalog=MyDB;User
cnn = new SqlConnection(connectionString);
ID=Name;Password=********";
cnn.Open();
SqlCommand cmd = new SqlCommand(textBox1.Text, cnn);
cmd.CommandText = textBox1.Text;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 2000;
using (cnn)
{
using (SqlDataReader rdr = cmd.ExecuteReader())
using (CsvfileWriter)
{
//For getting the Table Headers
DataTable Tablecolumns = new DataTable();
for (int i = 0; i < rdr.FieldCount; i++)
{
Tablecolumns.Columns.Add(rdr.GetName(i));
}
CsvfileWriter.WriteLine(string.Join(",",
Tablecolumns.Columns.Cast<DataColumn>().Select(csvfile =>
csvfile.ColumnName)));
while (rdr.Read())
{
label1.Text = rdr["SKU"].ToString() + " " +
rdr["SKUCode"].ToString();
CsvfileWriter.WriteLine(rdr["SKU"].ToString() + "," +
rdr["SKUCode"].ToString() + "," +
rdr["Compliance_Curr"].ToString() + "," +
rdr["Compliance_Prev"].ToString() + "," +
rdr["Difference"].ToString() + "," +
rdr["TotalSales_Curr"].ToString() + ",");
}
cnn.Close();
}
}
swra.Stop();
Console.WriteLine(swra.ElapsedMilliseconds);
}
}
I want to make sure that each procedure is executed differently and appended to the .csv file.
The problem you are facing is due to overwriting the file each time. So what you have in there is actually the result of the last execution of the command.
The culprit is the following line:
StreamWriter CsvfileWriter = new StreamWriter(#"D:\testfile.csv");
According to documentation,
If the file exists, it is overwritten; otherwise, a new file is created.
You need to use an overload of StreamWriter constructor which accepts a bool value specifying whether to append to the file or overwrite it.
var csvFileWriter = new StreamWriter(#"D:\testfile.csv", true);
Have you tried to call stored procedure with names of parameters (which declared in stored procedure)?
For example: EXECUTE spGet #Id = 1, #Number = 111, #....
In your code, you are creating StreamWriter object **everytime, you are clicking on **btnGetSku, Try to make it member variable and then write. Data is not being appended,
StreamWriter CsvfileWriter = null;
private void btnGetSku_Click(object sender, EventArgs e)
{
Stopwatch swra = new Stopwatch();
swra.Start();
if(CsvfileWriter == null)
CsvfileWriter = new StreamWriter(#"D:\testfile.csv");
string connectionString = null;
SqlConnection cnn;
connectionString = "Data Source=My-PC-Name;Initial Catalog=MyDB;User
cnn = new SqlConnection(connectionString);
ID=Name;Password=********";
cnn.Open();
SqlCommand cmd = new SqlCommand(textBox1.Text, cnn);
cmd.CommandText = textBox1.Text;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 2000;
using (cnn)
{
using (SqlDataReader rdr = cmd.ExecuteReader())
// Don't use using here. This disposes the streams
//using (CsvfileWriter)
{
//For getting the Table Headers
DataTable Tablecolumns = new DataTable();
for (int i = 0; i < rdr.FieldCount; i++)
{
Tablecolumns.Columns.Add(rdr.GetName(i));
}
CsvfileWriter.WriteLine(string.Join(",",
Tablecolumns.Columns.Cast<DataColumn>().Select(csvfile =>
csvfile.ColumnName)));
while (rdr.Read())
{
label1.Text = rdr["SKU"].ToString() + " " +
rdr["SKUCode"].ToString();
CsvfileWriter.WriteLine(rdr["SKU"].ToString() + "," +
rdr["SKUCode"].ToString() + "," +
rdr["Compliance_Curr"].ToString() + "," +
rdr["Compliance_Prev"].ToString() + "," +
rdr["Difference"].ToString() + "," +
rdr["TotalSales_Curr"].ToString() + ",");
}
cnn.Close();
}
}
swra.Stop();
Console.WriteLine(swra.ElapsedMilliseconds);
}

Categories

Resources