Re: Passing an int to a query
Posted by:
Maurice
Date: March 10, 2005 09:39AM
I actually was working more on the problem and it seems parameters are the answer. I'm taking in a string as user input (hardcoded at the minute for testing) and passing this into the query. Problem is nothing is returned at the end. Here's my code:
#region Using directives
using System;
using MySql.Data.MySqlClient;
#endregion
namespace ParameterTest
{
class ParamDemo
{
static void Main(string[] args)
{
// conn and reader declared outside try block for visibility in finally block
MySqlConnection conn = null;
MySqlDataReader reader = null;
string inputId = "52692945";
try
{
// instantiate and open connection
conn = new MySqlConnection("Userid=root;database=evoting;server=localhost;password=evargsoc");
//conn = MySqlConnection("Server=(local);DataBase=evoting;Integrated Security=SSPI");
conn.Open();
// 1. declare command object with parameter
MySqlCommand cmd = new MySqlCommand("select HasVoted from valid_voters ", conn);
// 2. define parameters used in command object
MySqlParameter param = new MySqlParameter();
param.ParameterName = "@Id";
param.Value = inputId;
// 3. add parameter to command object
cmd.Parameters.Add(param);
// get data stream
reader = cmd.ExecuteReader();
// write each record
while (reader.Read())
{
Console.WriteLine("{0}", reader["HasVoted"]);
}
}
finally
{
// close reader
if (reader != null)
{
reader.Close();
}
// close connection
if (conn != null)
{
conn.Close();
}
}
}
}
}
If I remove the WHERE clause the program works fine. Any ideas?
Subject
Written By
Posted
Re: Passing an int to a query
March 10, 2005 09:39AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.