Fastest way to get multiple rows using npgsql - c#

I have a following code with List of guids and I need to get all corresponding items from database:
var guids = new List<string>()
{
"1HFAoQchL1NB9Tc5wJLYHm",
"1OLd2nLP92c87kmkHaMAtO",
"2FwmMnAX98sg537nfo$S39",
"38pd0XqUvEvA8nrnF$3vvZ",
"0iqYqWKBvC_x62wk0jcKW3",
"0njEfI2BzDEeQpiOo5S18q",
"0chMA4Bgf9nxnq2i1gG9Fb",
"3TrnV9L79FfBz0ni6_bJ9N"
};
foreach (var gd in guids)
{
MemoryStream stream = null;
using (var conn = new NpgsqlConnection(connectionString))
{
string sQL = $"SELECT geometricaldata FROM entities WHERE guid= '{gd}'";
using (var command = new NpgsqlCommand(sQL, conn))
{
conn.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
stream = new MemoryStream((byte[])reader[0]);
}
reader.Close();
}
}
}
}
Do I really need to do this in a loop one by one or it would be better to batch SELECT statements? Or maybe there is another way how to get multiple rows with one query that I don't know of?

Related

Refering to view values one by one

I have the following code which establishing an SQL connection inside of a project I am working on. What I want to do is to create a for loop which contains a method and every time the loop repeats the method runs with a different value until all views of the returned query are used.
I can't figure out how to refer to every value of the view without saving the view to a list or an array first. Any ideas?
SqlConnection Con = new SqlConnection(#"Data Source=localhost\**;Initial Catalog=ML;User Id=sa;Password='**'");
string sql = #"select product_id,Name from E_PRODUCT_PROPERTY";
var mylist = new List<WineRating>();
using (var command = new SqlCommand(sql, Con))
{
Con.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
new WineRating { product_id = reader.GetInt32(0), Name = reader.GetString(1) };
///Here goes the code I suppose
method_name(reader.GetInt32(0), reader.GetString(1));
}
}
public static int method_name(int product_id, string Name)
{
int num = x *2;
Console.WriteLine(num + Name);
}
Perhaps like this:
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
MyMethodToPrintToScreen(reader.GetInt32(0), reader.GetString(1));
}
}
With the method to print to screen
private static void MyMethodToPrintToScreen(int id, string product)
{
//Do whatever you wish with the data: example
Console.WriteLine($"My id: {id} | Product: {product}");
}
Edit
Let me make it even more obvious(using your exact code):
SqlConnection Con = new SqlConnection(#"Data Source=localhost\**;Initial Catalog=ML;User Id=sa;Password='**'");
string sql = #"select product_id,Name from E_PRODUCT_PROPERTY";
var mylist = new List<WineRating>();
using (var command = new SqlCommand(sql, Con))
{
Con.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
method_name(reader.GetInt32(0), reader.GetString(1));
}
}
}

efficient way to insert the data from Access to csv file

I am trying to insert data from access database to .csv file in C#. I have 54744 records in Access database. Inserting the records in CSV file is taking more than 2 hours. Below is my code:
public void CreateCSVFile()
{
DataSet dsRecLoad = new DataSet();
DataTable dt = new DataTable("RecsCDDt");
StringBuilder sb = new StringBuilder();
using (OleDbConnection connection = new OleDbConnection(_configuration.GetConnectionString("AccessConnection")))
{
using (OleDbCommand cmd = new OleDbCommand("select * from RecsCD", connection))
{
connection.Open();
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);
dt.Load(reader);
}
}
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
if (!String.IsNullOrEmpty(sb.ToString()))
sb.Append(",");
sb.Append(row[dc.ColumnName].ToString());
}
sb.Append("\n");
}
string CSVfilePath = _configuration.GetValue<string>("MySettings:CSVFile");
if (!File.Exists(CSVfilePath))
{
File.WriteAllText(CSVfilePath, sb.ToString()) ;
}
}
Can anyone suggest me a more efficient way to insert the records in .CSV file.
Thank You.
Here's a small example of how you might achieve the same thing by streaming the data using a reader and writing to the file as a stream (untested):
await using var textWriter = new StreamWriter("path\\to\\file.csv");
await using var writer = new CsvHelper.CsvWriter(textWriter, CultureInfo.InvariantCulture);
await using var connection = new SqlConnection("connection_string");
await using var cmd = new SqlCommand("select * from RecsCD", connection);
await connection.OpenAsync();
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync()) {
foreach (int i in Enumerable.Range(0, reader.FieldCount)) {
writer.WriteField(await reader.GetFieldValueAsync<object>(i));
}
await writer.NextRecordAsync();
}
await writer.FlushAsync();
Note that the code depends on the CsvHelper library.

Automapper to read single record

I have been trying to read single Record using AutoMapper. But I couldn't get through using the below code,
public ChartOfAccount GetSingleCOA( )
{
string queryString = "select * from ChartofAccounts where AccNo=423";
using (var connection = new SqlConnection(conStr))
using (var command = new SqlCommand(queryString, connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
if (reader.HasRows)
{
reader.Read();
return Mapper.Map<IDataRecord, ChartOfAccount>(reader);
}
}
return null;
}
simply changing the line to
return Mapper.DynamicMap<IDataReader, ChartOfAccount>(reader);
solved the issue

How can I populate a list with values from a SQL Server database?

The list will grow and shrink depending on how many items I have in my database.
I need to populate a list not a listbox. I understand I will need to open a connection.
using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
{
using (var cmd = conn.CreateCommand())
{
conn.Open();
List<string> TagList = new List<string>();
for (int i = 0; i < TagList.Count; i++)
TagList[i].Add("Data from database");
cmd.ExecuteNonQuery();
}
}
I'm really not sure how to do this and I'm sure my method up here looks very wrong so I really need help.
Could someone show me what I'm doing wrong?
public IEnumerable<string> GetTagList()
{
using (var connection = new SqlConnection(Properties.Settings.Default.DBConnectionString))
using (var cmd = connection.CreateCommand())
{
connection.Open();
cmd.CommandText = "select Tag from TagsTable"; // update select command accordingly
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
yield return reader.GetString(reader.GetOrdinal("Tag"));
}
}
}
}
then you can call it as below
List<string> tags = GetTagList().ToList();
I would like to share my solution, hope helps someone in the future:
public List<string> getFromDataBase()
{
List<string> result = new List<string>();
using(SqlConnection con = new SqlConnection("connectionString"))
{
con.Open();
DataTable tap = new DataTable();
new SqlDataAdapter(query, con).Fill(tap);
result = tap.Rows.OfType<DataRow>().Select(dr => dr.Field<string>("columnName")).ToList();
}
return result;
}
This would do as it is (if I didn't do any typos...)
private void LoadList()
{
List<string> tagsList = new List<string>();
using (IDbConnection connection = new SqlConnection(Properties.Settings.Default.DBConnectionString))
{
connection.Open();
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT TAGCOLUMN FROM TAGSTABLE";
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (!reader.IsDBNull(0))
tagsList.Add(reader.GetString(0));
}
reader.Close();
}
}
connection.Close();
}
}
EDIT:
Of course you have to change the select statement to the correct one from your database.
I just used a pseudo one to show you what to put there.

Get database list in Npgsql

How can I get a list of all available PostgreSQL databases on a user's computer using Npgsql in C#?
In postgreSQL there are two way to do it :
\l
SELECT datname FROM pg_database;
Using Npgsql You can use such code snippet:
IEnumerable<string> GetDatabaseNames()
{
var connStrBuilder = new NpgsqlConnectionStringBuilder();
connStrBuilder.Host = "localhost";
connStrBuilder.Username = "theduck";
connStrBuilder.Password = "password123";
connStrBuilder.Database = "postgres"; // this database is always present
using (var conn = new NpgsqlConnection(connStrBuilder.ConnectionString))
{
conn.Open();
using (var command = new NpgsqlCommand("SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname;", conn))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return reader.GetString(0);
}
}
}
}

Categories