MSSQL中,将text,ntext转换为int型数据

先给你看这段代码:

 

 1  < SCRIPT language = " javascript "  type = " text/javascript " >
 2  <!--
 3       for ( var i  =   0 ; i  <=   9 ; i ++  )
 4      {
 5      document.writeln( ' <input type="radio" name="face" value=" ' +  i  + ' '   +  (i  ==   2   ?   ' checked '  :  '' +   '  /><img src="images/p ' +  i  + ' .gif" alt="" /> '   +   ' &nbsp;&nbsp;&nbsp;&nbsp; ' );
 6        ( i  ==   4 ) document.writeln( " <br /> " );
 7       }
 8    -->
 9    </ SCRIPT >
10 

 

用来对表情的编号实现。

后台获取表情编号:

 

1  protected   void  SqlDataSource1_Inserting( object  sender, SqlDataSourceCommandEventArgs e)
2      {
3          e.Command.Parameters.Add( new  SqlParameter( " @face " , Convert.ToDecimal(Request.Form[ " face " ].ToString())));
4          e.Command.Parameters.Add( new  SqlParameter( " @posttime " , System.DateTime.Now.ToString()));
5      }

 

 

 

要知道,数据库里face字段的类型是int。

这样可以转换成功。

但是Convert.Toint32(Request.Form["face"].ToString())会提示,ntext与int转换错误。

原来Request.Form["face"].ToString()是ntext类型啊,我晕。。。。

 

网上查了资料:

 

MSSQL中,企业管理器不允许将text或ntext型数据直接转换为int型,有时会对我们造成不便。

其实用一种简单的方法,就可以将text型数据转换为int型。

先把text或ntext型的数据都转换为nvarchar型,

然后再把nvarchar型转换为int型就可以了。

 

转Decimal也一样

 

http://www.topfisher.com/phpbbs/simple/index.php?t28.html

 

你可能感兴趣的:(MSSQL)