C# 无法转换类型';WebMatrix.Data.DynamicRecord';至';int';

C# 无法转换类型';WebMatrix.Data.DynamicRecord';至';int';,c#,sql,webmatrix,C#,Sql,Webmatrix,无法将类型“WebMatrix.Data.DynamicRecord”转换为“int”,尝试将QueryValue方法的值输出为int时会出现异常 public class SaveCustomerData : IDisposable { private readonly string _connectionString; private readonly Database _db; public SaveCustomerData(string connectionString) {

无法将类型“WebMatrix.Data.DynamicRecord”转换为“int”,尝试将QueryValue方法的值输出为int时会出现异常

public class SaveCustomerData : IDisposable
{
private readonly string _connectionString;
private readonly Database _db;

public SaveCustomerData(string connectionString)
{
    _connectionString = connectionString;
    _db = Database.Open(connectionString);
}

public void Insert(string fname, string lname, string phone, string cellno, DateTime dob, string gender, string address, string address2, string creditno, string city, string postalcode, string email) {
    var uid = Convert.ToInt32(_db.QuerySingle("SELECT UserID FROM UserProfile WHERE Email=@0",email));



    _db.Execute("INSERT INTO Customer_Table (customer_fname, customer_lname, customer_phone, customer_mobile, customer_dob, customer_gender, customer_address, customer_address2, customer_credit_no, customer_city, customer_postal, UserID) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12)",
        fname, lname, phone, cellno, dob, gender, address, address2, creditno, city, postalcode, uid);
您必须使用以下选项:

_db.QuerySingle("SELECT UserID FROM UserProfile WHERE Email=@0",email)
   .Item["UserID"];
为了获得用户ID的值,将返回
用户ID
。您在问题中发布了错误,因为您的查询返回类型为
WebMatrix.Data.DynamicRecord
的对象

通过使用自定义类型描述符和 动态语言运行库(DLR)的功能

如前所述,而不是
UserID
的值

为了获得执行
\u db.QuerySingle(…)
后返回的记录中某一列的值,您有两个选项:

  • 项[Int32]
使用指定的索引返回DynamicRecord实例中的列的值

  • 项[字符串]
使用指定的名称返回DynamicRecord实例中某列的值


我刚刚更新了我的帖子。您可以使用[0]获取该值,该值将返回索引为0的列中的值,也可以使用列的名称检索该值。