代码之家  ›  专栏  ›  技术社区  ›  Sin

C#实体框架查询存储过程时出错“不支持区域性”

  •  2
  • Sin  · 技术社区  · 6 年前

    我试图从一个包含一些韩文文本的数据库中查询数据。这同样适用于其他没有任何韩语信息的数据库。我应该设置一些文化信息吗?我找到的数据库排序规则是 韩国语_Wansung_Unicode_CI_AS

    using (DBModel data = new DBModel())
    {
      //command and parameters prepared here  
      var SqlCommand = "ProcedureName @LastUpdateDate, @LastUpdateTS";
       var SqlParams = new SqlParameter[]
       {
         new SqlParameter { ParameterName = "@LastUpdateDate",  Value =lastDatetime, Direction = System.Data.ParameterDirection.Input },
         new SqlParameter { ParameterName = "@LastUpdateTS",  Value =lastTS, Direction = System.Data.ParameterDirection.Input }
        };
      return data.Database.SqlQuery<BP>(SqlCommand, SqlParams).ToList();
    }
    

    这个 正在投掷 CultureNotFoundException异常

    不支持区域性。

    参数名称:区域性

    66578(0x10412)是无效的区域性标识符。

    at System.Globalization.CultureInfo.GetCultureInfo(Int32 culture)
       at System.Globalization.CompareInfo.GetCompareInfo(Int32 culture)
       at System.Data.ProviderBase.FieldNameLookup.LinearIndexOf(String fieldName, CompareOptions compareOptions)
       at System.Data.ProviderBase.FieldNameLookup.IndexOf(String fieldName)
       at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
       at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
       at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader(DbDataReader storeDataReader, String columnName, Int32& ordinal)
       at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType(DbDataReader reader, Type type, MetadataWorkspace workspace)
       at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate[TElement](DbDataReader reader, String entitySetName, MergeOption mergeOption, Boolean streaming, EntitySet& entitySet, TypeUsage& edmType)
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
       at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass69`1.<ExecuteStoreQueryReliably>b__68()
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
       at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass69`1.<ExecuteStoreQueryReliably>b__67()
       at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, ExecutionOptions executionOptions, Object[] parameters)
       at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass14`1.<ExecuteSqlQuery>b__13()
       at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    

    我也尝试了同样的方法,用普通的老方法将数据检索到一个datatable,不知怎么的,它工作了。那么在EF中需要设置什么呢?

                var SqlCommand = "exec ProcedureName '2018-11-11 16:30:00', 163000";                   
    
                string constring = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ToString();
                SqlConnection con = new System.Data.SqlClient.SqlConnection(constring);
                SqlCommand cmd = new SqlCommand(SqlCommand, con);
                cmd.CommandType = System.Data.CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Marc Gravell    6 年前

    这里的LCID 在TDS解析器中设置-特别是 TdsParser.TryProcessEnvChange ;那意味着你无能为力 改变一切。

    我只能提供两个建议:

    1. 尝试将相关的朝鲜语语言包安装到要查询的计算机上;可能现在无法解析区域性,但将语言包安装到Windows中有助于理解-
    2. 看看是否可以更改数据库排序规则;现在很明显,这是 巨大的 有巨大影响的改变,所以不应该轻易地完成——最好是在所有其他方法都失败之后

    当然,第三个选择可能是查看EF或SqlClient人员(重叠的团队)是否在github上响应;他们在dotnet/corefx下。不过,public github主要是“.NET Core”,因此一个很好的起点是确认.NET Core上也发生了同样的问题;如果是这样,就更容易与人打交道。

    不要 声称对此有权威性。