关于SQLDMO修改一个表的字段类型的问题!

jinqinghai 2006-08-17 11:46:42
SQLDMO.SQLServer2 svr = new SQLDMO.SQLServer2Class();
svr.Connect(ServerName, UserName, Password);
SQLDMO.Database2 myDb = new SQLDMO.Database2Class();
myDb = (SQLDMO.Database2)svr.Databases.Item(DatabaseName, "owner");
SQLDMO.Table2 myTb = new SQLDMO.Table2Class();
foreach (SQLDMO.Table2 tb in myDb.Tables)
{
if (tb.Name == TableName)
myTb = tb;
}
SQLDMO.Column2 cm = (SQLDMO.Column2)myTb.Columns.Item(ColumnName);

修改成为varchar,nvarchar类型的时候
cm.AlterDataType("Numeric",9,9, 3); 成功

修改为int的时候
cm.AlterDataType("int",4,9, 0); 出错

到底 AlterDataType方法修改字段为int类型的时候,怎样的参数才可以??
...全文
240 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yellow198267 2006-08-17
  • 打赏
  • 举报
回复
jinqinghai 2006-08-17
  • 打赏
  • 举报
回复
连接数据库出错:[Microsoft][ODBC SQL Server Driver][SQL Server]第 0 个列或参数: 不能对数据类型 int 指定列宽度。

转成其他类型都没有错,就是int类型头疼。。。。
zxkid 2006-08-17
  • 打赏
  • 举报
回复
string data types cannot be converted to numeric data types
--
字符型不能转换数值型

报的什么错?
jinqinghai 2006-08-17
  • 打赏
  • 举报
回复
To Edifier0709(腦袋重構中.....)

cm.AlterDataType("int",4,0, 0); 试过,抱错。

to oolongTea(乌龙茶)

cm.AlterDataType("int"); 不可以,因为必须传递4个参数。 在visual basic 里面是可以的


oolongTea 2006-08-17
  • 打赏
  • 举报
回复
Syntax

object.AlterDataType( Datatype , [ Length ] , [ Precision ] , [ Scale ] )


object

Expression that evaluates to an object in the Applies To list

DataType

String that specifies the new data type

Length

Optional long integer that specifies the length of a string data type

Precision

Optional long integer that specifies the precision of a numeric data type

Scale

Optional long integer that specifies the scale of a numeric data type.

Prototype (C/C++)

HRESULT AlterDataType(
SQLDMO_LPCSTR DataType,
long Length,
long Precision,
long Scale);

Remarks

When using AlterDataType to convert the data type of an existing column to a new data type, the two data types must be compatible. For example, an int data type can be converted to a decimal data type, and a char data type can be converted to an nvarchar data type. However string data types cannot be converted to numeric data types.


试试 cm.AlterDataType("int");
Edifier0709 2006-08-17
  • 打赏
  • 举报
回复
have a try:

cm.AlterDataType("int",4,0, 0);
jinqinghai 2006-08-17
  • 打赏
  • 举报
回复
zxkid(Born in heaven, die in hell: everybody has sins !) 果然是牛人!

用cm.AlterDataType("int",-1, -1, -1); 正确修改字段成为int类型

非常感谢! 结贴!
zxkid 2006-08-17
  • 打赏
  • 举报
回复
cm.AlterDataType("int",-1, -1, -1);

用-1行不行?

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧