如何获取表中特定列的长度 [英] How to get length of a specific column in a table

查看:43
本文介绍了如何获取表中特定列的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取表中特定列的长度,该表来自Visual Studio C#表单应用程序中名为 Users 的数据库.首先,我知道它与 column.length 命令有关,但是由于搜索了这些示例,所以我迷路了.

I am trying to get the length of a specific column in a table which table is from a database called Users in a visual studio C# form application. First of all i know it has to do with the column.length command but since those examples i have a searched i got lost.

有人可以告诉我一种简单的方法来实现这一目标吗?对于更具体的信息,我有一个名为 user_info 的表,它包含一个名为 searches 的列.我想将搜索的长度计入单个变量

Can someone tell me a simple way to get this happen? For more specific information i have a table called user_info and it contains a column which name is searches. I want to get the length of searches into a single variable

推荐答案

这是从数据库中提取列大小所需的C#代码.确保更新connString变量以包含您自己的SQL Server连接字符串.

Here is the C# code that you need to pull the column size from the database. Make sure you update the connString variable to contain your own SQL server connection string.

示例:持久性安全信息= False;集成安全性= true;初始目录=罗斯文;服务器=(本地)"

Int32 columnSize = 0;
string sql = "SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'user_info' AND COLUMN_NAME = 'searches'";
string connString = "Your Sql Server Connection String";
using (SqlConnection conn = new SqlConnection(connString))
{
   SqlCommand cmd = new SqlCommand(sql, conn);
   try
   {
      conn.Open();
      columnSize = (Int32)cmd.ExecuteScalar();
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
}

这篇关于如何获取表中特定列的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆