不使用参数的没有问题,例子如下(我用了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();
}
}
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();
}
}
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.
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参数,也一样失败。
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();