I've been stuck all day long with something I thought it would be easy
to do.
I'm using MS Enterprise Library and C# (.net 3.5) to retrieve some
data from informix (via OleDB). If I create a statement like
Database db = Program.GetDatabase(myDbName);
try
{
int id = 386
string sqlCommand = "select * from users where id = "
+ id;
DbCommand dbCommand =
db.GetSqlStringCommand(sqlCommand);
DataSet ds = db.ExecuteDataSet(dbCommand);
Console.WriteLine(String.Format("Username = {0}",
ds.Tables[0].Rows[0]["name"]));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
It works fine, thus proving that I have connection to the informix.
Now, I don't want to use the concatenation for obvious reasons, so I
tried to use parameters... I tried with @ and :, I'll post here my
version with :, but it doesn't work:
Database db = Program.GetDatabase(_cmecfDatabaseName);
try
{
string sqlCommand = "select * from users where id
= :id";
DbCommand dbCommand =
db.GetSqlStringCommand(sqlCommand);
db.AddInParameter(dbCommand, ":id", DbType.Int32,
386);
DataSet ds = db.ExecuteDataSet(dbCommand);
Console.WriteLine(String.Format("Username = {0}",
ds.Tables[0].Rows[0]["name"]));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
It catches me the following exception:
E42000: (-201) A syntax error has occurred.
Anybody had the same problem?
Cheers
database.AddInParameter(clearCommand, "@BaseName", DbType.String, baseName);
And in another site:
Looking at your code, you wrote:public void AddInParameter ( DbCommand command, string name, DbType dbType, Object value )
No, but I think I can tell the difference between an Informix-SQL
question and a C# API question.
If the output of Gumby's impressive crystal ball didn't work, I'd ask in
news:microsoft.public.dotnet.languages.csharp
--
RGB
1Using thi example,using Microsoft.Practices.EnterpriseLibrary.Data; 2
using Microsoft.Practices.EnterpriseLibrary.Data.Sql; 3
using System.Data.SqlClient; 4
5
SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("DemoDbSqlClient"); 6
7
SqlCommand command = (SqlCommand)db.GetSqlStringCommand( 8
"SELECT * FROM CUSTOMER WHERE FIRSTNAME = @FIRSTNAME AND LASTNAME = @LASTNAME"); 9
10
db.AddInParameter(command, "@FirstName", SqlDbType.VarChar, "Luis"); 11
command.Parameters[0].Size = 50; 12
db.AddInParameter(command, "@LastName", SqlDbType.VarChar, "Ramirez"); 13
command.Parameters[1].Size = 50; 14
15
this.GridView1.DataSource = db.ExecuteReader(command); 16
this.GridView1.DataBind();
You can see that they use @ and not :. Ok interesting enough.
Also note that the substitution parameter outside of the string isn't in all caps. That would
imply that the variable susbstitution will take the 2nd parameter and upshift it in
both the string and the input variable name and then match on that.
Also note that you don't necessarily need to have a variable as the last object. You could put a string
literal in its place.
But hey! What do I know?
I just did this in less time than it takes for my coffee to cool.
string insertCustomerSql = "INSERT INTO Customers
(Name, EmailAddress) VALUES (@Name,@EmailAddress)
SELECT @CustomerId = SCOPE_IDENTITY()"; string insertOrderSql = "INSERT INTO Orders (CustomerId,
Number, OrderDate) VALUES (@CustomerId, @Number,
@OrderDate)"; using (TransactionScope scope =
new TransactionScope(TransactionScopeOption.RequiresNew)) { DbCommand insertCustomerCommand =
database.GetSqlStringCommand(insertCustomerSql); database.AddInParameter(insertCustomerCommand,
"Name", DbType.String, "Bill Doe"); database.AddInParameter(insertCustomerCommand,
"EmailAddress", DbType.String, "bi...@doe.com"); database.AddOutParameter(insertCustomerCommand,
"CustomerId", DbType.Int32, 4); database.ExecuteNonQuery(insertCustomerCommand); int customerId = (int)database.GetParameterValue
(insertCustomerCommand, "CustomerId"); DbCommand insertOrderCommand =
database.GetSqlStringCommand(insertOrderSql); database.AddInParameter(insertOrderCommand,
"CustomerId", DbType.Int32, customerId); database.AddInParameter(insertOrderCommand,
"Number", DbType.String, "12345"); database.AddInParameter(insertOrderCommand, "OrderDate",
DbType.DateTime, System.DateTime.Now); database.ExecuteNonQuery(insertOrderCommand);
My intent was to encourage the OP to find an appropriate forum for any
further C# API questions.
You probably mistook my "impressive" for sarcasm instead of the genuine
praise I intended. You crusty old git :-)
--
RGB
I'm just asking here because this works with other DB's (MS SQL for
example)... But maybe the problem is not informix, but the OleDb
driver? I don't know...
now I see sarcasm :-)
Nope, that didn't work. And I remember I tried that yesterday a couple
of times... I will try the suggestions below.
If I only had a way to see what statement is being sent to informix,
or what informix is receiving, that would be easier.
You can use onmode -I 201 and run the app again, this will cause a af
file and a mem dump to be created. You can look at it to see what has
been sent to the server
Alternatively, you can try turning SQLIDEBUG on.
That works.... for MS SQL. For informix, the first problem is that
obviously you can't cast to SqlCommand. Using the generic database
(Database class), informix keep responding with syntax error:
ditch the enterprise library and use the IBM drivers directly... this
one now works:
try
{
string IfxConnectionString =
"Host=blablabla; " +
"Service=blablabla; " +
"Server=blablabla; " +
"Database=blablabla; " +
"User Id=blablabla; " +
"Password=blablabla; ";
IfxConnection myIfxConn = new
IfxConnection(IfxConnectionString);
myIfxConn.Open();
IfxCommand myIfxCmd = new IfxCommand();
myIfxCmd.Connection = myIfxConn;
myIfxCmd.CommandText = "SELECT * FROM USERS WHERE ID
= ?;";
myIfxCmd.CommandType = CommandType.Text;
myIfxCmd.CommandTimeout = 1200;
myIfxCmd.Parameters.Add("ID", IfxType.Integer);
myIfxCmd.Parameters["ID"].Value = 386;
IfxDataReader reader = myIfxCmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("id={0},
name={1}",reader["prid"],reader["name"]));
}
myIfxConn.Close();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
This one works good as well....
So now I wonder... the '?' thing... is a requirement from who? The
informix driver? Or the DB itself?
Console.WriteLine("Test Parameter");
Console.WriteLine("===========================");
Database db =
DatabaseFactory.CreateDatabase(_cmecfDatabaseName);
try
{
string sqlCommand = "SELECT * FROM USERS WHERE PRID
= ?;";
DbCommand dbCommand =
db.GetSqlStringCommand(sqlCommand);
int prid = 386;
db.AddInParameter(dbCommand, "prid", DbType.Int32,
prid);
}
_______________________________________________
Informix-list mailing list
Inform...@iiug.org
http://www.iiug.org/mailman/listinfo/informix-list