Obtaining the Maximum Field Size of an Access Column

喜你入骨 提交于 2020-01-17 04:11:01

问题


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 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.


回答1:


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




回答2:


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();
        }



来源:https://stackoverflow.com/questions/6953588/obtaining-the-maximum-field-size-of-an-access-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!