简体   繁体   English

从数据库中选择数据取决于列表框中的项目c#

[英]Select Data from Database Depend on Items in Listbox c#

I have a problem to select data depending on all items in a Listbox. 我在根据列表框中的所有项目选择数据时遇到问题。 Here, in my Listbox there are two items, named Kamera125 and Kamera127. 在我的列表框中,有两个项目,分别是Kamera125和Kamera127。 Kamera125 and Kamera127 exist in a MS Access Database. MS Access数据库中存在Kamera125和Kamera127。 So, when I run my program, I want my program to select Kamera125 and Kamera127 from listbox that connected to MS Access. 因此,当我运行程序时,我希望程序从连接到MS Access的列表框中选择Kamera125和Kamera127。 I used the following query 我使用以下查询

string selectsemuakoordgaris = "select * from koordinatgaris where namakamera='" + listBox3.Text + "'"; 

and it doesn't work. 而且不起作用。

These is my codes : 这些是我的代码:

            private void ProsesSemuaKamera()
            {
                Stopwatch watch = Stopwatch.StartNew();

                Ping ping = new Ping();
                PingReply pingreply;

                OleDbConnection kon = new OleDbConnection(koneksi);
                OleDbCommand command = kon.CreateCommand();
                kon.Open();
                string selecturl = "select * from datakamera";
                command.CommandText = selecturl;
                OleDbDataReader bacadata = command.ExecuteReader();

                while (bacadata.Read())
                {
                    int counturl = 0;
                    pingreply = ping.Send(bacadata["ipadd"].ToString());

                    if (pingreply.Status == IPStatus.Success)
                    {
                        listBox1.Items.Add(bacadata["ipadd"].ToString());
                        listBox3.Items.Add(bacadata["namakamera"].ToString());
                        textBox1.Text += bacadata["namakamera"].ToString() + Environment.NewLine;

                        CaptureSemuaKamera = new Capture(bacadata["urlkamera"].ToString());
                        Application.Idle += new EventHandler(ProcessFrameSemuaKamera);
                    }
                    else if (pingreply.Status != IPStatus.Success)
                    {
                        listBox2.Items.Add(bacadata["ipadd"].ToString());
                    }
                }
                kon.Close();

                watch.Stop();
                File.AppendAllText(@"D:\Dokumen\Alfon\TA Alfon\Waktu Eksekusi Ping.txt", "Waktu eksekusi ping " + DateTime.Now + " :" + " " + watch.Elapsed.TotalMilliseconds.ToString() + Environment.NewLine);
            }


            private void ProcessFrameSemuaKamera(object sender, EventArgs e)
            {
                Image<Bgr, Byte> sourceImage = CaptureSemuaKamera.QueryFrame();
                SourceBox.Image = sourceImage.Bitmap;
ProsesSemuaKamera();
            }

            private void ProsesKameraSemua()
            {
                Image<Bgr, Byte> sourceImage = CaptureSemuaKamera.QueryFrame();
                SourceBox.Image = sourceImage.Bitmap;

                OleDbConnection kon = new OleDbConnection(koneksi);
                OleDbCommand commandkoord = kon.CreateCommand();
                OleDbCommand commandkoordgaris = kon.CreateCommand();

                kon.Open();

                string selectsemuakoord = "select * from koordinatkotak where namakamera='"+ listBox3.Items + "'";
                string selectsemuakoordgaris = "select * from koordinatgaris where namakamera='" + listBox3.Items + "'";
                commandkoord.CommandText = selectsemuakoord;
                commandkoordgaris.CommandText = selectsemuakoordgaris;
                OleDbDataReader bacakoord = commandkoord.ExecuteReader();
                OleDbDataReader bacakoordgaris = commandkoordgaris.ExecuteReader();

                while (bacakoord.Read() && bacakoordgaris.Read())
                {
                    #region Perspective projection

                    PointF[] srcs = new PointF[4];
                    srcs[0] = new PointF(int.Parse(bacakoord["x1source"].ToString()), int.Parse(bacakoord["y1source"].ToString())); //119, 187
                    srcs[1] = new PointF(int.Parse(bacakoord["x2source"].ToString()), int.Parse(bacakoord["y2source"].ToString())); //242, 181
                    srcs[2] = new PointF(int.Parse(bacakoord["x3source"].ToString()), int.Parse(bacakoord["y3source"].ToString())); //253, 225
                    srcs[3] = new PointF(int.Parse(bacakoord["x4source"].ToString()), int.Parse(bacakoord["y4source"].ToString())); //112, 231

                    PointF[] dsts = new PointF[4];
                    dsts[0] = new PointF(int.Parse(bacakoord["x1proj"].ToString()), int.Parse(bacakoord["y1proj"].ToString()));
                    dsts[1] = new PointF(int.Parse(bacakoord["x2proj"].ToString()), int.Parse(bacakoord["y2proj"].ToString()));
                    dsts[2] = new PointF(int.Parse(bacakoord["x3proj"].ToString()), int.Parse(bacakoord["y3proj"].ToString()));
                    dsts[3] = new PointF(int.Parse(bacakoord["x4proj"].ToString()), int.Parse(bacakoord["y4proj"].ToString()));


                    HomographyMatrix mywarpmat = CameraCalibration.GetPerspectiveTransform(srcs, dsts);
                    Image<Bgr, Byte> newImage = sourceImage.WarpPerspective(mywarpmat, 355, 288, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR, Emgu.CV.CvEnum.WARP.CV_WARP_FILL_OUTLIERS, new Bgr(0, 0, 0));
                    Image<Gray, Byte> newImageGray = newImage.Convert<Gray, Byte>();

                    Image<Bgr, Byte> imageToShow = newImage.Copy();
                    Image<Bgr, Byte> imageToShowGaris = newImage.Copy();

                    ProjectionBox.Image = newImage.Bitmap; //I want to show Projection result in ProjectionBox. All of coordinates are saved in database. When Kamera125 is choosen, all of coordinates in Kamera125 will be executed. So here, I want to execute all of coordinates of Kamera125 and Kamera127 that is shown in listBox.

                    #endregion
                }
                kon.Close();
            }

here's a simple method where you can pass in your listbox that has the items selected. 这是一种简单的方法,您可以在其中选择了项目的列表框中传递。 It will return a string of the where clause built from the selected items. 它将返回从所选项目构建的where子句的字符串。

    private string BuildWhereClause(ListBox lb)
    {
        string WHEREclause = string.Empty;
        foreach(var itm in lb.SelectedItems)
        {
            if (WHEREclause  == string.Empty )
            {
                WHEREclause += " WHERE namakamera = '" + itm + "' ";
            }
            else
            {
                WHEREclause += " OR namakamera = '" + itm + "' ";
            }
        }
        return WHEREclause;
    }

From this you can build your statement 由此您可以建立自己的陈述

string selectsemuakoord = "select * from koordinatkotak " + BuildWhereClause(YourListBox);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM