如果存在查询则出错 [英] Error in if exists query

查看:83
本文介绍了如果存在查询则出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此代码中有错误我找不到它



此行中的错误4(字符串查询=如果存在(选择宠物的头像) Skey = @Skey);)



I have error in this Code i can''t Find it

Error in this line 4 (string query = "if exists(select Avatar from pets Where Skey = @Skey);)

string connectionString = WebConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string query = "if exists(select Avatar from pets  Where Skey = @Skey);
query += " begin select Avatar from pets  Where Skey = @Skey end";
query += " else  begin select Avatar from pets  Where Skey = '1' end" ;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@Skey", Request.QueryString["ID"]);
object ReturnImage = cmd.ExecuteScalar();
byte[] photo = (byte[])ReturnImage;
Response.BinaryWrite(photo);
con.Close();

推荐答案

非常清楚:你忘记在行尾(分号前)关闭报价。

认为这是你帖子上的拼写错误;但它似乎是真正的问题。
Pretty clear : you forgot to close the quote at the end of line (before semicolon).
Thought it was a typo on your post ; but it appears it''s the real problem.


更正了你的代码..



Corrected your code..

string connectionString = WebConfigurationManager.ConnectionStrings["conString"].ConnectionString;
          SqlConnection con = new SqlConnection(connectionString);
          string query = "if ((select COUNT(Avatar) from pets  Where Skey = @Skey)> 0)";
          query += " begin select Avatar from pets  Where Skey = @Skey end";
          query += " else  begin select Avatar from pets  Where Skey = '1' end" ;
          SqlCommand cmd = new SqlCommand(query, con);
          con.Open();
          cmd.Parameters.AddWithValue("@Skey", Request.QueryString["ID"]);
          object ReturnImage = cmd.ExecuteScalar();
          byte[] photo = (byte[])ReturnImage;
          Response.BinaryWrite(photo);
          con.Close();





还有一件事-----------



其中Skey =''1''结束;



应该是



其中Skey = 1结束;



And one more thing-----------

Where Skey = ''1'' end" ;

should be

Where Skey = 1 end" ;


这篇关于如果存在查询则出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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