.net中string类型转换成int类型

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:23:07
string sq4 = "select count(*) from [gdzc1] where zaiyong='是'or weiqiyong='是'";

int x = Convert.ToInt32(TextBox1.Text);
int y = Convert.ToInt32(sq4);//输入字符串的格式不正确。
运行时总提示上面的错误,请帮忙!

(1)低容量小的类型自动转换为容量大的数据类型:数据类型按容量大小排序为: byte,short,char->int->long->float->double
(2)大容量的类型转化成小容量的数据时,需要强制转化.如:float a=45.0F, 转化成int行时为:int b=(float)a;
但是我们在平时的输入或输出时,经常使用String引用类型的字符串数据,其实String类型的数据和int(float,double等)型数据之间也可以相互转化. 其中把String类型转化成int类型有两种方法:(同理,转化成float,long等数值型也雷同)
(1)int choose1=Integer.parseInt(choice);
(2)int choose2=Integer.valueOf(choice).intValue();
把int类型转化成String类型有三种方法:如:int a=100;
(1)String s=Integer.toString(100);
(2 )String s=String.valueOf(100);
(3)String s=""+a;

int y = Convert.ToInt32(sq4);//你需要转换的SQL查询后的结果,而不是这个语句

SqlCommand cmd = new SqlCommand();
cmd.CommandText = sq4;
cmd.Connection = 你的数据库连接;
数据库连接.Open();
int y = Convert.ToInt32(cmd.ExecuteScalar());
数据库连接.Close();

string sq4 = "select count(*) from [gdzc1] where zaiyong='是'or weiqiyong='是'"