.net - Determine a SQL Server Database Column’s Default from C# -
i need find default value of column in sql server 2005 or 2008 database .net.
my first attempt execute stored procedure:
sp_help @objname
where @objname
table. in seventh table returned (the constraints table), going use value in constraint_keys
default constraints. when run query sql server management studio behaves intended, when run c# code, column constraint_keys
null (although other columns populated).
my second attempt use:
select name, [text] syscomments com inner join syscolumns col on com.id = col.cdefault col.id = object_id(@table) , col.cdefault > 0
which works fine in sql server management studio. when run .net, however, returns no rows.
additional:
example .net code (using enterprise libraries):
private datatable getdefaults(string tablename string database) { var db = databaseel.create(database); string sql = @" select name, [text] syscomments com inner join syscolumns col on com.id = col.cdefault col.id = object_id(@table) , col.cdefault > 0"; using (var command = db.getsqlstringcommand(sql)) { db.addinparameter(command, "@table", dbtype.string, tablename); return db.executedatatable(command); } }
note have tried each of following linked similar question:
select name, object_definition(default_object_id) default_value sys.columns object_id = object_id(@table) , default_object_id != 0
and
select sc.name column_name, sm.text default_value sys.sysobjects join sys.syscolumns sc on sc.id = so.id left join sys.syscomments sm on sm.id = sc.cdefault so.xtype = 'u' , so.name = @table , sm.text not null
they both worked in ssms, in .net, former had nulls in default_value
column, , latter had no rows.
the answer seems to grant user access db_ddladmin
role. while db_datareader
, db_datawriter
enough run necessary queries, return nulls default values if user has no access db_ddladmin
.
the reason worked me sql server management studio, of course, because logged in administrative user.
Comments
Post a Comment