小编ank*_*311的帖子

在C#构造函数头中使用冒号

下面是一个struct名为Complex 的构造函数,它有两个成员变量,Real和Imaginary:

public Complex(double real, double imaginary) : this()
{
 Real = real;
 Imaginary = imaginary;
}
Run Code Online (Sandbox Code Playgroud)

函数头中冒号后的部分有什么用?

c# constructor

15
推荐指数
3
解决办法
1万
查看次数

在C#中访问SQL Server存储过程输出参数

我有一个简单的SQL Server存储过程:

ALTER PROCEDURE GetRowCount

(
@count int=0 OUTPUT
)

AS
Select * from Emp where age>30;
SET @count=@@ROWCOUNT;

RETURN
Run Code Online (Sandbox Code Playgroud)

我试图访问以下C#代码中的输出参数:

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=answers;Integrated Security=True";

SqlCommand cmd = new SqlCommand();
cmd.Connection = con;

cmd.CommandText = "GetRowCount";
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@count", SqlDbType.Int));
cmd.Parameters["@count"].Direction = ParameterDirection.Output;
con.Open();
SqlDataReader reader=cmd.ExecuteReader();
int ans = (int)cmd.Parameters["@count"].Value;
Console.WriteLine(ans);
Run Code Online (Sandbox Code Playgroud)

但是在运行代码时,会在代码的倒数第二行抛出NullReferenceException.我哪里错了?提前致谢!

PS我是SQL Procedures的新手,所以我参考了这篇文章.

c# t-sql sql-server stored-procedures

3
推荐指数
1
解决办法
3万
查看次数

初始化存储过程输出参数

我有一个简单的SQL Server存储过程:

ALTER PROCEDURE GetRowCount

(
@count int=0 OUTPUT
)

AS
Select * from Emp where age>30;
SET @count=@count+@@ROWCOUNT;

RETURN
Run Code Online (Sandbox Code Playgroud)

我正在尝试初始化并访问以下C#代码中的输出参数:

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=answers;Integrated Security=True";

SqlCommand cmd = new SqlCommand();
cmd.Connection = con;

cmd.CommandText = "GetRowCount";
cmd.Parameters.Add(new SqlParameter("@count", SqlDbType.Int));
cmd.Parameters["@count"].Direction = ParameterDirection.Output;
con.Open();
cmd.Parameters["@count"].Value=5;
cmd.ExecuteNonQuery();

int ans = (int)(cmd.Parameters["@count"].Value);
Console.WriteLine(ans);
Run Code Online (Sandbox Code Playgroud)

但是在运行代码时,会在代码InvalidCastException的倒数第二行抛出一个(我调试并检查了Value属性中没有返回任何内容).

如何在代码中正确初始化输出参数?提前致谢!

c# t-sql sql-server stored-procedures

3
推荐指数
1
解决办法
2543
查看次数

Include方法的作用是什么

我正在尝试学习项目的LINQ.但我对Include方法感到有些困惑.它是干什么用的?相册变量的以下两个初始化之间有什么区别?

var album = storeDB.Albums.Include("Artist").ToList();
var album = storeDB.Albums.ToList();
Run Code Online (Sandbox Code Playgroud)

c# linq-to-entities

3
推荐指数
1
解决办法
116
查看次数

二进制搜索以获取第一个整数的位置> =搜索的整数

在排序数组中,我需要找到第一个整数的位置> =给定的整数(如果数组中不存在这样的整数,则应返回-1).二元搜索的以下修改是否正确解决问题?我使用了不少测试用例来验证功能,但仍想确认.

n =不.数组中的元素

ele =要搜索的整数

int binarySearch(int a[],int n,int ele)
{
  int lower=0;
  int upper=n-1;

  int mid;
  int pos=-1;
  while(lower<=upper)
  {
    mid=(lower+upper)/2;
    if (a[mid]==ele)
    {
        while(mid>=0 && a[mid]==ele)
            mid--;
        return mid+1;
    }
    else if(a[mid]<ele)
        lower=mid+1;
    else
    {
        pos=mid;
        upper=mid-1;
    }
  }
  return pos;
}
Run Code Online (Sandbox Code Playgroud)

c binary-search

1
推荐指数
1
解决办法
539
查看次数