Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 获取“a”的最大长度;“字符串”;使用LINQ到SQL的列_.net_Sql_Linq_Linq To Sql - Fatal编程技术网

.net 获取“a”的最大长度;“字符串”;使用LINQ到SQL的列

.net 获取“a”的最大长度;“字符串”;使用LINQ到SQL的列,.net,sql,linq,linq-to-sql,.net,Sql,Linq,Linq To Sql,是否可以获取VARCHAR、CHAR等的最大列长度?在纯T-SQL中,您可以使用以下查询: select max_length from sys.columns as c inner join sys.objects o on c.object_id = o.object_id where o.name = 'myTable' and c.name = 'myColumn' 对于linq to sql,您需要将其重写为linq。以下是一种避免触及数据库的方法: 使用反射,获取与所讨论的列对应

是否可以获取VARCHAR、CHAR等的最大列长度?

在纯T-SQL中,您可以使用以下查询:

select max_length from sys.columns as c inner join sys.objects o on c.object_id = o.object_id where o.name = 'myTable' and c.name = 'myColumn'

对于linq to sql,您需要将其重写为linq。

以下是一种避免触及数据库的方法:

  • 使用反射,获取与所讨论的列对应的实体类的属性
  • 然后,检索属性的System.Data.Linq.Mapping.Column属性
  • 然后,解析该属性的DbType属性(例如NVarChar(255)notnull)以获取列长度
    • 回答如下:

      虽然我发现改变更整洁:

      public static int GetLengthLimit(Type type, string field) //definition changed so we no longer need the object reference
      //Type type = obj.GetType(); //comment this line
      
      并使用以下方式拨打电话:

      int i = GetLengthLimit(typeof(Pet), "Name");
      
      有人能想出一种方法来强键入字段引用吗?

      public static int GetLengthLimit(Model.riffeedsentities,string table,string field)
      
      public static int GetLengthLimit(Model.RIVFeedsEntities ent, string table, string field)
      {
          int maxLength = 0;   // default value = we can't determine the length
      
          // Connect to the meta data...
          MetadataWorkspace mw = ent.MetadataWorkspace;
      
          // Force a read of the model (just in case)...
          // http://thedatafarm.com/blog/data-access/quick-trick-for-forcing-metadataworkspace-itemcollections-to-load/
          var q = ent.Clients;
          string n = q.ToTraceString();
      
          // Get the items (tables, views, etc)...
          var items = mw.GetItems<EntityType>(DataSpace.SSpace);
          foreach (var i in items)
          {
              if (i.Name.Equals(table, StringComparison.CurrentCultureIgnoreCase))
              {
                  // wrapped in try/catch for other data types that don't have a MaxLength...
                  try
                  {
                      string val = i.Properties[field].TypeUsage.Facets["MaxLength"].Value.ToString();
                      int.TryParse(val, out maxLength);
                  }
                  catch
                  {
                      maxLength = 0;
                  }
                  break;
              }
          }
      
          return maxLength;
      }
      
      { int maxLength=0;//默认值=无法确定长度 //连接到元数据。。。 MetadataWorkspace mw=ent.MetadataWorkspace; //强制读取模型(以防万一)。。。 // http://thedatafarm.com/blog/data-access/quick-trick-for-forcing-metadataworkspace-itemcollections-to-load/ var q=耳鼻喉科客户; 字符串n=q.ToTraceString(); //获取项目(表、视图等)。。。 var items=mw.GetItems(DataSpace.SSpace); foreach(项目中的var i) { if(i.Name.Equals(表,StringComparison.CurrentCultureIgnoreCase)) { //包装在其他没有MaxLength的数据类型的try/catch中。。。 尝试 { 字符串val=i.Properties[field].TypeUsage.Facets[“MaxLength”].Value.ToString(); int.TryParse(val,out maxLength); } 抓住 { maxLength=0; } 打破 } } 返回最大长度; }
      您想知道列的最大可能长度,例如varchar(1000)的1000或此列中值的最大长度吗?