填充:SelectCommand 连接 属性 尚未初始化
fill: SelectCommand connection property has not been initialized
Dim SiparisOnayi As String
Dim SiparisDurumu As String
Dim SiparisIli As String
Dim SiparisOdemeYontemi As String
Dim SiparisKargoFirmasi As String
Dim SiparisSatisKanali As String
Dim a1, a2, a3, a4, a5, a6, a7, soncom As String
If ComboBox1.Text = Nothing Then
a1 = Nothing
Else
SiparisOnayi = ComboBox1.Text
a1 = " and Siparis_Onay = SiparisOnayi"
End If
If ComboBox2.Text = Nothing Then
a2 = Nothing
Else
SiparisDurumu = ComboBox2.Text
a2 = " and Siparis_Durumu = SiparisDurumu "
End If
If ComboBox3.Text = Nothing Then
a3 = Nothing
Else
SiparisIli = ComboBox3.Text
a3 = " and Musteri_IL = SiparisIli "
End If
If ComboBox4.Text = Nothing Then
a4 = Nothing
Else
a4 = " and Kullanici_Kodu = SiparisKullanicisi"
End If
If ComboBox5.Text = Nothing Then
a5 = Nothing
Else
SiparisOdemeYontemi = ComboBox5.Text
a5 = " and Odeme_Yontemi = SiparisOdemeYontemi"
End If
If ComboBox6.Text = Nothing Then
a6 = Nothing
Else
SiparisKargoFirmasi = ComboBox6.Text
a6 = " and Kargo_Adi = SiparisKargoFirmasi"
End If
If ComboBox7.Text = Nothing Then
a7 = Nothing
Else
SiparisSatisKanali = ComboBox7.Text
a7 = " and Satis_Kanali = SiparisSatisKanali"
End If
soncom = "SELECT * FROM `Siparisler` WHERE `Siparis_Tarihi` BETWEEN @d1 and @d2" & a1 & a2 & a3 & a4 & a5 & a6 & a7 & ", connection"
Try
Dim command As New MySqlCommand(soncom)
command.Parameters.Add("@d1", MySqlDbType.DateTime).Value = DateTimePicker2.Value
command.Parameters.Add("@d2", MySqlDbType.DateTime).Value = DateTimePicker3.Value
Dim table As New DataTable
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(table)
DataGridView1.DataSource = table
Label12.Text = "Toplam " & table.Rows.Count & " Kayıt bulundu ve gösteriliyor."
myconnection.close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
错误:填充:SelectCommand 连接 属性 尚未初始化
如何修复我的代码?
这一行末尾的 & ", connection"
在我看来是错误的:
soncom = "SELECT ..." & a1 & ... a7 & ", connection"
但是,除了命令和适配器之外,您还需要一个连接对象,WHERE 子句当前的组装方式如果能够正常工作,将极易受到 sql 注入问题的影响,但它不会,因为组合框值从未实际插入到最终字符串中,并且如果在调用 Fill()
.
期间抛出异常,代码很容易使连接保持打开状态
这应该可以解决所有这些问题(一旦您填写了连接字符串):
Dim table As New DataTable
Using connection As New MySqlConnection("connection string here")
Using command As New MySqlCommand("", connection)
Using adapter As New MySqlDataAdapter(command)
Dim sql As String = "SELECT * FROM `Siparisler` WHERE `Siparis_Tarihi` BETWEEN @d1 and @d2"
If Not string.IsNullOrWhitespace(ComboBox1.Text) Then
sql += " and Siparis_Onay = @SiparisOnayi"
command.Parameters.AddWithValue("@SiparisOnayi", ComboBox1.Text)
End if
If Not string.IsNullOrWhitespace(ComboBox2.Text) Then
sql += " and Siparis_Durumu = @SiparisDurumu"
command.Parameters.AddWithValue("@SiparisDurumu", ComboBox2.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox3.Text) Then
sql += " and Musteri_IL = @SiparisIli"
command.Parameters.AddWithValue("@SiparisIli", ComboBox3.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox4.Text) Then
sql +" and Kullanici_Kodu = @SiparisKullanicisi"
command.Parameters.AddWithValue("@SiparisKullanicisi", ComboBox4.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox5.Text) Then
sql += " and Odeme_Yontemi = @SiparisOdemeYontemi"
command.Parameters.AddWithValue("@SiparisOdemeYontemi", ComboBox5.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox6.Text) Then
sql += " and Kargo_Adi = @SiparisKargoFirmasi"
command.Parameters.AddWithValue("@SiparisKargoFirmasi", ComboBox6.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox7.Text) Then
sql += " and Satis_Kanali = @SiparisSatisKanali"
command.Parameters.AddWithValue("@SiparisSatisKanali", ComboBox7.Text)
End If
command.CommandText = sql
command.Parameters.Add("@d1", MySqlDbType.DateTime).Value = DateTimePicker2.Value
command.Parameters.Add("@d2", MySqlDbType.DateTime).Value = DateTimePicker3.Value
Try
adapter.Fill(table)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Using
End Using
DataGridView1.DataSource = table
Label12.Text = $"Toplam {table.Rows.Count} Kayıt bulundu ve gösteriliyor."
Dim SiparisOnayi As String
Dim SiparisDurumu As String
Dim SiparisIli As String
Dim SiparisOdemeYontemi As String
Dim SiparisKargoFirmasi As String
Dim SiparisSatisKanali As String
Dim a1, a2, a3, a4, a5, a6, a7, soncom As String
If ComboBox1.Text = Nothing Then
a1 = Nothing
Else
SiparisOnayi = ComboBox1.Text
a1 = " and Siparis_Onay = SiparisOnayi"
End If
If ComboBox2.Text = Nothing Then
a2 = Nothing
Else
SiparisDurumu = ComboBox2.Text
a2 = " and Siparis_Durumu = SiparisDurumu "
End If
If ComboBox3.Text = Nothing Then
a3 = Nothing
Else
SiparisIli = ComboBox3.Text
a3 = " and Musteri_IL = SiparisIli "
End If
If ComboBox4.Text = Nothing Then
a4 = Nothing
Else
a4 = " and Kullanici_Kodu = SiparisKullanicisi"
End If
If ComboBox5.Text = Nothing Then
a5 = Nothing
Else
SiparisOdemeYontemi = ComboBox5.Text
a5 = " and Odeme_Yontemi = SiparisOdemeYontemi"
End If
If ComboBox6.Text = Nothing Then
a6 = Nothing
Else
SiparisKargoFirmasi = ComboBox6.Text
a6 = " and Kargo_Adi = SiparisKargoFirmasi"
End If
If ComboBox7.Text = Nothing Then
a7 = Nothing
Else
SiparisSatisKanali = ComboBox7.Text
a7 = " and Satis_Kanali = SiparisSatisKanali"
End If
soncom = "SELECT * FROM `Siparisler` WHERE `Siparis_Tarihi` BETWEEN @d1 and @d2" & a1 & a2 & a3 & a4 & a5 & a6 & a7 & ", connection"
Try
Dim command As New MySqlCommand(soncom)
command.Parameters.Add("@d1", MySqlDbType.DateTime).Value = DateTimePicker2.Value
command.Parameters.Add("@d2", MySqlDbType.DateTime).Value = DateTimePicker3.Value
Dim table As New DataTable
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(table)
DataGridView1.DataSource = table
Label12.Text = "Toplam " & table.Rows.Count & " Kayıt bulundu ve gösteriliyor."
myconnection.close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
错误:填充:SelectCommand 连接 属性 尚未初始化
如何修复我的代码?
这一行末尾的 & ", connection"
在我看来是错误的:
soncom = "SELECT ..." & a1 & ... a7 & ", connection"
但是,除了命令和适配器之外,您还需要一个连接对象,WHERE 子句当前的组装方式如果能够正常工作,将极易受到 sql 注入问题的影响,但它不会,因为组合框值从未实际插入到最终字符串中,并且如果在调用 Fill()
.
这应该可以解决所有这些问题(一旦您填写了连接字符串):
Dim table As New DataTable
Using connection As New MySqlConnection("connection string here")
Using command As New MySqlCommand("", connection)
Using adapter As New MySqlDataAdapter(command)
Dim sql As String = "SELECT * FROM `Siparisler` WHERE `Siparis_Tarihi` BETWEEN @d1 and @d2"
If Not string.IsNullOrWhitespace(ComboBox1.Text) Then
sql += " and Siparis_Onay = @SiparisOnayi"
command.Parameters.AddWithValue("@SiparisOnayi", ComboBox1.Text)
End if
If Not string.IsNullOrWhitespace(ComboBox2.Text) Then
sql += " and Siparis_Durumu = @SiparisDurumu"
command.Parameters.AddWithValue("@SiparisDurumu", ComboBox2.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox3.Text) Then
sql += " and Musteri_IL = @SiparisIli"
command.Parameters.AddWithValue("@SiparisIli", ComboBox3.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox4.Text) Then
sql +" and Kullanici_Kodu = @SiparisKullanicisi"
command.Parameters.AddWithValue("@SiparisKullanicisi", ComboBox4.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox5.Text) Then
sql += " and Odeme_Yontemi = @SiparisOdemeYontemi"
command.Parameters.AddWithValue("@SiparisOdemeYontemi", ComboBox5.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox6.Text) Then
sql += " and Kargo_Adi = @SiparisKargoFirmasi"
command.Parameters.AddWithValue("@SiparisKargoFirmasi", ComboBox6.Text)
End If
If Not string.IsNullOrWhitespace(ComboBox7.Text) Then
sql += " and Satis_Kanali = @SiparisSatisKanali"
command.Parameters.AddWithValue("@SiparisSatisKanali", ComboBox7.Text)
End If
command.CommandText = sql
command.Parameters.Add("@d1", MySqlDbType.DateTime).Value = DateTimePicker2.Value
command.Parameters.Add("@d2", MySqlDbType.DateTime).Value = DateTimePicker3.Value
Try
adapter.Fill(table)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Using
End Using
DataGridView1.DataSource = table
Label12.Text = $"Toplam {table.Rows.Count} Kayıt bulundu ve gösteriliyor."