将数据库显示到列表框中 [英] display database into listbox

查看:110
本文介绍了将数据库显示到列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

这是我的代码,用于显示列表框中数据库中的数据
数据库有很多很多行

将这段代码放在

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

推荐答案

尝试一些不同的查询以找到正确的解决方案.

首先,删除WHERE条件.
Try few different queries to find the correct solution.

First of all, remove WHERE condition.
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).


它对我有用

你可以试试这个吗

1)运行此查询
It worked for me

Can you try this

1) run this query
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.


这篇关于将数据库显示到列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆