Wanting to update database as I read from it calculation - c#

I am trying to run a calculation for a banking MS now I want to read accrued interest from a database and then update the value before conducting the next read I have currently tried this within a while loop in which the command text cannot be changed whilst the reader is open thus wont work, any ideas/solutions.
I have thought about using an array to hold the data but am unsure how to filter this out after the read
UPDATE: after introducing a cache in the form of an array I am trying to execute the update and it says that no such column product.prodid exists although this is used to read from so does exist
UPDATE 2: The code now updates to the database but incorrectly, for some reason it stores the same value in the Accrued column instead of each individual value, I can localise this error to either of the for loops, I think one solution could be to used J as a parameter and make that equal to the account ID ensuring that each row is updated then the next, could this work?
private void Btn_AccInt_Click(object sender, EventArgs e)
{
connection.Open();
command = connection.CreateCommand();
int max = 0;
double IntAccint = 0.0;
command.CommandText = "SELECT balance, accrued, intrate FROM account, product WHERE account.prodid = product.prodid;";
reader = command.ExecuteReader();
double[] storedrate = new double[100];
while (reader.Read())
{
string AccIntrest, AccBal, Intrate;
AccBal = reader[0].ToString();
AccIntrest = reader[1].ToString();
Intrate = reader[2].ToString();
double IntAccbal, IntIntrat;
IntAccbal = double.Parse(AccBal);
IntAccint = double.Parse(AccIntrest);
IntIntrat = double.Parse(Intrate);
IntAccint = IntAccint + ((IntAccbal * IntIntrat) / 365);
for (int i = 0; i < reader.StepCount; i++)
{
storedrate[i] = IntAccint;
max = reader.StepCount;
}
}
reader.Close();
Update = connection.CreateCommand();
for (int j = 0; j < max; j++ )
{
Update.CommandText = "UPDATE account SET accrued = #Accured";
IntAccint = storedrate[j];
Update.Parameters.AddWithValue("Accured", IntAccint);
Update.ExecuteNonQuery();
}
}

Related

Create generic SQL INSERT statement from datatable with looping

I'm trying to create a generic method of taking a datatable and creating a SQL INSERT statement from its contents.
I've spent the better part of a couple of days researching this on StackOverflow.
I just need some help with the looping section code--it doesn't give the desired results and I've reached the limit of my coding skills.
I don't need BulkCopy solutions or database specific ones. I just need the INSERT string. I don't care about SQL injections or anything else. This is an internal legacy app that needs to have a few repetitive/manual processes automated.
I have written an example console program to demonstrate. The size of the datatable isn't really a factor form me. It is never over 60 rows and has 6 columns:
The datatable should look like:
ID Group X Y Z
1 A 100 200 400
2 B 200 400 800
3 C 300 600 1200
4 D 400 800 1600
5 E 500 1000 2000
The example console app is:
using System;
using System.Data;
namespace crSQLInsert
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("ID");
dt.Columns.Add("Group");
dt.Columns.Add("X");
dt.Columns.Add("Y");
dt.Columns.Add("Z");
for (int i = 0; i < 5; i++)
{
Random rnd = new Random();
char cLtr = Convert.ToChar(i+65);
double colVal = ((i + 1) * 100) ;
DataRow dr = dt.NewRow();
dr["ID"] = 1 + i;
dr["Group"] = cLtr;
dr["X"] = colVal ;
dr["Y"] = colVal * 2;
dr["Z"] = colVal * 4;
dt.Rows.Add(dr);
}
createSQL(dt);
}
public static void createSQL(DataTable dtSQL)
{
// Create generic SQL query to add datatable contents to my_table
string sSQL = "";
sSQL += "INSERT INTO my_table (ID, Group, X, Y, Z) VALUES (";
for (int i = 0; i < dtSQL.Rows.Count; i++)
{
for (int j = 0; j < dtSQL.Columns.Count; j++)
{
sSQL += dtSQL.Rows[i][j].ToString().Trim();
if (j != dtSQL.Columns.Count - 1)
{
sSQL += "','";
}
else
{
sSQL += "')";
}
}
}
Console.WriteLine(sSQL);
}
}
}
The resulting SQL string should be like below:
(I've formatted it so that it's easier to read--the actual string doesn't necessarily need the \r\n as it will end up as a string in an ODBCCommand().
INSERT INTO my_table (ID, Group, X, Y, Z)
VALUES (1','A','100','200','400'),
(2','B','200','400','800'),
(3','C','300','600','1200'),
(4','D','400','800','1600'),
(5','E','500','1000','2000');
Thanks for any help.

Adding values to a list box from array WITH text

I'm adding values from a file to an array and then adding those values to a list box in a form. I have methods performing calculations and everything is working great, but I'd like to be able to use a loop and insert the year prior to the values I am inserting into the list box. Is there a way to use the .Add method and include a variable which will be changing in this? Something like populationListbox.Items.Add(i, value); if i is my loop counter? Code is below so I'd like first line in the list box to have the year I specify with my counter, followed by the population. Like this, "1950 - 151868". As of now it only displays the value 151868. Thanks!
const int SIZE = 41;
int[] pops = new int[SIZE];
int index = 0;
int greatestChange;
int leastChange;
int greatestYear;
int leastYear;
double averageChange;
StreamReader inputFile;
inputFile = File.OpenText("USPopulation.txt");
while (!inputFile.EndOfStream && index < pops.Length)
{
pops[index] = int.Parse(inputFile.ReadLine());
index++;
}
inputFile.Close();
foreach (int value in pops)
{
**populationListbox.Items.Add(value);**
}
greatestChange = greatestIncrease(pops) * 1000;
leastChange = leastIncrease(pops) * 1000;
averageChange = averageIncrease(pops) * 1000;
greatestYear = greatestIncreaseyear(pops);
leastYear = leastIncreaseyear(pops);
greatestIncreaselabel.Text = greatestChange.ToString("N0");
leastIncreaselabel.Text = leastChange.ToString("N0");
averageChangelabel.Text = averageChange.ToString("N0");
greatestIncreaseyearlabel.Text = greatestYear.ToString();
leastIncreaseyearlabel.Text = leastYear.ToString();
Like this?
int i = 1950;
foreach (int value in pops)
{
populationListbox.Items.Add(i.ToString() + " - " + value);
i++;
}
Your life will be a lot easier if you stop trying to program C# as if it were 1980s C and use the power of it's Framework:
var pops = File.ReadLines("USPopulation.txt").Select(int.Parse);
populationListbox.Items.AddRange(pops.Select((p,i) => $"{i} - {p}"));

Unable to retrieve more than 10k records from Google Analytics

I've developed a windows console application which extracts Google Analytics data and writes to .CSV file. When queried for data of particular date on Google Analytics Query Explorer, it displayed:- "Your query matched 96782 results...".
The problem is when when i query for the data of same date using application, by default it returns only 1000 records and when I set
DataResource.GaResource.GetRequest objRequest.MaxResult more than 10k, then it gives me max to max 10k records.
Is there any way to get records more than 10k.
So what i did is, I set Request.MaxResult = 10000 and Response.ItemsPerPage = 10000. The vital role is played by Request.StartIndex which has pagination mechanism e.g. in 1st loop we'll get 1-10000 rows then 10001-20000....20001-30000 and so on.
Under loop, request to data of a particular day initially is set to Request.StartIndex = 1 and then consecutively increases to MaxResult + 1 as soon as row count reaches 10000th row e.g.:-
Request.StartIndex = 1 then 10001 then 20001....30001...40001 and so on untill Response.Rows == null.
Here's my code:-
StringBuilder sbrCSVData = new StringBuilder();
int intStartIndex = 1;
int intIndexCnt = 0;
int intMaxRecords = 10000;
AnalyticsService objAnaSrv = new AnalyticsService(objInit);
string metrics = "ga:visitors,ga:visits,ga:visitBounceRate,ga:pageviews,ga:pageviewsPerVisit,ga:uniquePageviews,ga:avgTimeOnPage,ga:exits,ga:avgPageLoadTime,ga:goal1ConversionRate";
DataResource.GaResource.GetRequest objRequest = objAnaSrv.Data.Ga.Get(strProfId, startDate,endDate, metrics);
objRequest.Dimensions = "ga:visitCount,ga:browser,ga:browserVersion,ga:IsMobile,ga:screenResolution,ga:date,ga:hour";
objRequest.MaxResults = 10000;
while (true)
{
objRequest.StartIndex = intStartIndex;
GaData objResponse = objRequest.Fetch();
objResponse.ItemsPerPage = intMaxRecords;
if (objResponse.Rows != null)
{
intIndexCnt++;
for (int intRows = 0; intRows < objResponse.Rows.Count; intRows++)
{
IList<string> lstRow = objResponse.Rows[intRows];
for (int intCols = 0; intCols <= lstRow.Count - 1; intCols++)
{
strRowData += lstRow[intCols].ToString() + "|";
}
strRowData = strRowData.Remove(strRowData.Length - 1, 1);
sbrCSVData.Append(strRowData + "\r\n");
strRowData = "";
}
intStartIndex = intIndexCnt * intMaxRecords + 1;
}
else break;
}
Yes this thing is mentioned in there documentation.
To get more data, general practice is break it down your interval or any criteria then do multiple queries to fetch the data.
Read https://developers.google.com/analytics/devguides/reporting/core/v3/limits-quotas for full detail
Hope it might help you a bit

Writing large number of records (bulk insert) to Access in .NET/C#

What is the best way to perform bulk inserts into an MS Access database from .NET? Using ADO.NET, it is taking way over an hour to write out a large dataset.
Note that my original post, before I "refactored" it, had both the question and answer in the question part. I took Igor Turman's suggestion and re-wrote it in two parts - the question above and followed by my answer.
I found that using DAO in a specific manner is roughly 30 times faster than using ADO.NET. I am sharing the code and results in this answer. As background, in the below, the test is to write out 100 000 records of a table with 20 columns.
A summary of the technique and times - from best to worse:
02.8 seconds: Use DAO, use DAO.Field's to refer to the table columns
02.8 seconds: Write out to a text file, use Automation to import the text into Access
11.0 seconds: Use DAO, use the column index to refer to the table columns.
17.0 seconds: Use DAO, refer to the column by name
79.0 seconds: Use ADO.NET, generate INSERT statements for each row
86.0 seconds: Use ADO.NET, use DataTable to an DataAdapter for "batch" insert
As background, occasionally I need to perform analysis of reasonably large amounts of data, and I find that Access is the best platform. The analysis involves many queries, and often a lot of VBA code.
For various reasons, I wanted to use C# instead of VBA. The typical way is to use OleDB to connect to Access. I used an OleDbDataReader to grab millions of records, and it worked quite well. But when outputting results to a table, it took a long, long time. Over an hour.
First, let's discuss the two typical ways to write records to Access from C#. Both ways involve OleDB and ADO.NET. The first is to generate INSERT statements one at time, and execute them, taking 79 seconds for the 100 000 records. The code is:
public static double TestADONET_Insert_TransferToAccess()
{
StringBuilder names = new StringBuilder();
for (int k = 0; k < 20; k++)
{
string fieldName = "Field" + (k + 1).ToString();
if (k > 0)
{
names.Append(",");
}
names.Append(fieldName);
}
DateTime start = DateTime.Now;
using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM TEMP";
int numRowsDeleted = cmd.ExecuteNonQuery();
Console.WriteLine("Deleted {0} rows from TEMP", numRowsDeleted);
for (int i = 0; i < 100000; i++)
{
StringBuilder insertSQL = new StringBuilder("INSERT INTO TEMP (")
.Append(names)
.Append(") VALUES (");
for (int k = 0; k < 19; k++)
{
insertSQL.Append(i + k).Append(",");
}
insertSQL.Append(i + 19).Append(")");
cmd.CommandText = insertSQL.ToString();
cmd.ExecuteNonQuery();
}
cmd.Dispose();
}
double elapsedTimeInSeconds = DateTime.Now.Subtract(start).TotalSeconds;
Console.WriteLine("Append took {0} seconds", elapsedTimeInSeconds);
return elapsedTimeInSeconds;
}
Note that I found no method in Access that allows a bulk insert.
I had then thought that maybe using a data table with a data adapter would be prove useful. Especially since I thought that I could do batch inserts using the UpdateBatchSize property of a data adapter. However, apparently only SQL Server and Oracle support that, and Access does not. And it took the longest time of 86 seconds. The code I used was:
public static double TestADONET_DataTable_TransferToAccess()
{
StringBuilder names = new StringBuilder();
StringBuilder values = new StringBuilder();
DataTable dt = new DataTable("TEMP");
for (int k = 0; k < 20; k++)
{
string fieldName = "Field" + (k + 1).ToString();
dt.Columns.Add(fieldName, typeof(int));
if (k > 0)
{
names.Append(",");
values.Append(",");
}
names.Append(fieldName);
values.Append("#" + fieldName);
}
DateTime start = DateTime.Now;
OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB);
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM TEMP";
int numRowsDeleted = cmd.ExecuteNonQuery();
Console.WriteLine("Deleted {0} rows from TEMP", numRowsDeleted);
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM TEMP", conn);
da.InsertCommand = new OleDbCommand("INSERT INTO TEMP (" + names.ToString() + ") VALUES (" + values.ToString() + ")");
for (int k = 0; k < 20; k++)
{
string fieldName = "Field" + (k + 1).ToString();
da.InsertCommand.Parameters.Add("#" + fieldName, OleDbType.Integer, 4, fieldName);
}
da.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
da.InsertCommand.Connection = conn;
//da.UpdateBatchSize = 0;
for (int i = 0; i < 100000; i++)
{
DataRow dr = dt.NewRow();
for (int k = 0; k < 20; k++)
{
dr["Field" + (k + 1).ToString()] = i + k;
}
dt.Rows.Add(dr);
}
da.Update(dt);
conn.Close();
double elapsedTimeInSeconds = DateTime.Now.Subtract(start).TotalSeconds;
Console.WriteLine("Append took {0} seconds", elapsedTimeInSeconds);
return elapsedTimeInSeconds;
}
Then I tried non-standard ways. First, I wrote out to a text file, and then used Automation to import that in. This was fast - 2.8 seconds - and tied for first place. But I consider this fragile for a number of reasons: Outputing date fields is tricky. I had to format them specially (someDate.ToString("yyyy-MM-dd HH:mm")), and then set up a special "import specification" that codes in this format. The import specification also had to have the "quote" delimiter set right. In the example below, with only integer fields, there was no need for an import specification.
Text files are also fragile for "internationalization" where there is a use of comma's for decimal separators, different date formats, possible the use of unicode.
Notice that the first record contains the field names so that the column order isn't dependent on the table, and that we used Automation to do the actual import of the text file.
public static double TestTextTransferToAccess()
{
StringBuilder names = new StringBuilder();
for (int k = 0; k < 20; k++)
{
string fieldName = "Field" + (k + 1).ToString();
if (k > 0)
{
names.Append(",");
}
names.Append(fieldName);
}
DateTime start = DateTime.Now;
StreamWriter sw = new StreamWriter(Properties.Settings.Default.TEMPPathLocation);
sw.WriteLine(names);
for (int i = 0; i < 100000; i++)
{
for (int k = 0; k < 19; k++)
{
sw.Write(i + k);
sw.Write(",");
}
sw.WriteLine(i + 19);
}
sw.Close();
ACCESS.Application accApplication = new ACCESS.Application();
string databaseName = Properties.Settings.Default.AccessDB
.Split(new char[] { ';' }).First(s => s.StartsWith("Data Source=")).Substring(12);
accApplication.OpenCurrentDatabase(databaseName, false, "");
accApplication.DoCmd.RunSQL("DELETE FROM TEMP");
accApplication.DoCmd.TransferText(TransferType: ACCESS.AcTextTransferType.acImportDelim,
TableName: "TEMP",
FileName: Properties.Settings.Default.TEMPPathLocation,
HasFieldNames: true);
accApplication.CloseCurrentDatabase();
accApplication.Quit();
accApplication = null;
double elapsedTimeInSeconds = DateTime.Now.Subtract(start).TotalSeconds;
Console.WriteLine("Append took {0} seconds", elapsedTimeInSeconds);
return elapsedTimeInSeconds;
}
Finally, I tried DAO. Lots of sites out there give huge warnings about using DAO. However, it turns out that it is simply the best way to interact between Access and .NET, especially when you need to write out large number of records. Also, it gives access to all the properties of a table. I read somewhere that it's easiest to program transactions using DAO instead of ADO.NET.
Notice that there are several lines of code that are commented. They will be explained soon.
public static double TestDAOTransferToAccess()
{
string databaseName = Properties.Settings.Default.AccessDB
.Split(new char[] { ';' }).First(s => s.StartsWith("Data Source=")).Substring(12);
DateTime start = DateTime.Now;
DAO.DBEngine dbEngine = new DAO.DBEngine();
DAO.Database db = dbEngine.OpenDatabase(databaseName);
db.Execute("DELETE FROM TEMP");
DAO.Recordset rs = db.OpenRecordset("TEMP");
DAO.Field[] myFields = new DAO.Field[20];
for (int k = 0; k < 20; k++) myFields[k] = rs.Fields["Field" + (k + 1).ToString()];
//dbEngine.BeginTrans();
for (int i = 0; i < 100000; i++)
{
rs.AddNew();
for (int k = 0; k < 20; k++)
{
//rs.Fields[k].Value = i + k;
myFields[k].Value = i + k;
//rs.Fields["Field" + (k + 1).ToString()].Value = i + k;
}
rs.Update();
//if (0 == i % 5000)
//{
//dbEngine.CommitTrans();
//dbEngine.BeginTrans();
//}
}
//dbEngine.CommitTrans();
rs.Close();
db.Close();
double elapsedTimeInSeconds = DateTime.Now.Subtract(start).TotalSeconds;
Console.WriteLine("Append took {0} seconds", elapsedTimeInSeconds);
return elapsedTimeInSeconds;
}
In this code, we created DAO.Field variables for each column (myFields[k]) and then used them. It took 2.8 seconds. Alternatively, one could directly access those fields as found in the commented line rs.Fields["Field" + (k + 1).ToString()].Value = i + k; which increased the time to 17 seconds. Wrapping the code in a transaction (see the commented lines) dropped that to 14 seconds. Using an integer index rs.Fields[k].Value = i + k; droppped that to 11 seconds. Using the DAO.Field (myFields[k]) and a transaction actually took longer, increasing the time to 3.1 seconds.
Lastly, for completeness, all of this code was in a simple static class, and the using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACCESS = Microsoft.Office.Interop.Access; // USED ONLY FOR THE TEXT FILE METHOD
using DAO = Microsoft.Office.Interop.Access.Dao; // USED ONLY FOR THE DAO METHOD
using System.Data; // USED ONLY FOR THE ADO.NET/DataTable METHOD
using System.Data.OleDb; // USED FOR BOTH ADO.NET METHODS
using System.IO; // USED ONLY FOR THE TEXT FILE METHOD
Thanks Marc, in order to vote you I created an account on StackOverFlow...
Below is the reusable method [Tested on C# with 64 Bit - Win 7, Windows 2008 R2, Vista, XP platforms]
Performance Details:
Exports 120,000 Rows in 4 seconds.
Copy the below code and pass the parameters... and see the performance.
Just pass your datatable with the same schema, as of target Access Db Table.
DBPath= Full path of access Db
TableNm = Name of Target Access Db table.
The code:
public void BulkExportToAccess(DataTable dtOutData, String DBPath, String TableNm)
{
DAO.DBEngine dbEngine = new DAO.DBEngine();
Boolean CheckFl = false;
try
{
DAO.Database db = dbEngine.OpenDatabase(DBPath);
DAO.Recordset AccesssRecordset = db.OpenRecordset(TableNm);
DAO.Field[] AccesssFields = new DAO.Field[dtOutData.Columns.Count];
//Loop on each row of dtOutData
for (Int32 rowCounter = 0; rowCounter < dtOutData.Rows.Count; rowCounter++)
{
AccesssRecordset.AddNew();
//Loop on column
for (Int32 colCounter = 0; colCounter < dtOutData.Columns.Count; colCounter++)
{
// for the first time... setup the field name.
if (!CheckFl)
AccesssFields[colCounter] = AccesssRecordset.Fields[dtOutData.Columns[colCounter].ColumnName];
AccesssFields[colCounter].Value = dtOutData.Rows[rowCounter][colCounter];
}
AccesssRecordset.Update();
CheckFl = true;
}
AccesssRecordset.Close();
db.Close();
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine);
dbEngine = null;
}
}
You can use a KORM, object relation mapper that allows bulk operations over MsAccess.
database
.Query<Movie>()
.AsDbSet()
.BulkInsert(_data);
or if you have source reader, you can directly use MsAccessBulkInsert class:
using (var bulkInsert = new MsAccessBulkInsert("connection string"))
{
bulkInsert.Insert(sourceReader);
}
KORM is available from nuget Kros.KORM.MsAccess and it's opensource on GitHub
Thanks Marc for the examples.
On my system the performance of DAO is not as good as suggested here:
TestADONET_Insert_TransferToAccess(): 68 seconds
TestDAOTransferToAccess(): 29 seconds
Since on my system the use of Office interop libraries is not an option I tried a new method involving the writing of a CSV file and then importing it via ADO:
public static double TestADONET_Insert_FromCsv()
{
StringBuilder names = new StringBuilder();
for (int k = 0; k < 20; k++)
{
string fieldName = "Field" + (k + 1).ToString();
if (k > 0)
{
names.Append(",");
}
names.Append(fieldName);
}
DateTime start = DateTime.Now;
StreamWriter sw = new StreamWriter("tmpdata.csv");
sw.WriteLine(names);
for (int i = 0; i < 100000; i++)
{
for (int k = 0; k < 19; k++)
{
sw.Write(i + k);
sw.Write(",");
}
sw.WriteLine(i + 19);
}
sw.Close();
using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM TEMP";
int numRowsDeleted = cmd.ExecuteNonQuery();
Console.WriteLine("Deleted {0} rows from TEMP", numRowsDeleted);
StringBuilder insertSQL = new StringBuilder("INSERT INTO TEMP (")
.Append(names)
.Append(") SELECT ")
.Append(names)
.Append(#" FROM [Text;Database=.;HDR=yes].[tmpdata.csv]");
cmd.CommandText = insertSQL.ToString();
cmd.ExecuteNonQuery();
cmd.Dispose();
}
double elapsedTimeInSeconds = DateTime.Now.Subtract(start).TotalSeconds;
Console.WriteLine("Append took {0} seconds", elapsedTimeInSeconds);
return elapsedTimeInSeconds;
}
Performace analysis of TestADONET_Insert_FromCsv(): 1.9 seconds
Similar to Marc's example TestTextTransferToAccess(), this method is also fragile for a number of reasons regarding the use of CSV files.
Hope this helps.
Lorenzo
First make sure that the access table columns have the same column names and similar types. Then you can use this function which I believe is very fast and elegant.
public void AccessBulkCopy(DataTable table)
{
foreach (DataRow r in table.Rows)
r.SetAdded();
var myAdapter = new OleDbDataAdapter("SELECT * FROM " + table.TableName, _myAccessConn);
var cbr = new OleDbCommandBuilder(myAdapter);
cbr.QuotePrefix = "[";
cbr.QuoteSuffix = "]";
cbr.GetInsertCommand(true);
myAdapter.Update(table);
}
Another method to consider, involving linking tables via DAO or ADOX then executing statements like this:
SELECT * INTO Table1 FROM _LINKED_Table1
Please see my full answer here:
MS Access Batch Update via ADO.Net and COM Interoperability
To add to Marc's answer:
Note that having the [STAThread] attribute above your Main method. will make your program easily able to communicate with COM objects, increasing the speed further. I know it's not for every application but if you heavily depend on DAO, I would recommend it.
Further more, using the DAO insertion method. If you have a column that is not required and you want to insert null, don't even set it's value. Setting the value cost time even if it's null.
Note the position of the DAO component here. This helps explain the efficiency improvements.

Prepared Statements - Iterative query

I have to upgrade the following code to use prepared statements:
OdbcCommand cmd = sql.CreateCommand();
cmd.CommandText = "SELECT [EMail] from myTable WHERE "+,
for (int i = 0; i < 50; i++)
{
if (i > 0)
{
cmd.CommandText += " OR ";
}
cmd.CommandText += "UNIQUE_ID = " + lUniqueIDS[i];
}
Forbid my stupid code above, it's just an example... I'm trying to fetch all the Emails of users with IDs either x, y, z, etc...
The question is - how can I rewrite it using prepared statements?
A blind naive guess would be
for (int i = 0; i < 50; i++)
{
if (i > 0)
{
cmd.CommandText += " OR ";
}
cmd.CommandText += "UNIQUE_ID = ?";
cmd.Parameters.Add("#UNIQUE_ID", OdbcType.BigInt).Value = lUniqueIDS[i];
}
Should it work? Can I append the same parameter (unique_id) more than once?
It looks like you're using positional parameters (i.e. ? in the query, rather than #UNIQUE_ID) which means the names of the parameters shouldn't matter as far as the SQL is concerned. However, I wouldn't be entirely surprised to see the provider complain... and it may make diagnostics harder too. I suggest you use the index as a suffix:
cmd.Parameters.Add("#UNIQUE_ID" + i, ObdcType.BigInt).Value = lUniqueIDs[i];

Categories