开发者

Obtaining the Maximum Field Size of an Access Column

开发者 https://www.devze.com 2023-03-26 06:35 出处:网络
Hi all you Stack Overflowers. I am having bit of a dilemma here. I need to obtain the Maximum of characters you may enter into a field but I have had no success and Google did not produce anyhing simi

Hi all you Stack Overflowers. I am having bit of a dilemma here. I need to obtain the Maximum of characters you may enter into a field but I have had no success and Google did not produce anyhing similar to what I need

Here is my Code

            lvwDestination.Columns.Add("Fields in开发者_StackOverflow中文版 Database", 150, HorizontalAlignment.Left);

            lvwDestination.Columns.Add("DataType", 100, HorizontalAlignment.Left);

            lvwDestination.Columns.Add("Max", 100, HorizontalAlignment.Left);

            for (int i = 0; i < DataTable.Columns.Count; i++)
            {

                _lvwItem = new ListViewItem(DataTable.Columns[i].ColumnName, 0);

                _lvwItem.Name = DataTable.Columns[i].ColumnName;

                _lvwItem.SubItems.Add(DataTable.Columns[i].DataType.Name.ToString());

               _lvwItem.SubItems.Add(DataTable.Columns[i].MaxLength.ToString());
                lvwDestination.Items.AddRange(new ListViewItem[] { _lvwItem });


            }

Now as you can see I have tried

_lvwItem.SubItems.Add(DataTable.Columns[i].MaxLength.ToString());

but the only thing that this came up with is -1 for every column. I have also made sure that my columns do in fact have the property field size attribute on. The .ColumnName and .DataType works perfectly but .MaxLength produces -1 always. Thanks in advance for anyone that might be able to come up with a solution.


try setting following property: OledbDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey of your DataAdapter before calling the Fill method...


I've run a quick test against a SQL database table - it looks like the MaxLength property isn't retrieved from the database and always has its value set as -1 unless specifically set by the user:

        string sql = "select top 10 * from [TestTable]";
        string connStr = ConfigurationManager.ConnectionStrings["DbConnString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable data = new DataTable();
            da.Fill(data);

            foreach (DataColumn col in data.Columns)
            {
                Console.WriteLine(
                    string.Format("{0}\t{1}\t{2}",
                            col.ColumnName,
                            col.DataType.ToString(),
                            col.MaxLength
                        )
                    );
            }
            Console.ReadLine();
        }

0

精彩评论

暂无评论...
验证码 换一张
取 消