sql语句查询到的值如何赋值给一个变量

kerry030401 2008-08-05 02:49:37
现在我想把我用sql语句查询到的值赋值给一个变量如int,请问如何实现?
例如语句为“select A from table where years=2000 and months=2”
这样在数据库里查到的是A这个列的一个值,我想把这个值现在赋值给一个int变量,请问如何实现?
...全文
12255 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
潜水VIP 2012-04-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
C# code
sqlstr = "select A from table where years=2000 and months=2" ;
BaseService bc = new BaseService();
DataTable dt = bc.ExecuteDataTable(sqlstr );
foreach (DataRow dr in dt.Row……
[/Quote]

谢谢分享!
suyiming 2008-08-06
  • 打赏
  • 举报
回复
declear @sum int 
select @sum=cast((select A from table where years=2000 and months=2) as int)
print @sum
/*
测试
declare @sum int
select @sum=2
print @sum

====
输出

2
*/
suyiming 2008-08-06
  • 打赏
  • 举报
回复
select @sum=cast((select A from table where years=2000 and months=2) as int) 
print @sum[
/*
declare @sum int
select @sum=2
print @sum

====
输出

2
*/
suyiming 2008-08-06
  • 打赏
  • 举报
回复
[code=C#]declear @sum int
select @sum=cast((select A from table where years=2000 and months=2) as int)
print @sum[
/*
declare @sum int
select @sum=2
print @sum

====
输出

2
*/code]
财富实验室 2008-08-06
  • 打赏
  • 举报
回复
寻寻觅觅为的就是答案阿!
brooklyng60 2008-08-05
  • 打赏
  • 举报
回复
然后执行存储过程就可以得到结果啊,因为我们用到了Sqlhepler,一些简单的语句都用这了
public static int getNum(int year1,int month1 int year2,int month2)
{
return Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings["ConnectionString"].ToString(),"minus ",year1,month1,year2,month2));
}
kerry030401 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 brooklyng60 的回复:]
用个存储过程带两个参数啊,然后执行2次,如果有写死,可以带4个参数,直接返回相减的值也行
create proc minus
@year1 int,
@month1 int
@year2 int,
@month2 int
as
declare @num1 int
declare @sql1 nvarchar(2000)
set @sql1 ='select @num1=A from table where years=' + cast(@year1 as nvarchar(20))+ 'and month=' + cast(@month1 as nvarchar(20))
exec(@sql1)

declare @num2 int
declare @sql2 …
[/Quote]
这位兄弟 可以再详细点嘛?做完这个存储过程后再怎么操作?我对存储过程可以说是非常的不懂的,谢谢!
zhangxiaopin 2008-08-05
  • 打赏
  • 举报
回复

declare @aaa int

select @aaa=a from tablename where 1=1
brooklyng60 2008-08-05
  • 打赏
  • 举报
回复
用个存储过程带两个参数啊,然后执行2次,如果有写死,可以带4个参数,直接返回相减的值也行
create proc minus
@year1 int,
@month1 int
@year2 int,
@month2 int
as
declare @num1 int
declare @sql1 nvarchar(2000)
set @sql1 ='select @num1=A from table where years=' + cast(@year1 as nvarchar(20))+ 'and month=' + cast(@month1 as nvarchar(20))
exec(@sql1)

declare @num2 int
declare @sql2 nvarchar(2000)
set @sql2 ='select @num2=A from table where years=' + cast(@year2 as nvarchar(20))+ 'and month=' + cast(@month2 as nvarchar(20))
exec(@sql2)

select (@num1-@num2) as number

超人Q 2008-08-05
  • 打赏
  • 举报
回复
可以写成存储过程,在读取
超人Q 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yangpeiyu 的回复:]
declear @变量 int
select @变量=A from table where years=2000 and months=2
[/Quote]
kerry030401 2008-08-05
  • 打赏
  • 举报
回复
现在问题我需要2个sql语句都赋值给变量,再进行相减~请问这2个sql语句分别怎么放入sqldatareader?
ljqingas 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jiang_jiajia10 的回复:]


C# codesqlstr = "select A from table where years=2000 and months=2" ;
BaseService bc = new BaseService();
DataTable dt = bc.ExecuteDataTable(sqlstr );
foreach (DataRow dr in dt.Rows)
{
int n = dr["A"].ToString();

}



BaseService bc = new BaseService();
写一些数据库读取的代码,因为这是我自己定义的方法。
[/Quote]
柳泉青云 2008-08-05
  • 打赏
  • 举报
回复
SqlDataReader dr=db.getReader(“select A from table where years=2000 and months=2");
if(dr.Read())
{
if(dr["A"].ToString().Trim()!=null)
{
int intA=int.part(dr["A"].ToString());
}
}
pdsnet 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jacklygoodluck 的回复:]
declare @n int
select @n=A from table where years=2000 and months=2
[/Quote]
kerry030401 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jacklygoodluck 的回复:]
declare @n int
select @n=A from table where years=2000 and months=2
[/Quote]

请问这个是写在存储过程里面还是...?我不太清楚 对存储过程不是很了解~
并且我的years和months也是不定的,根据dropdownlist的选择来变的~
jiang_jiajia10 2008-08-05
  • 打赏
  • 举报
回复

sqlstr = "select A from table where years=2000 and months=2" ; 
BaseService bc = new BaseService();
DataTable dt = bc.ExecuteDataTable(sqlstr );
foreach (DataRow dr in dt.Rows)
{
int n = dr["A"].ToString();

}

BaseService bc = new BaseService();
写一些数据库读取的代码,因为这是我自己定义的方法。
ReyZhang 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jacklygoodluck 的回复:]
declare @n int
select @n=A from table where years=2000 and months=2
[/Quote]
fuda_1985 2008-08-05
  • 打赏
  • 举报
回复
declare @n int
select top 1 (@n=A) from table where years=2000 and months=2
这样写安全点!~
popcode 2008-08-05
  • 打赏
  • 举报
回复
把查询结果绑到DataTable,然后在DataTable里取出来
加载更多回复(4)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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