如何从数据库中获取listView中选择的数据? [英] how can i get the data selected in listView from database ?

查看:103
本文介绍了如何从数据库中获取listView中选择的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

wana获取在winform中的listView控件中选择的值

像这样:


消息1//listView项目
讯息2
讯息3


如果我在列表视图中选择消息1,它将在数据库中获取消息1的内容吗?像电话中的示例消息一样思考.

我在下面编写了代码,但没有wrk并获取数据

hi guys ,

wana get the value that was choosen in the listView control in winform

like that :


Message 1 // items of listView
Message 2
Message 3


if i choose Message 1 in listview ,it will get the content of Message 1 in the database ? Think like sample Messages in telephones.

I wrote code below but doesnt wrk and get the data

baglanti.Open();
          int a = listView1.SelectedIndices[0];
          string cek = "Select icerik From hazir_mesajlar Where Kimlik"+a ;
          komut = new OleDbCommand(cek);
          komut.Connection = baglanti;

          OleDbDataReader dr = komut.ExecuteReader();

          dr.Read();
       string okudu = dr[0].ToString();

       MessageBox.Show(okudu);
       komut.Dispose();
       baglanti.Close();

推荐答案

更正来自以下位置的SQL查询:
Correct your SQL query from:
string cek = "Select icerik From hazir_mesajlar Where Kimlik"+a ;




to

string cek = "Select icerik From hazir_mesajlar Where Kimlik = " + a ;


WHERE子句缺少等号.一个简单的DEBUG就会告诉您.


WHERE clause was missing an equal to sign. A simple DEBUG would have told you that.


对于初学者来说,string cek = "Select icerik From hazir_mesajlar Where Kimlik"+a ;将不起作用.例如,如果使用a == 1,则您的代码将生成Kimlik1而不是Kimlik=1.
您也将不使用选定的索引,而是使用与hazir_mesajlar表的主键相对应的listview项的值.
For starters, string cek = "Select icerik From hazir_mesajlar Where Kimlik"+a ; won''t work. If a == 1 for example, then your code would result in Kimlik1 instead of Kimlik=1 which may work.
You would also be adivsed not to use the selected index, but rather a value of the listview item that corresponds to the primary key of your hazir_mesajlar table.


这篇关于如何从数据库中获取listView中选择的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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