使用datagridview中当前选定的行值查询数据库 [英] Query database using currently selected row value in datagridview
本文介绍了使用datagridview中当前选定的行值查询数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用所选行的列中的值查询数据库?
我尝试了什么:
我已经尝试过的是查询数据库但它需要时间,因为有些值很大所以我创建了另一个表具有巨大价值。
Hi,
is it possible to query a db using the value in a column of the selected row?
What I have tried:
What i have tried already is query the db but it takes along time because some values are huge so i have created another table with the huge value.
推荐答案
尝试:
Try:
if (myDataGridView.SelectedRows.Count > 0)
{
DataGridViewRow row = myDataGridView.SelectedRows[0];
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable WHERE myColumn=@VALUE", con))
{
cmd.Parameters.AddWithValue("@VALUE", row.Cells[0].Value);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int id = (int)reader["Id"];
string desc = (string)reader["description"];
Console.WriteLine("ID: {0}\n {1}", id, desc);
}
}
}
}
}
这篇关于使用datagridview中当前选定的行值查询数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文