问题描述
你好
这是我的代码,用于显示列表框中数据库中的数据
数据库有很多很多行
将这段代码放在
hello
this my code to display data from database in listbox
the database have many many line
put this code get the first one in the database in
WHERE IDName= @IDName
中的数据库中的第一个代码中并且我需要显示所有行,其中
and I need to Display ALL line where
WHERE IDName= @IDName
try
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents\WinDB.mdf;Connect Timeout=30;User Instance=True;Integrated Security=SSPI";
SqlDataAdapter da = new SqlDataAdapter();
connection.Open();
SqlDataReader Dr;
SqlCommand command1 = new SqlCommand("SELECT IP FROM ConnectedDevice WHERE IDName= @IDName", connection);
command1.Parameters.Add(new SqlParameter("@IDName", name));
command1.ExecuteNonQuery();
Dr = command1.ExecuteReader();
ConnDevic.Items.Clear();
ConnDevic.BeginUpdate();
while (Dr.Read())
{
ConnDevic.Items.Add(Dr.GetString(0).ToString());
}
ConnDevic.EndUpdate();
Dr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
我该怎么做! :S
在此先感谢
how I can do that !! :S
thanks in advance
推荐答案
SELECT IP
FROM ConnectedDevice
如果您的IDName
字段是文本字段,请尝试:
If your IDName
field is a text field, try:
SELECT IP
FROM ConnectedDevice
WHERE IDName Like @IDName + '%'
但是,如果您的IDName
字段是具有唯一值的数字字段,则您的查询将始终仅返回1个值(记录).
But if your IDName
field is a number field with the unique values, your query will always return only 1 value (record).
SELECT IP FROM ConnectedDevice WHERE IDName= @IDName
通过将@IDName替换为正确的IdName并执行它.请参见结果
2)将此代码替换为
by replacing @IDName with correct IdName and execute it.see the result
2) Replace your code with this
try
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents\WinDB.mdf;Connect Timeout=30;User Instance=True;Integrated Security=SSPI";
SqlDataAdapter da = new SqlDataAdapter();
connection.Open();
SqlDataReader Dr;
SqlCommand command1 = new SqlCommand("SELECT IP FROM ConnectedDevice WHERE IDName= @IDName", connection);
command1.Parameters.Add(new SqlParameter("@IDName", name));
command1.ExecuteNonQuery();
Dr = command1.ExecuteReader();
ConnDevic.Items.Clear();
// ConnDevic.BeginUpdate();
while (Dr.Read())
{
ConnDevic.Items.Add(Dr.GetString(0).ToString());
}
// ConnDevic.EndUpdate();
Dr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
这里也将@IDName替换为您第一次尝试的值.查看结果.
here also replace @IDName with the value one you have tried in 1st time.see the result.
这篇关于将数据库显示到列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!