[英]load data from sql server table and populate textboxes using dapper
i pass selected ProductID as a string to retrieve data from row with the same ProductID我将选定的 ProductID 作为字符串传递,以从具有相同 ProductID 的行中检索数据
private void modifyButton_Click(object sender, EventArgs e)
{
if (productsTable.SelectedRows.Count > 0)
{
int selectedrowindex = productsTable.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = productsTable.Rows[selectedrowindex];
string ProductID = Convert.ToString(selectedRow.Cells["ProductID"].Value);
ProductModel product = db.LoadProduct(ProductID);
}
}
then i try to load data and get it as a ProductModel to populate textboxes with it然后我尝试加载数据并将其作为 ProductModel 来填充文本框
public ProductModel LoadProduct(string productId)
{
ProductModel output;
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(CnnString(db)))
{
output = connection.Query
(@"SELECT * FROM Products WHERE ProductID = @ProductID", new { ProductID = productId }).Single();
}
return output;
}
i get error Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: "Cannot implicitly convert type object to SalesManager.Models.ProductModel我收到错误 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“无法将类型 object 隐式转换为 SalesManager.Models.ProductModel
i tried before with a List and it seem to work, but then i dont know to to get objects and convert the to strings.我之前尝试过使用列表,它似乎可以工作,但是我不知道要获取对象并将其转换为字符串。
You need to use the generic overload of Query, eg您需要使用 Query 的通用重载,例如
output = connection.Query<ProductModel>(@"SELECT * FROM Products WHERE ProductID = @ProductID", new { ProductID = productId }).Single();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.