C# 返回值和bool
我试图返回与bool响应一起单独查询的值,但我无法正确获取它。我没有收到任何错误,除非搜索发现了什么。值Subscriberkey从另一个类传递到该类中。我对返回的任何更改都会破坏代码并添加类似的内容C# 返回值和bool,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我试图返回与bool响应一起单独查询的值,但我无法正确获取它。我没有收到任何错误,除非搜索发现了什么。值Subscriberkey从另一个类传递到该类中。我对返回的任何更改都会破坏代码并添加类似的内容 if (var == null) { return true; } return SubscriberQuery.LookupSubProfile(querysubscriber); 不起作用 public static bool LookupSubProfile (Subscrib
if (var == null)
{
return true;
}
return SubscriberQuery.LookupSubProfile(querysubscriber);
不起作用
public static bool LookupSubProfile (SubscriberProfileQuery subscriber)
{
try
{
var connString = "Server = Server\\SQLEXPRESS; initial catalog = Stuff; integrated security = True;";
var query = "SELECT * FROM Subscriber WHERE SubscriberKey = '@SubscriberKey'";
query = query.Replace("@SubscriberKey", subscriber.Subscriberkey);
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Dispose();
conn.Close();
}
return false;
}
catch
{
return false;
}
你可以用两种方法来做: 1.使用out参数并将其发送给方法作为参考:
public static bool LookupSubProfile (SubscriberProfileQuery subscriber,out int someValueToReturn)
{
...
return true;
}
其用途是(一个int):
2.您可以返回元组:
(bool, int) LookupSubProfile()
{
//...
return ( true, 3);
}
你可以像这样使用它:
var (value, result) = LookupSubProfile();
让我们尝试修复该代码中存在的所有问题:
ExecuteNonQuery
不适用于SELECT
,因为这正是一个查询using
语句已经调用了Dispose()
,它调用了Close()
,所以这些都不是必需的bool
,如果未找到任何值,则返回null
,如果出现异常,则抛出异常你能描述一下你为什么要退货吗?为什么不在失败时返回null,或者在失败时抛出异常呢?
command.ExecuteNonQuery()
对于select
操作?我还注意到您的代码容易受到注入攻击。我也不明白您为什么要通过执行非查询来执行查询。基本上,这些对我来说都没有意义;你能更好地解释你想做什么吗?关键字可能对你有用。自己的类不是比元组更好吗?出于好奇,为什么不为元组创建一个DTO呢?我提供了一种可能的方法,当OP显然不知道基本原理时,使用c#7(值,结果)
,这是在浪费他们的时间试图理解它到底是什么out
只是教授过时的做法,除非你有很好的理由这样做,否则不应该使用这些做法。正如@EricLipper所说,在这种情况下,只返回null
要容易得多。此外,这并没有解决这个问题中的多个问题,这些问题甚至与tanks@CamiloTerevinto无关。这回答了我的很多问题,我认为我走的是正确的道路。至少我会犯错误,我相信我能解决。你帮了大忙。2+分你介意帮我再做一件事吗。我把数字2改成了我以前拥有的,因为VS在抱怨,但我得到的是“必须声明标量变量”@SubscriberKey“。我试着把它转换成字符串,但还是出现了那个错误。如果表中有帮助,则为GUID。任何ideas@user3266908,如果参数值是GUID,请指定SqlDbType.UniqueIdentifier
以匹配数据库列类型。另外,请注意,ExecuteScalar
返回查询返回的第一行的第一列的值,并丢弃其余的值。在这种情况下,更适合指定该列名,而不是*
。
var (value, result) = LookupSubProfile();
public static string LookupSubProfile (SubscriberProfileQuery subscriber)
{
try
{
var connString = "Server = Server\\SQLEXPRESS; initial catalog = Stuff; integrated security = True;";
var query = "SELECT * FROM Subscriber WHERE SubscriberKey = @SubscriberKey";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(query, conn))
{
// 2: add parameters
command.Parameters.Add("SubscriberKey", SqlDataType.VarChar).Value = suscriber.SuscriberKey;
// 1. use ExecuteScalar/ExecuteReader,
// you will need to define what exactly you need here
var result = command.ExecuteScalar();
if (result != null)
{
// 4. return the result
return (string)result;
}
}
// 3. remove unneeded calls
}
// 4. return null if nothing was found
return null;
}
catch
{
// 4: throw the error, log if possible
throw;
}
}