我正在尝试运行存储过程并将结果添加到数据表中。我的存储过程按原样执行,如果我查询表格结果存储在SSMS中,那么就是准确的结果。但是,我的代码每次都会产生numberofrecords = 0
!
我在下面的语法中设置错误了什么?
using (conn = new SqlConnection(SQLConn))
{
using (cmd = new SqlCommand(storedprocname, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
cmd.Parameters.Add("d1", SqlDbType.Date, 100);
cmd.Parameters.Add("d2", SqlDbType.Date, 100);
cmd.Parameters["@Name"].Value = cboNames.Text.ToString();
cmd.Parameters["d1"].Value = dtpd1.Value.ToString("MM/dd/yyyy");
cmd.Parameters["d2"].Value = dtpd2.Value.ToString("MM/dd/yyyy");
cmd.Parameters.Add("@Dolla", SqlDbType.VarChar, 100);
cmd.Parameters["@Dolla"].Value = cboDolla.Text.ToString();
using (da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
int numberOfRecords = 0;
numberOfRecords = dt.Select().Length;
MessageBox.Show(numberOfRecords.ToString());
}
这些是我的类变量声明:
public static SqlCommand cmd;
public static SqlDataAdapter da;
public static DataSet ds = new DataSet();
public static DataTable dt = new DataTable();
修改
这是我的存储过程,产生大约32行
ALTER Procedure [dbo].[TestParamQuery]
(
@Name varchar(max)
,@d1 varchar(100)
,@d2 varchar(100)
,@dolla varchar(500)
)
As
Select
EmployeeName
,EmployeeNumber
,CAST(hiredate As Date) [hire date]
,saleamount
FROM [TestDB].[dbo].[SalesFigs]
WHERE employeename = @Name
AND hiredate between @d1 AND @d2
AND saleamount >= @dolla
编辑2
这就是我执行存储过程的方式,以确保它直接在SSMS中返回我想要的结果
USE [TestDB]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[TestParamQuery]
@Name = N'Sam Wise',
@d1 = N'03/01/2016',
@d2 = N'01/30/2016',
@Dolla = N'1000'
SELECT 'Return Value' = @return_value
GO
答案 0 :(得分:0)
不幸的是,SqlCommand没有简单的方法或属性来将命令转换为字符串,包括所有参数和值。我过去使用过这样的方法来简化调试命令:
public static string PrintCommand(this SqlCommand command){
if (command == null) throw new ArgumentNullException("command");
var sb = new StringBuilder();
sb.AppendLine(command.CommandText);
foreach (var p in command.Parameters){
sb.AppendLine("\t" + p.ParameterName + ": " + p.Value);
}
return sb.ToString();
}
它应输出如下字符串:
"dbo.MyCommandName
@Name: myNameParameterValue
d1: 01/01/2016
d2: 02/02/2016
@Dolla: myDollaValue"
然后,您可以像这样调用它,并检查step-thru调试器中的值。
using (conn = new SqlConnection(SQLConn))
using (cmd = new SqlCommand(storedprocname, conn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
cmd.Parameters.Add("d1", SqlDbType.Date, 100);
cmd.Parameters.Add("d2", SqlDbType.Date, 100);
cmd.Parameters["@Name"].Value = cboNames.Text.ToString();
cmd.Parameters["d1"].Value = dtpd1.Value.ToString("MM/dd/yyyy");
cmd.Parameters["d2"].Value = dtpd2.Value.ToString("MM/dd/yyyy");
cmd.Parameters.Add("@Dolla", SqlDbType.VarChar, 100);
cmd.Parameters["@Dolla"].Value = cboDolla.Text.ToString();
//Get a text representation here:
var text = cmd.PrintCommand();
//Put a breakpoint here to check the value:
using (da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
int numberOfRecords = 0;
numberOfRecords = dt.Select().Length;
MessageBox.Show(numberOfRecords.ToString());
}
答案 1 :(得分:0)
不确定为什么你无法获得价值观。但无论如何请尝试这种方法:
using (conn = new SqlConnection(SQLConn))
using (cmd = new SqlCommand(storedprocname, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", cboNames.Text);
cmd.Parameters.AddWithValue("d1", dtpd1.Value.ToShortDateString();
cmd.Parameters.AddWithValue("d2", dtpd2.Value.ToShortDateString();
cmd.Parameters.AddWithValue("@Dolla", cboDolla.Text);
using (da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
var numberOfRecords = dt.Rows.Count;
MessageBox.Show(numberOfRecords);
}