.NET中的Oracle列默认值

时间:2013-03-29 14:53:41

标签: .net oracle

当我在Oracle中运行以下SQL时,会正确报告列默认值('Test')。当我在.NET中运行相同的SQL时,报告的默认值为NULL:

SELECT column_name, data_default FROM user_tab_columns WHERE table_name='MYTABLENAME'

有问题的列是Varchar2。我还测试了一个列 - 同样的问题。

数据库版本是Oracle Database 11g Express Edition版本11.2.0.2.0 - 生产

.NET连接器是ODP.NET

操作系统是Windows 7/64

我从all_tab_columns获得相同的结果(NULL)

.NET代码:

Dim provider As DbProviderFactory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client")
Using connection As DbConnection = provider.CreateConnection
  connection.ConnectionString = "Data Source=localhost; User Id=userid; Password=password"
  connection.Open
  Using dt As New DataTable
    Using command As DbCommand = provider.CreateCommand
      command.CommandText = "SELECT column_name, data_default FROM user_tab_columns WHERE table_name='MYTABLENAME'"
      command.Connection = connection
      Using da As DbDataAdapter = provider.CreateDataAdapter
        da.SelectCommand = command
        da.Fill(dt)
      End Using
    End Using
    For Each row As DataRow in dt.Rows
      Response.Write(row("COLUMN_NAME") & " " & row("DATA_DEFAULT") & "<br>")
    Next
  End Using
End Using

2 个答案:

答案 0 :(得分:2)

data_default属于Oracle类型LONG。通过此文档http://docs.oracle.com/html/B14164_01/featData.htm#i1007197,您似乎需要执行一些特殊工作来检索LONG值。

答案 1 :(得分:0)

我最终创建了一个Oracle函数来处理这个问题:

CREATE OR REPLACE 
  FUNCTION GetColumnDefaultValue(
    TableName IN varchar2,
    ColumnName IN varchar2
  )
  RETURN varchar2
    AS ddLong long; 
    BEGIN
      SELECT data_default
      INTO ddLong
      FROM user_tab_columns
      WHERE table_name = TableName
        AND column_name = ColumnName;
    RETURN
      substr(ddLong,0,255);
    END;

使用以下方法检索默认值:

SELECT GetColumnDefaultValue('TABLENAME','FIELDNAME') FROM dual

请注意,这只检索默认值的第一个字节。修改substr(ddLong,0,255);以返回更多内容。