Datareader contains seemingly random amount of rows, despite of Select statement criteria - c#

I have searched far and wide, but most Datareader problem/answer pairs concern getting past the first row, not returning anything, getting single values using datareader, etc.. Nothing quite like waht I'm encountering at the moment.
To be clear, this is an assignment for my evening class, albeit just a very small part of it.
the function takes size as int; the table has two colmuns: col1 and col2 of which col1 holds the index value as double and col2 holds a randomly generated double. table is an excel worksheet in a workbook, don't know if that's relevant.
table has been populated using an insert statement carried out by an ADO command object without problems.
Now, instead of supplying me with the amount of rows as specified by size/#size in the query (as it plays the double role of index/UID in this case), the datareader object fetches seemingly random amounts of rows. I say seemingly, because the number does seem to be fixed to the value of "size" (eg. size = 10 -> datareader contains 3 rows after .executeReader(); size = 2 -> datareader contains 113 rows; size = 5 -> 446 rows).
While debugging I kept track of #size parameter for the query remains 10.0
and I can't put my finger on when/why reader.Read() turns False.
I also substituted the parameter in the query string with a literal (5.0); which resulted in a type mismatch in criteria expression exception. But it's all Doubles, or am I missing something?! I'm guessing this is going to be the kicker somehow, but I'm at a loss right now.
As you can probably guess I'm pretty new at programming, so bear with me please.
What causes my code to behave the way it does?
private Double[] getSelection(int size, string table)
{
List<Double> list = new List<Double>();
Double[] toSort;
OleDbConnection connect = new OleDbConnection(cntstring);
connect.Open();
OleDbCommand command = connect.CreateCommand();
command.CommandType = CommandType.Text;
command.Parameters.Add("#size", OleDbType.Double).Value = Convert.ToDouble(size);
command.CommandText = String.Format("SELECT * FROM [{0}$] WHERE col1 < #size;", table);
try
{
OleDbDataReader reader = command.ExecuteReader();
Double outputReader;
while (reader.Read())
{
outputReader = Convert.ToDouble(reader.GetValue(1)); /for some reason (which is not my main concern at the moment) the reader.getDouble() method returned an invalid cast exception
list.Add(outputReader);
}
toSort = new double[list.Count()];
foreach (double d in list)
{
toSort[list.IndexOf(d)] = d;
}
string output = String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", toSort[0], toSort[1], toSort[2], toSort[3], toSort[4], toSort[5], toSort[6], toSort[7], toSort[8], toSort[9]);
//to check for values; the String.Format is where i first encountered the index out of bounds exception
MessageBox.Show(output);
reader.Close();
reader.Dispose();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
connect.Close();
connect.Dispose();
return toSort;
}
}

Did you try single quotes around #size in your select statement, i.e.
'#size'

Related

How to get column value from Ado.Net reader

I am trying to get value of a given table column from Ado.Net as follows
var query = "select transmission_time, actual_transaction_amount as sum_amount FROM table_name";
using (NpgsqlConnection conn = new NpgsqlConnection("connectionString"))
{
conn.Open();
NpgsqlCommand command = new NpgsqlCommand(query, conn);
using (NpgsqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var amount = reader.GetDecimal(Convert.ToInt32(reader["sum_amount"]));
result.Add(amount.ToString());//= amount;
}
}
But I get an error
System.OverflowException: Value was either too large or too small for an Int32.
Blockquote
But when I change the line to this
var amount = reader.GetDecimal(Convert.ToInt64(reader["sum_amount"]));
I get the following error
Argument 1 cannot convert from 'long' to 'int'
Please I need to convert the figures in that column to numbers and return it in my result.
The values in the column I am trying to get are like these examples
10000, 5000, 20000, etc. in a postgreSql table
The application is a Asp.Net 6 application running on Windows
Any help will be appreciated.
Thak you
This line doesn't make sense:
var amount = reader.GetDecimal(Convert.ToInt32(reader["sum_amount"]));
I assume that what you're actually trying to do there is get an int value from the sum_amount column and convert it to a decimal. What that code actually does is get a boxed int (i.e. an object reference) from the sum_amount column, unbox it and then use that int value as an index to get a decimal from a different column that probably doesn't exist. I think that what you actually meant to do was this:
var amount = Convert.ToDecimal(reader.GetInt64("sum_amount"));

Getting Database values into variable form c#

I am developing a cricket simulation and i need to retrieve certain statistics from a players data. I've got the following code.
public List<float> BattingData()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
string query = "SELECT [INNS], [NOT OUTS], [AVG] FROM [" + batTeam + "] WHERE [Player] = '" + name + "';";
SqlCommand com = new SqlCommand(query, con);
con.Open();
using (SqlDataReader reader = com.ExecuteReader())
{
if(reader.HasRows)
{
while (reader.NextResult())
{
Innings = Convert.ToInt32(reader["INNS"]);
NotOuts = Convert.ToInt32(reader["NOT OUTS"]);
Avg = Convert.ToSingle(reader["AVG"]);
}
}
}
con.Close();
OutRatePG = (Innings = NotOuts) / Innings;
OutRatePB = OutRatePG / 240;
RunsPB = Avg / 240;
battingData.Add(OutRatePB);
battingData.Add(RunsPB);
return battingData;
}
The error that I am getting is that when I try to divie by 'Innings' it is saying cannot divide by zero, so I think the variables are being returned as zero and no data is being assigned to them.
This line is the issue:
while (reader.NextResult())
What this does is move the reader to the next resultset, ignoring the rest of the rows unread. To advance a reader to the next row, you need to call reader.Read() instead.
You have some other issues with your code:
You appear to have a separate table for each team. This is incorrect database design. You should create a Team table, with each team in it, and then foreign key your TeamResults table to it. Query it using INNER JOIN.
You are concatenating user-entered values to your query. This leaves you open to SQL injection attacks. Use parameters instead. (You cannot parameterize a table name, another reason you should do as above 1.)
You do not need to check for HasRows. If there are no rows, Read() will return false.
It looks like you only want one row. If that is the case you don't want a while(reader.Read()) loop, instead if(reader.Read()). (If you only need a single value, you can refactor the code to use command.ExecuteScalar().)
In database records check if value for Innings has 0
also you can try the below code before performing any operation.
> if(Innings>0) { OutRatePG = (Innings - NotOuts) / Innings; }

SqlDataReader.Read() always false in C#

I have a Table in my local database Ships(HistID,ShipName,ShipLength). I am polling the database for all ships with HistID == theme, but while(reader.Read()){} is never entered. Also, my Ships table has more than one row (saw this problem in another SO question) so I'm not sure why I cant store the results into List of Tuples. Executing the query alone in Visual Studio 2015 yields the correct results.
public List<Tuple<String, int>> getHistoricalShipList(int theme)
{
List<Tuple<String, int>> list = new List<Tuple<string, int>>();
using (db)
{
cmd = new SqlCommand(#"Select ShipName, ShipLength from Ships Where HistID=#theme", db);
cmd.Parameters.AddWithValue("#theme", theme);
db.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows) // always returning false
{
//Loop through results
while (reader.Read())
{
String shipName = reader[0].ToString();
int shipLength = Convert.ToInt32(reader[1]);
list.Add(Tuple.Create(shipName, shipLength));
}
}
db.Close();
}
return list;
}
EDIT:: removed the single quotes from the query as suggested, but still having the same issue.
Your theme is of type int, and you are enclosing it in single quotes like it is a string value. Remove the quotes, but more importantly, use Parameters
cmd = new SqlCommand(string.Format(#"Select ShipName, ShipLength from Ships Where HistID={0}", theme), db);
Never use string concatenation/string format to build SQL statements, your code is prone to SQL injection.
cmd = new SqlCommand(#"Select ShipName, ShipLength from Ships Where HistID=#theme", db);
cmd.Parameters.AddWithValue("#theme", theme);
//Or more precise
//cmd.Parameters.Add(new SqlParameter("#theme", SqlDbType.Int) {Value = theme});
The reason you are not getting any rows back is, that your field HistID is of numeric type and you are trying to compare it with a string value (by enclosing the value in single quote).
Remove HasRows check and just use the .Read while loop; there's various bug reports on HasRows not being entirely accurate in some cases.
Other than that, it's likely you're making a mistake somewhere. Either theme isn't what you expect it to be, or some environment error like hitting the wrong database.

Checking and Saving/Loading from MySQL C#

I am making something that requires MySQL. I have the saving done from in-game, which is simply done by INSERT.
I have a column that will have a password in and I need to check if the inputted password matched any of the rows and then if it is, get all of the contents of the row then save it to variables.
Does anyone have an idea how to do this in C#?
//////////////////////////
I have found how to save and get the string, however it will only get 1 string at a time :(
MySql.Data.MySqlClient.MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM (player) WHERE (pass)";
command.ExecuteNonQuery();
command.CommandType = System.Data.CommandType.Text;
MySql.Data.MySqlClient.MySqlDataReader reader = command.ExecuteReader();
reader.Read();
ayy = reader.GetString(1);
print (ayy);
if(ayy == password){
//something
}
My best practice is to use MySQLDataAdapter to fill a DataTable. You can then iterate through the rows and try to match the password.
Something like this;
DataTable dt = new DataTable();
using(MySQLDataAdapter adapter = new MySQLDataAdaper(query, connection))
{
adapter.Fill(dt);
}
foreach(DataRow row in dt.Rows)
{
//Supposing you stored your password in a stringfield in your database
if((row.Field<String>("columnName").Equals("password"))
{
//Do something with it
}
}
I hope this compiles since I typed this from my phone. You can find a nice explanation and example here.
However, if you are needing data from a specific user, why not specificly ask it from the database? Your query would be like;
SELECT * FROM usercolumn WHERE user_id = input_id AND pass = input_pass
Since I suppose every user is unique, you will now get the data from the specific user, meaning you should not have to check for passwords anymore.
For the SQL statement, you should be able to search your database as follows and get only the entry you need back from it.
"SELECT * FROM table_name WHERE column_name LIKE input_string"
If input_string contains any of the special characters for SQL string comparison (% and _, I believe) you'll just have to escape them which can be done quite simply with regex. As I said in the comments, it's been a while since I've done SQL, but there's plenty of resources online for perfecting that query.
This should then return the entire row, and if I'm thinking correctly you should be able to then put the entire row into an array of objects all at once, or simply read them string by string and convert to values as needed using one of the Convert methods, as found here: http://msdn.microsoft.com/en-us/library/system.convert(v=vs.110).aspx
Edit as per Prix's comment: Data entered into the MySQL table should not need conversion.
Example to get an integer:
string x = [...];
[...]
var y = Convert.ToInt32(x);
If you're able to get them into object arrays, that works as well.
object[] obj = [...];
[...]
var x0 = Convert.To[...](obj[0]);
var x1 = Convert.To[...](obj[1]);
Etcetera.

List<T> to database and back again

So im trying to write my list to a database and ive got some questions. First of all nothing seems to get saved into the database but theres no errors that would cause a crash sofar and I know for a fact that this piece of code has worked in the past for me:
public void saveToDb(int var1, string var2)
{
SqlCommand cmd = new SqlCommand("Insert into [table] (col1, col2) VALUES (#param1, #param2)", conn);
cmd.Parameters.AddWithValue("#param1", var1);
cmd.Parameters.AddWithValue("#param2", var2);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
I have a three piece layer going on, ui, logic and DAL. First of all it didnt work with the database and the app.config files in the DAL class library so those were moved to the main class library and now its not whining about the database already existing and it finds my connection string.
To send the data into this method im doing it this way:
for (int i = 0; i < myList.Count; i++)
{
da.saveToDb(myList.val1, myList.val2);
}
Its not giving me an error in the code but nothing seems to be saved or is resetted when I stop the program but I dont know which but I can see the variable being passed correctly by printing them before doing the insert so im guessing the Db is resetted? Is it putting my db into the debug folder and flushing it everytime or what?
Another thing is last time I did this it was a form so there was always going to be just one insert at a time, right now my list of objects could contain anywhere from 1 to alot of objects and with this way I would be opening and closing the db connection once for each object in my list right? How do you do bulk inserts? Been reading about datasets but they all seem to be about reading from a database not writing so im not sure about that. I did read from a database in another project using Dataset and adapter so that shouldnt be a problem but how do I bulk send a list into a table? To make it abit more tricky I cant just convert the entire list because of 10 propertys 8 is going to go into one table and the remaining 2 is to go in a second table so I would need to loop them and add the respective property to the respective dataset (or what you use).
edit
Well ive now made some adjustments and im trying two methods and none of which work but neither gives an error either which is getting frustrating.
First method:
public void saveToDb(int val1, string val2)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["[MyConnectionString]"].ConnectionString);
SqlCommand cmd = new SqlCommand("Insert into [table] (val1, val2) VALUES (#param1, #param2)", conn);
cmd.Parameters.AddWithValue("#param1", val1);
cmd.Parameters.AddWithValue("#param2", val2);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
And the second I try to sqlbulkcopy a table:
public void SaveToDb()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["[MyConnectionString]"].ConnectionString))
{
conn.Open();
using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
{
bulk.ColumnMappings.Add("col1", "col1");
bulk.ColumnMappings.Add("col2", "col1");
bulk.DestinationTableName = "[table]";
System.Diagnostics.Debug.Print("Open");
bulk.WriteToServer(tab);
foreach (DataRow row in tab.Rows)
{
foreach (var item in row.ItemArray)
{
System.Diagnostics.Debug.Print(item.ToString());
}
}
System.Diagnostics.Debug.Print("sending");
}
System.Diagnostics.Debug.Print("closing");
}
}
I shouldnt have to map it since the table contains the exact same columns as the table with the same naming (upper/lower case) but it gets weird with the primary key which increments so I mapped it so it should add the increment automatically but nothing gets inserted, the print sure enough shows the values but if I comment out the mapping I get an error saying that null cant be assigned (which is true my table wont take nulls) but the value shouldnt be null since its there in the table?
It seems you forgot the index.
for (int i = 0; i < myList.Count; i++)
{
da.saveToDb(myList[i].val1, myList[i].val2);
}
I think you are missing the connection object.
var cn = new SqlConnection().
Then you call open on the connection object.
connection.command will give access to the command object.

Categories

Resources