4 回答

TA貢獻1825條經(jīng)驗 獲得超4個贊
這里:
string value = "me@live.com";
DataView dv = new DataView(dt);
dv.RowFilter = "email = "+value;
你正在做的是:
DataView dv = new DataView(dt);
dv.RowFilter = "email = me@live.com";
因此,您缺少包含文字字符串的撇號。你應該改用這個:
string value = "'me@live.com'";
所以得到的過濾器是:
dv.RowFilter = "email = 'me@live.com'";
由于您可能從其他地方收到電子郵件(而不是硬編碼在文件中),如果您使用的是 C# 6.0 或更高版本,則可以改為:
string value = "me@live.com";
DataView dv = new DataView(dt);
dv.RowFilter = $"email = '{value}'";

TA貢獻1860條經(jīng)驗 獲得超8個贊
嘗試這個。
using (SqlConnection connection = new SqlConnection(conn))
{
connection.Open();
var query = "select * from mytable WHERE email = 'me@live.com'";
SqlCommand mycommand = new SqlCommand(query,connection);
// SqlDataReader dataReader = mycommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(mycommand.ExecuteReader());
DataView dv = new DataView(dt);
dataGridView1.DataSource = dv.ToTable();
// dataGridView1.DataSource = dt;
}

TA貢獻1877條經(jīng)驗 獲得超1個贊
為什么在從數(shù)據(jù)庫中獲取整個數(shù)據(jù)后進行過濾?只需在查詢本身中過濾它:
select * from mytable where email = 'me@live.com'

TA貢獻1810條經(jīng)驗 獲得超5個贊
試試這個,讓我知道它是否有效
using (SqlConnection connection = new SqlConnection(conn))
{
connection.Open();
SqlCommand mycommand = new SqlCommand("select * from mytable",connection);
// SqlDataReader dataReader = mycommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(mycommand.ExecuteReader());
string value = "me@live.com";
DataView dv = new DataView(dt);
dv.RowFilter = "email LIKE %'" + value + "'%";
dataGridView1.DataSource = dv.ToTable();
// dataGridView1.DataSource = dt;
}
- 4 回答
- 0 關注
- 175 瀏覽
添加回答
舉報