不使用参数的没有问题,例子如下(我用了Type.InvokeMember()来实现类似VB.NET的缺省参数功能):
private void FillIDList()
{
string connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

string sql = "select ItemID from Offer";

ADODB.ConnectionClass cnn = new ADODB.ConnectionClass(); cnn.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, cnn, new object[]{connectionString}); try
{
ADODB.Recordset rs = (ADODB.Recordset)cnn.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, 
null, cnn, new object[]{sql});

this.DropDownList1.Items.Clear(); while(!rs.EOF)
{
this.DropDownList1.Items.Add(new ListItem((string)rs.Fields["ItemID"].Value, ""));
rs.MoveNext();
} }
finally
{
cnn.Close();
}
}

解决方案 »

  1.   

    have you triedparam.Value = new Guid(this.DropDownList1.SelectedItem.Text);?
      

  2.   

    yes I've already tried and got another exception:
    Application uses a value of the wrong type for the current operation.Seems we can not use System.Guid type directly in ADODB Guid parameter. I believe the first question has nothing to do with Guid type... even if I'm using a varchar parameter in another query, still get same exception.
      

  3.   

    English is very pool...so 看不懂 what you say
      

  4.   

    我的意思是说我也试过给param.Value赋予Guid的方法,但仍然会有异常:
    Application uses a value of the wrong type for the current operation.
    这个异常甚至不到cmd.Parameters.Append()就抛出了:
    param.Value = new Guid(this.DropDownList1.SelectedItem.Text);//这一步就出错了看起来不能把System.Guid类型的值赋予ADODB中ADODB.DataTypeEnum.adGUID类型的参数(不兼容?)。不过我相信Append()的时候出错不是数据类型的原因,因为我在另一个查询中使用varchar参数,也一样失败。
      

  5.   

    how did you define your stored procedure?I am not going to do testing on ADODB for you, but here is a suggestion, instead of using guid, how about just using char(36) as the parameter, I believe the SQL Server will convert it for you1. 
    use tempdb
    gocreate table testguid (id int identity primary key, name uniqueidentifier)
    goinsert into testguid values ('3854091E-A03A-4E3D-B5C5-786272725DE0')
    gocreate proc getguid
    @name char(36)
    as
      select * from testguid where name = @namegocreate proc getguid2
    @name uniqueidentifier
    as
      select * from testguid where name = @namego2. 
    string sConn = "server=localhost;database=tempdb;uid=sa;pwd=;";
    string sSql = "getguid";
    SqlConnection conn = new SqlConnection(sConn);
    SqlCommand cmd = new SqlCommand(sSql, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@name", "3854091E-A03A-4E3D-B5C5-786272725DE0");
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    Console.WriteLine("{0}={1}", reader["id"].GetType().Name, reader["id"]);
    Console.WriteLine("{0}={1}", reader["name"].GetType().Name, reader["name"]);
    }
    reader.Close();
    conn.Close();3. 
    string sConn = "server=localhost;database=tempdb;uid=sa;pwd=;";
    string sSql = "getguid2";
    SqlConnection conn = new SqlConnection(sConn);
    SqlCommand cmd = new SqlCommand(sSql, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@name", new Guid("3854091E-A03A-4E3D-B5C5-786272725DE0"));
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    Console.WriteLine("{0}={1}", reader["id"].GetType().Name, reader["id"]);
    Console.WriteLine("{0}={1}", reader["name"].GetType().Name, reader["name"]);
    }
    reader.Close();
    conn.Close();
      

  6.   

    谢谢思归。我的数据库使用Guid作为主键,在ADO.NET下工作得很好。我的程序中有一部分使用OWC PivotTable来做数据透视,而OWC不支持直接访问ADO.NET数据源,但是能够支持直接使用ADO RecordSet形成的Xml数据源,因此我不得不用ADODB。添加参数会出错的问题是我今天刚刚遇到的,不清楚怎么回事,所以上来问问。如果思归大哥能够提供一种让PivotTable绑定到ADO.NET DataSet数据源的办法,那也可以绕开这个问题,我可就感激不尽了……我想通过XSL来把DataSet.WriteXml()产生的XmlDocument转换成RecordSet Xml格式也不是不可以,就是比较麻烦(我现在还不会用XSL),不知道有没有别比较容易的办法?
      

  7.   

    晕了……CSDN上没人能解决这个问题吗? :~~~~~~~~~~~(