如何使用c#在变量处保存查询?
我有这个SP:如何使用c#在变量处保存查询?,c#,asp.net,sql-server-2008-r2,C#,Asp.net,Sql Server 2008 R2,我有这个SP: create procedure SP_Filas (@fact int) as select COUNT(C.id_art) from cat_articulo C inner join det_factura D on C.id_art = D.id_art inner join factura F on F.id_fact = D.id_fact inner join cat_cliente B on B.id_cliente = F.id_cliente w
create procedure SP_Filas
(@fact int)
as
select COUNT(C.id_art) from cat_articulo C inner join det_factura D on C.id_art = D.id_art
inner join factura F on F.id_fact = D.id_fact
inner join cat_cliente B on B.id_cliente = F.id_cliente
where D.id_fact = @fact
例如,此SP将抛出3。
如何将3保存到C#Asp.NEt的int变量中?我意识到您可能是C#的新手(可能是一般编程),但您有许多问题需要查找和解决-我建议重新开始,一次构建一个程序,使用调试器(或单元测试)验证结果。现在看来,您正在将一堆代码放在一起(可能是从其他来源复制和粘贴的),并试图解决所有问题。这种方法将导致添加补丁(稍后将很难理解)来解决逻辑问题,而不是逐块系统地构建程序 也就是说 我看到几个问题:
- 您混合了
和AddWithValue
的语法。使用以下任一选项:Add
或cmd.Parameters.Add("@fact", SqlDbType.Int).Value = fact;
- 实际上并不是从表中提取数据。您需要在
上使用列名索引器:DataRow
total_fact = Convert.ToString(row["total_fact"]); ^--^
- 您总是只读取第一行(
)DataRow row=dt.Rows[0];
- 您为每个项目调用了两次
——一次在循环定义中,另一次在循环结束时i++
- 为什么要将值提取到固定大小为50的数组中?为什么不保留
或使用数据表
列表
数组
来存储这些大量数据。创建一个自定义的类
,其中包含如下所需的成员,并将每个实例添加到IEnumarable
集合中
public class MyClass
{
public string id_art { get; set; }
public string nombre { get; set; }
public string precio { get; set; }
}
SqlConnection cnx;
String cadCnx = "Here goes my credentials"
cnx = new SqlConnection(cadCnx);
String query = "exec SP_Datos_PDF @fact";
SqlCommand cmd = new SqlCommand(query, cnx);
cmd.Parameters.AddWithValue("@fact", SqlDbType.Int).Value = fact;
cnx.Open();
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i <= filas; i++)
{
DataRow row = dt.Rows[0];
var item = new MyClass();
item.id_art = Convert.ToString("id_art");
item.nombre = Convert.ToString("nombre");
item.precio = Convert.ToString("precio");
Items.Add(item);
i++;
}
}
公共类MyClass
{
公共字符串id_art{get;set;}
公共字符串nombre{get;set;}
公共字符串precio{get;set;}
}
SqlConnection-cnx;
String cadCnx=“我的凭证在这里”
cnx=新的SqlConnection(cadCnx);
String query=“exec SP_Datos_PDF@fact”;
SqlCommand cmd=新的SqlCommand(查询,cnx);
cmd.Parameters.AddWithValue(“@fact”,SqlDbType.Int).Value=fact;
cnx.Open();
DataTable dt=新的DataTable();
SqlDataAdapter adp=新的SqlDataAdapter(cmd);
自动进料(dt);
如果(dt.Rows.Count>0)
{
对于(int i=0;i),首先你应该说“它不工作”意思是…有错误吗?有什么问题吗?它没有将SP的结果保存在我的DataTable中,因此我无法将查询的值保存在变量D中:仍然不工作。我需要的是将来自我的DB的值(例如,id_art=5、6、7)保存在我的数组id_art[]但是它没有保存任何东西D:@RalphVB查看我的编辑。由于@fact
的值不正确,您可能没有得到任何行结果。您知道如何使用调试器吗?谢谢您的更正:p(我很遗憾,因为双i++)我需要保存到数组中,因为我要将从数据库中提取的所有信息打印成PDF。但仍然没有保存。让我编辑我的问题,以获得新代码:)您应该仍然能够使用列表
或数据表
迭代并保存到PDF中。如果您仍然计划使用50的固定索引长度,那么您应该确保filas
的最大值为49,否则您将遇到索引超出范围异常。您的代码没有回答问题“当我试图打印全部事实时。。。"并且没有帮助,因为它仍然包含D Stanley提到的问题。我没有在MyClass模型中添加total_事实。您可以添加此属性,并执行与在旧代码段中所做的相同的操作。请在下一次投票之前正确查看答案。虽然OP询问的是total_事实,而不是模型,但您的代码的主要问题不在total_fact.您的代码不起作用-请参见以下答案:1)cmd.Parameters.AddWithValue(“@fact”,SqlDbType.Int)。Value=fact;2)item.id_art=Convert.ToString(“id_art”);3)DataRow=dt.Rows[0];4)i++;这对我来说非常专业,但我已经解决了我的问题。SQLServer上计算行的SP不工作,这就是它不保存的原因。非常感谢,兄弟。
public class MyClass
{
public string id_art { get; set; }
public string nombre { get; set; }
public string precio { get; set; }
}
SqlConnection cnx;
String cadCnx = "Here goes my credentials"
cnx = new SqlConnection(cadCnx);
String query = "exec SP_Datos_PDF @fact";
SqlCommand cmd = new SqlCommand(query, cnx);
cmd.Parameters.AddWithValue("@fact", SqlDbType.Int).Value = fact;
cnx.Open();
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i <= filas; i++)
{
DataRow row = dt.Rows[0];
var item = new MyClass();
item.id_art = Convert.ToString("id_art");
item.nombre = Convert.ToString("nombre");
item.precio = Convert.ToString("precio");
Items.Add(item);
i++;
}
}