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

时间:2016-01-23 11:38:22

标签: c# ms-access listbox

我根据列表框中的所有项目选择数据时遇到问题。在我的列表框中,有两个项目,名为Kamera125和Kamera127。 Kamera125和Kamera127存在于MS Access数据库中。所以,当我运行我的程序时,我希望我的程序从连接到MS Access的列表框中选择Kamera125和Kamera127。我使用了以下查询

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

它不起作用。

这些是我的代码:

            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();
            }

1 个答案:

答案 0 :(得分:1)

这是一个简单的方法,您可以传入包含所选项目的列表框。它将返回从所选项构建的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;
    }

从这里你可以建立你的陈述

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