Casting - IDataParameter

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Is there some way to cast a IDataParameter to a SQLParameter? It's not
working and I'm not sure why.
 
Hi Doug,

If you are referring to System.Data.SqlClient.SqlParameter, then yes. It implements IDataParameter (I checked in the 2.0
framework).

If you'd like to include a short but complete code sample (See Jon Skeet's article:
http://www.yoda.arachsys.com/csharp/complete.html) then I'm sure somebody would be happy to help you.
 
Hi

That works perfect for me:

using System;
using System.Data;
using System.Data.SqlClient;
using NUnit.Framework;

namespace myNameSpace
{
[TestFixture]
public class TestCasting
{
[Test]
public void CastIDataParameter()
{
IDataParameter param = new SqlParameter("myParamName", true);
SqlParameter castedParam = (param as SqlParameter);

Assert.AreEqual("myParamName", castedParam.ParameterName);
Assert.AreEqual(true, castedParam.Value);

Assert.AreEqual("myParamName", param.ParameterName);
Assert.AreEqual(true, param.Value);
}
}
}

Greetings from Switzerland
Thomas
 
I decided not to use the IDataParameter object and went with using the
SqlParameter object directly. I got rid of the code I had before that
wasn't working so unfortunately can't send it along, but I think it
might have been more of a problem with some common code I was trying to
use within our group. Thanks though!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top