再次SQL!就是行不通 [英] SQL again! It just doesn't work

查看:105
本文介绍了再次SQL!就是行不通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我从SQL数据库读取数据时遇到问题.

语法如下:

从属性中选择Max(Property_ID)

我在SQL Server Management Studio Express中尝试了它,但是它可以工作,但是当我在Visual Web Developer中尝试执行它时,它给我一个错误.


这里的代码:

Hi,

I am having a problem reading data from SQL database.

The syntax is like such:

SELECT Max(Property_ID) FROM Property

I tried it in SQL Server Management Studio Express and it works, but when I tried to execute it in Visual Web Developer..., it gets me an error.


Here the code:

string SelectProperty_ID = "SELECT Max(Property_ID) FROM Property";
        SqlCommand property_id_Command = new SqlCommand(SelectProperty_ID, connect1);
        SqlDataReader readProperty_ID;
        
        try
        {
            connect1.Open();//make connection
            readProperty_ID = property_id_Command.ExecuteReader();
            readProperty_ID.Read();
            lblSpecial.Text = readProperty_ID["Property_ID"].ToString();
            readProperty_ID.Close();
        }
        catch (Exception error) { Label1.Text = "Error Readin.......etc



顺便说一句,具有readProperty_ID.Read();有或没有,都不好.

任何人都可以帮忙.



BTW, having readProperty_ID.Read(); with or without while NO good.

Could anyone help please.

推荐答案

在不进行深入研究和编码的情况下,我有两个建议可以尝试:

1).在SQL语句中的名称上使用别名
例如从属性中选择MAX(Property_ID)Property_ID"
您很可能会收到错误消息,因为您正在使用aggergate函数,因为在阅读器中实际上不存在Property_ID

2).不必执行ExecuteReader,而是执行ExecuteScalar,因为您只有一个返回值,并将返回的OBJECT类型转换为整数.
Without digging into this and coding it, I have two suggestions you can try:

1). Use an alias on on the name in your SQL statement
e.g. "SELECT MAX(Property_ID) Property_ID FROM Property"
Most likely, you''re getting an error because Property_ID does not actually exist in the reader, since you''re using an aggergate function

2). instead of doing ExecuteReader do ExecuteScalar, since you have a single return value, and cast the return OBJECT type into an integer.


感谢您的答复.

但是,请您告诉我这是怎么回事,
Thanks for your replies.

But, can you please tell me how this,
SELECT MAX(Property_ID) \"Property_ID\" FROM Property


,可以在C#代码中使用

而在普通的SQL查询中应该是这样的


, works in c# code

while in a normal SQL query should be like this

SELECT MAX(Property_ID) "\Property_ID\" FROM Property


对您的代码进行此更改,您将获得所需的结果..
make this change to your code and u will get desired result..
try
        {
            connect1.Open();//make connection
            readProperty_ID = property_id_Command.ExecuteReader();
            while(readProperty_ID.Read())
            {
                lblSpecial.Text = readProperty_ID["Property_ID"].ToString();
            }
       readProperty_ID.Close();
 
       }
        catch (Exception error) { Label1.Text = "Error Readin.......etc



快乐编码



happy coding


这篇关于再次SQL!就是行不通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆