mariadb 如何通过Windows Form App查询数据后显示数据?

aoyhnmkz  于 12个月前  发布在  Windows
关注(0)|答案(1)|浏览(124)

我有一个简单的Windows窗体应用程序,它有一个连接到MariaDB的数据网格视图。我添加了“添加条目”和“删除条目”按钮,但我在添加“搜索”按钮时遇到了麻烦。我还有六个文本框,它们代表数据库中一列的值(共6列),我用它们来添加条目到网格/数据库。(当我按下“搜索”以显示具有该列值的所有条目时)。
这是我想出来的,但它最终没有工作。我知道问题是在最后几行,我不知道如何在我的数据网格视图中显示新的数据表。提前感谢。

private void btnPretraga_Click(object sender, EventArgs e)
{
    string connString = ConfigurationManager.ConnectionStrings["MariaDBConnectionString"].ConnectionString;
    using (MySqlConnection konekcija = new MySqlConnection(connString))
    {
        konekcija.Open();
        string upit = "SELECT * FROM drvnisortiment WHERE (vrstaDrveta = @VrstaDrveta OR duzinaM = @DuzinaM OR precnikCM = @PrecnikCM OR klasa=@Klasa OR redniBrDoznake=@RedniBrDoznake OR vrstaSlUzitka=@VrstaSlUzitka)";
        using (MySqlCommand cmd = new MySqlCommand(upit, konekcija))
        {
            MySqlDataAdapter SDA = new MySqlDataAdapter(upit, connString);
            cmd.Parameters.AddWithValue("@VrstaDrveta", vrstaDrvetatextBox.Text);
            cmd.Parameters.AddWithValue("@DuzinaM", duzinaMtextBox.Text);
            cmd.Parameters.AddWithValue("@PrecnikCM", precnikCMtextBox.Text);
            cmd.Parameters.AddWithValue("@Klasa", klasatextBox.Text);
            cmd.Parameters.AddWithValue("@RedniBrDoznake", redniBrDoznaketextBox.Text);
            cmd.Parameters.AddWithValue("@VrstaSlUzitka", vrstaSlUzitkatextBox.Text);
            DataSet ds = new DataSet();
            SDA.Fill(ds, "drvnisortiment");
            drvniSortimentGridView.DataSource = ds.Tables[0];

        }
    }
}

字符串

yqyhoc1h

yqyhoc1h1#

当包含DataGridView LOADS的窗体考虑使用SQLite时,将运行此代码。

Private Sub ViewSearches()

    Dim intID As Integer
    Dim strDate As String
    Dim strAmt As Decimal
    Dim strYear As Integer
    Dim strSM As Integer

    Dim rowCount As Integer
    Dim maxRowCount As Integer
    Dim emptyStr As String = "  "

    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")

        conn.Open()

        Using cmd As New SQLiteCommand("", conn)

            If gvSearchType = "All" Then
                cmd.CommandText = "SELECT * FROM TxData"
                'cmd.Parameters.AddWithValue("$intID", intID)
                'cmd.Parameters.AddWithValue("$strDate", strDate)
                'cmd.Parameters.AddWithValue("$strAmt", strAmt)
                'cmd.Parameters.AddWithValue("$gvYear", strYear)
                'cmd.Parameters.AddWithValue("$gvSM", strSM)

            ElseIf gvSearchType = "MoRangeYr" Then
                'cmd.CommandText = "SELECT * FROM TxData"
                'cmd.CommandText = "SELECT * FROM TxData WHERE txAmount >= '1.00' OR  txAmount <= '2.00'"
                cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth >= $gvFromMonth AND txSearchMonth <= $gvToMonth AND txYear = $gvYear "
                'cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth BETWEEN $gvFromMonth AND $gvToMonth AND txYear = $gvYear "
                cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
                cmd.Parameters.AddWithValue("$gvToMonth", gvToMonth)
                cmd.Parameters.AddWithValue("$gvYear", gvYear)
            End If

            Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
                'dgvTX.DataSource = rdr
                'Statement Above is when DB is bound to dgvTX
                '============================================

                While rdr.Read()
                    intID = CInt((rdr("TID")))
                    strDate = rdr("txSortDate").ToString
                    strAmt = CDec(rdr("txAmount"))
                    strYear = CInt(rdr("txYear").ToString)
                    strSM = CInt(rdr("txSearchMonth").ToString) 'was txSearchMonth

                    'dgvTX.Columns(4).DefaultCellStyle.Format = "N"

                    dgvTX.Columns(2).DefaultCellStyle.Format = "C" 'Adds the $ sign and commas
                    dgvTX.Rows.Add(intID, strDate, strAmt, strYear, strSM, emptyStr)
                    rowCount = rowCount + 1

                End While
                If rowCount <= 25 Then
                    maxRowCount = 25 - rowCount
                    For iA = 1 To maxRowCount
                        dgvTX.Rows.Add(" ")
                    Next
                End If

                'dgvTX.Columns(3).DefaultCellStyle.Format = "N"

                dgvTX.Sort(dgvTX.Columns(0), ListSortDirection.Ascending)

            End Using
        End Using
    End Using

End Sub

字符串
End Class

相关问题