2012-02-25 75 views
1

我將對象分配給此SqlParameter列表,然後嘗試執行SqlCommand,但它引發異常,表示其中一個對象無法轉換爲SqlDbType。我最好在將它們添加到參數集合列表之前處理這些對象。那麼,我將如何檢查一個值被添加到參數列表中是否是一個好的/適當的值?我應該檢查什麼屬性?如何知道/檢查一個值是否與SqlDbType兼容?

這裏是我的代碼:

bool Submit(Progs progs, CommandType commandType, string commandText) 
{ 
    try 
    { 
     List<SqlParameter> paramCollection = new List<SqlParameter>(); 
     foreach(Prog p in progs) 
     { 
      SqlParameter spTemp = new SqlParameter { ParameterName = p.Name , Value = p.Value}; 
      paramCollection.Add(spTemp); 
      using (SqlConnection con = GetConnection()) 
      { 
       SqlCommand cmd = new SqlCommand { CommandType = commandType, CommandText = commandText, Connection = con }; 
       con.Open(); 
       cmd.Parameters.AddRange(paramCollection); // Exception is thrown from this line 
       cmd.ExecuteNonQuery(); 
      } 
      return true; 
     } 
     catch(Exception exc) 
     { 
      return false; 
     } 
    } 

異常thown說: 沒有映射從對象類型sol2.CodeBase.BL.Letter []到已知的託管提供原生類型的存在。

PS:有一個屬性的SqlParameter的所謂ParamaterIsSqlType(是的,這paramAter而不是參數),其僅在運行時出現(即當我檢查spTemp與下一行斷點),並且這些始終設置爲false?這是什麼樣的屬性,以便它只在運行時出現?另外,這個「ParamaterIsSqlType」表示什麼值?

+0

此屬性是不公開的,因此你不能看也不能使用它,是的,它的拼寫錯誤,但[不固定] (http://connect.microsoft.com/VisualStudio/feedback/details/90483/the-word-parameter-is-misspelled-in-the-paramaterissqltype-flag-in-the-non-public-members-of-system -data-的SqlClient-的SqlParameter)。 – 2012-02-25 20:48:42

+0

什麼是'param'?它在哪裏宣佈?您還打開連接兩次,應該引發[InvalidOperationException](http://msdn.microsoft.com/en-us/library/system.invalidoperationexception.aspx)。 – 2012-02-25 20:54:23

+0

雅,這是一個打字錯誤,現在修復。所以,現在,你會如何建議檢查轉換是否可能?另外,這個paramaterIsSqlType表示什麼(爲什麼它總是錯誤的,或者當它是真的)? – MrClan 2012-02-25 21:06:52

回答

0

做什麼SqlParameterwould do to infer conversionTypeSqlDbType如果它沒有明確設置。因此,不,框架中沒有可用的屬性或方法。

System.Type type = p.Value.GetType(); 
var isConvertible = IsConvertibleToSqlDbType(type); 
if(!isConvertible){ 
    //call your custom ToSqlType-method 
} 

下面的方法直接從SqlParemeter'sprivate void InferSqlType (object value)得出:

public static bool IsConvertibleToSqlDbType(Type type) 
{ 
    switch(type.FullName) { 
     case "System.Int64": 
     case "System.Data.SqlTypes.SqlInt64": 
      //SetSqlDbType (SqlDbType.BigInt); 
      return true; 
     case "System.Boolean": 
     case "System.Data.SqlTypes.SqlBoolean": 
      //SetSqlDbType (SqlDbType.Bit); 
      return true; 
     case "System.String": 
     case "System.Data.SqlTypes.SqlString": 
      //SetSqlDbType (SqlDbType.NVarChar); 
      return true; 
     case "System.DateTime": 
     case "System.Data.SqlTypes.SqlDateTime": 
      //SetSqlDbType (SqlDbType.DateTime); 
      return true; 
     case "System.Decimal": 
     case "System.Data.SqlTypes.SqlDecimal": 
      //SetSqlDbType (SqlDbType.Decimal); 
      return true; 
     case "System.Double": 
     case "System.Data.SqlTypes.SqlDouble": 
      //SetSqlDbType (SqlDbType.Float); 
      return true; 
     case "System.Byte[]": 
     case "System.Data.SqlTypes.SqlBinary": 
      //SetSqlDbType (SqlDbType.VarBinary); 
      return true; 
     case "System.Byte": 
     case "System.Data.SqlTypes.SqlByte": 
      //SetSqlDbType (SqlDbType.TinyInt); 
      return true; 
     case "System.Int32": 
     case "System.Data.SqlTypes.SqlInt32": 
      //SetSqlDbType (SqlDbType.Int); 
      return true; 
     case "System.Single": 
     case "System.Data.SqlTypes.Single": 
      //SetSqlDbType (SqlDbType.Real); 
      return true; 
     case "System.Int16": 
     case "System.Data.SqlTypes.SqlInt16": 
      //SetSqlDbType (SqlDbType.SmallInt); 
      return true; 
     case "System.Guid": 
     case "System.Data.SqlTypes.SqlGuid": 
      //SetSqlDbType (SqlDbType.UniqueIdentifier); 
      return true; 
     case "System.Money": 
     case "System.SmallMoney": 
     case "System.Data.SqlTypes.SqlMoney": 
      //SetSqlDbType (SqlDbType.Money); 
      return true; 
     case "System.Object": 
      //SetSqlDbType (SqlDbType.Variant); 
      return true; 
     default: 
      return false; 
    } 
} 
+0

這個怎麼樣? ...*修改後的代碼*: SqlParameter spTemp = new SqlParameter {ParameterName = p.Name,Value = p.Value}; 嘗試 SqlDbType sqlType = spTemp.SqlDbType; } catch { spTemp.Value = spTemp.Value.ToSqlType(); } paramCollection.Add(spTemp); – MrClan 2012-02-26 05:43:52

相關問題