select from 数据库dbosyscolumns where object_name(id)='表名'
var
name: string;
FieldType: TFieldType;
i, size: integer;
begin
for i := 0 to adsFieldsCount - 1 do
begin
name := adsFields[i]FieldName; //列名
FieldType := adsFields[i]DataType; //字段类型
size := adsFields[i]DataSize; //字段长度
end;
end;
老是遇到TEXT类型和NTEXT类型的字段读不出来的问题今天弄了半天,又查了资料,终于把这个问题搞定了首先我们应该了解这两个类型的字段到底有什么不同:虽然看起这他们是叫TEXT,但他不是用来保存文本的,实际上是用来保存文件,和IMAGE类型一样的,一般用来保存MIME类型的数据而我把它用来保存新闻的主体内容了,这样能存的更多结果老是读取不出来我用的是ADO连接,在读取数据时,因为习惯了以前ASP的写法,用connexecute()来读取,,,这种读取对存MIME类型的字段是无效的,,,所以改成rsopen的方法来读取就好了记于此,以做学习那么这两种执行方法在本质上有什么不同个人认为用connexecute()方式得到的RS里对MIME类型应该只是得到了一个索引,而用rsopen()的方式得到的应该是MIME类型的内容吧!对两种方式得到的RS的字节大小做个比较应该能说明结果那么在应用时,在仅列出索引,标题什么的时,用connexecute() 在要显示完整内容时用rsopen(),我想这样能提高不少性能吧!
楼主是什麼数据库,什麼版本;
MS SQL用
sysobjects/syscolumns/systypes三个连接就行了
以SQL2005也可用
select
aName as 表名,
bName as 列名,
cName as 类型,
bmax_length as 字节数,
bPrecision as 整数位,
bScale as 小数位
from Systables a
inner join Syscolumns b on aobject_id=bobject_id
inner join sysTypes c on cUser_Type_ID=bUser_Type_ID
select from sysobjects 查询所有表
select
cname as tablename,
aname as colname,
bname as typename
from
syscolumns a,systypes b ,sysobjects c
where
axusertype=bxusertype
and aid=cid
order by cid
这个肯定得从系统表中查询了。
select aname as [column],bname as type
from syscolumns a,systypes b
where aid=object_id('表名') and axtype=bxtype
把“表名”替换成你要查看字段类型的表名,比如你要查看sysobjects表的各字段类型那么就是
select aname as [column],bname as type
from syscolumns a,systypes b
where aid=object_id('sysobjects') and axtype=bxtype
另外可以通过存储过程
exec sp_help 表名
来查看表各字段的信息,其中就包括字段类型。
CREATE PROCEDURE gettableinfo
/@TableName varchar(32)/
AS
/创建临时表/
create table #TableFields(
tableName varchar(32),
fieldname varchar(32),
fieldtype varchar(32),
fieldlength varchar(32),
scale varchar(32),
des varchar(256),
defaultvalue varchar(32),
CanNULL varchar(32)
)
/ 声明游标/
declare table_cur scroll cursor
for select sysobjectsname from sysobjects where sysobjectsxtype = 'U'
for update of sysobjectsname
/声明临时表名/
declare @TName varchar(32)
/ 打开游标/
open table_cur
fetch next from table_cur into @TName
while @@fetch_status=0 begin
SELECT sysobjectsname AS tableName, syscolumnsname AS filedname,
systypesname AS fieldtype, syscolumnslength,
syscolumnsscale
into #FiledInfo_Master
FROM syscolumns INNER JOIN
systypes ON syscolumnsxtype = systypesxtype INNER JOIN
sysobjects ON syscolumnsid = sysobjectsid
WHERE (sysobjectsxtype = 'U') AND (systypesname <> 'sysname') and sysobjectsname=@TName
/得到字段描述/
SELECT objname as filedname ,value into #FiledInfo
FROM ::fn_listextendedproperty('MS_Description', 'user',
'dbo', 'table', @TName,
'column', DEFAULT)
/得到字段缺省值/
SELECT objname as filedname, value as defaultvalue
into #FiledInfo2
FROM ::fn_listextendedproperty('DefaultValue', 'user',
'dbo', 'table', @TName,
'column', DEFAULT)
/得到字段是否可为空/
SELECT objname as filedname, value as CanNULL
into #FiledInfo3
FROM ::fn_listextendedproperty('MS_AllowBlanks', 'user',
'dbo', 'table', @TName,
'column', DEFAULT)
/联结字段描述和属性/
insert into #TableFields
SELECT cast(#FiledInfo_MastertableName as varchar(32)) ,
cast(#FiledInfo_Masterfiledname as varchar(32)),
cast(#FiledInfo_Masterfieldtype as varchar(32)),
cast(#FiledInfo_Masterlength as varchar(32)),
cast(#FiledInfo_Masterscale as varchar(32)),
cast(#FiledInfo[value] as varchar(256)),
cast(#FiledInfo2defaultvalue as varchar(32)),
cast(#FiledInfo3CanNULL as varchar(32))
FROM dbo#FiledInfo_Master LEFT OUTER JOIN
dbo#FiledInfo ON dbo#FiledInfo_Masterfiledname = dbo#FiledInfoFiledName
LEFT OUTER JOIN dbo#FiledInfo2
ON dbo#FiledInfo_Masterfiledname = dbo#FiledInfo2FiledName
LEFT OUTER JOIN dbo#FiledInfo3
ON dbo#FiledInfo_Masterfiledname = dbo#FiledInfo3FiledName
where
#FiledInfo_Mastertablename=@TName
fetch next from table_cur into @TName
--if exists (select from dbosysobjects where id = object_id(N'#FiledInfo_Master') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table #FiledInfo_Master
--if exists (select from dbosysobjects where id = object_id(N'#FiledInfo') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table #FiledInfo
--if exists (select from dbosysobjects where id = object_id(N'#FiledInfo2') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table #FiledInfo2
drop table #FiledInfo3
end
select from #TableFields
deallocate table_cur
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
uses typeinfo;
procedure TForm1Button1Click(Sender: TObject);
var
c : TFieldType;
begin
c := ftString;
//根据enum得到字符串
showmessage(GetEnumName(Typeinfo(TFieldType),Ord(c)));
// 根据字符串得到枚举类型
c := TFieldType(GetEnumValue(Typeinfo(TFieldType),'ftInteger'));
showmessage(GetEnumName(Typeinfo(TFieldType),Ord(c)));
end;
如果把照片直接保存在SQL Server数据库中,微软推荐用varbinary(MAX)字段。下面的代码演示了用C# *** 作varbinary(MAX)字段的基本方法。
1、新增记录
private void btnBrowse_Click(object sender, EventArgs e)//浏览照片
{
OpenFileDialog dlg = new OpenFileDialog();
dlgFilter = "jpg(jpg文件)|jpg|gif|gif";
dlgFilterIndex = 1;
if (dlgShowDialog()==DialogResultOK)
{
textBox3Text = dlgFileName;
pictureBox1Image = ImageFromFile(dlgFileName);
}
}
//新增记录
using (SqlConnection conn = new SqlConnection(ConfigurationManagerConnectionStrings["db"]ConnectionString))
{
String sql = "insert into emp(name,age,photo) values(@name,@age,@photo)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmdParametersAddWithValue("@name", textBox1Text);
cmdParametersAddWithValue("@age", ConvertToInt32(textBox2Text));
//byte[] b;
//using(FileStream fs=new FileStream(textBox3Text,FileModeOpen,FileAccessRead))
//{
// b = new byte[fsLength];
// fsRead(b, 0, (int)fsLength);
//}
byte[] b;
if (textBox3Text != "")
{
b = FileReadAllBytes(textBox3Text);
cmdParametersAddWithValue("@photo", b);
}
else
{
cmdParametersAddWithValue("@photo", SystemDataSqlTypesSqlBinaryNull);
}
connOpen();
try
{
cmdExecuteNonQuery();
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
}
}
2、显示记录信息并提供修改功能
private void FormDetail_Load(object sender, EventArgs e)//显示记录详细信息
{
using (SqlConnection conn = new SqlConnection(ConfigurationManagerConnectionStrings["db"]ConnectionString))
{
String sql = "select from emp where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmdParametersAddWithValue("@id", id);
connOpen();
using (SqlDataReader dr = cmdExecuteReader())
{
if(drRead())
{
textBox1Text = dr[1]ToString();
textBox2Text = dr[2]ToString();
if (!drIsDBNull(3))//防止照片字段为空
{
SystemDataSqlTypesSqlBytes bytes = drGetSqlBytes(3);
pictureBox1Image=ImageFromStream(bytesStream);//显示照片
}
}
}
}
}
private void btnSave_Click(object sender, EventArgs e)//更新记录
{
using (SqlConnection conn = new SqlConnection(ConfigurationManagerConnectionStrings["db"]ConnectionString))
{
byte[] b;
if (textBox3Text != "")//需要更新照片
{
String sql = "update emp set name=@name,age=@age,photo=@photo where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmdParametersAddWithValue("@name", textBox1Text);
cmdParametersAddWithValue("@age", ConvertToInt32(textBox2Text));
cmdParametersAddWithValue("@id", id);
b = FileReadAllBytes(textBox3Text);
cmdParametersAddWithValue("@photo", b);
connOpen();
try
{
cmdExecuteNonQuery();
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
}
}
else//不需要更新照片
{
String sql = "update emp set name=@name,age=@age where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmdParametersAddWithValue("@name", textBox1Text);
cmdParametersAddWithValue("@age", ConvertToInt32(textBox2Text));
cmdParametersAddWithValue("@id", id);
connOpen();
try
{
cmdExecuteNonQuery();
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
}
}
}
}
以上就是关于DELPHI怎么获取SQL指定字段的字段类型全部的内容,包括:DELPHI怎么获取SQL指定字段的字段类型、如何读取SQL数据库里TEXT和NTEXT类型字段内容的问题、sql查询字段类型等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)