尝试与数据库进行通信时,我对如何将值作为参数传递(例如itemID)并获取具有此ID的记录感到困惑。
以下是我的存储过程:
ALTER PROCEDURE [dbo].[sp_lightItem]
(
@itemID INT
)
AS
BEGIN
SELECT [itemID],
[itemName],
[itemLocation],
[itemChBy]
FROM [dbo].[Item]
WHERE itemSystemType='E' and itemID=@itemID ORDER BY itemID DESC;
END
到目前为止这是我的c#代码..
public string LoadItemNew(int ItemID)
{
var acf = new AcFunctions();
var newstorevalue = SqlHelper.ExecuteDataset(acf.AcConn(), "sp_lightItem", ItemID);
}
正如您在存储过程中看到的,我想要的是取回这4个元素:
[itemID],[itemName],[itemLocation],[itemChBy]
不幸的是我不知道如何让他们回来/如何在c#函数中调用它们 欢迎任何帮助。
答案 0 :(得分:3)
我没有足够的回购评论,你能否提供
的定义AcFunctions();
我相信你必须返回ConnectionString
试试这个
public string LoadItemNew(int ItemID)
{
var acf = new AcFunctions();
var newstorevalue = SqlHelper.ExecuteDataset(acf.AcConn(), "sp_lightItem", new SqlParameter ("@itemID",ItemID));
}
修改1 试试这个
public string LoadItemNew(int ItemID)
{
List<string> testList = new List<string>();
var acf = new AcFunctions();
var newstorevalue = SqlHelper.ExecuteReader(acf.AcConn(), "sp_lightItem", new SqlParameter ("@itemID",ItemID));
if(newstorevalue.HasRows)
{
while(newstorevalue.Read())
{
testList.Add(newstorevalue["itemID"].ToString());
testList.Add(newstorevalue["itemName"].ToString());
testList.Add(newstorevalue["itemLocation"].ToString());
testList.Add(newstorevalue["itemChBy"].ToString());
}
}
}
答案 1 :(得分:2)
您可以尝试这种方法,我将使用数据传输对象来保存从数据库中检索的数据,并使用执行DataReader 进行阅读。
首先,您需要创建一个DTO类,我将其称为 LightItemDTO
public class LightItemDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public string ChangedBy { get; set; }
}
注意:如何知道属性的类型,您可以参考以下链接:SQL Server Data Type Mappings
现在,我将使用ADO.NET执行存储过程以从数据库中获取数据
public IEnumerable<LightItemDTO> GetLightItem(string itemText, string sqlConnectionString)
{
var results = new List<LightItemDTO>();
using (var con = new SqlConnection(sqlConnectionString))
{
using (var cmd = new SqlCommand("sp_lightItem", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ItemID", SqlDbType.VarChar).Value = itemText;
con.Open();
using (var reader = cmd.ExecuteReader())
{
results.Add(new LightItemDTO
{
Id = Convert.ToInt32(reader["itemID"]),
Name = reader["itemName"].ToString(),
Location = reader["itemLocation"].ToString(),
ChangedBy = reader["itemChBy"].ToString()
});
}
}
}
return results;
}
使用DataReader是高性能的最佳实践。 ADO.NET是实现此任务的手动方式,您可以使用一些ORM框架来实现更轻松,例如:Entity Framework,Dapper.NET ......
答案 2 :(得分:0)
您可以使用以下参数执行存储过程:
using (SqlConnection con = new SqlConnection(dc.Con)) {
using (SqlCommand cmd = new SqlCommand("sp_lightItem", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ItemID", SqlDbType.VarChar).Value = itemId.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
答案 3 :(得分:0)
首先将Commandtype
设置为stored procedure
,该过程将返回一些您将保存在dataset
中的数据,然后将数据集返回到您想要填充的位置数据
public DataSet LoadItemNew(int ItemID)
{
var acf = new AcFunctions();
return DataSet ds = SqlHelper.ExecuteDataset(acf.AcConn(),CommandType.StoredProcedure, "sp_lightItem",new SqlParameter("@itemID" ItemID);
}
答案 4 :(得分:0)
你可以尝试这样..
public string LoadItemNew(int ItemID)
{
var acf = new AcFunctions();
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@itemID", ItemID));
DataSet Ds = SqlHelper.ExecuteDataset(acf.AcConn(),CommandType.StoredProcedure, "sp_lightItem" , parameters.ToArray());
return "ok";
}