Пустые записи, добавленные в базу данных SQL server при загрузке массовых записей через xls-файл в VB, net
Моя проблема заключается в том, что при загрузке массовых записей в базу данных SQL Server через VB.Net он создает много пустых записей после вставки всех записей в файл xls
как устранить эту проблему. принимая во внимание, что Excel.xls в файле нет пустых записей .
Что я уже пробовал:
Private Sub browseXLfile_Click(sender As Object, e As EventArgs) Handles browseXLfile.Click btnCustRefresh.PerformClick() Dim SystemUserName As String = Environment.UserName Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Users WHERE Username = @SystemUserName AND Role = 'Super-User' ", cnnOLEDB) If cnnOLEDB.State = ConnectionState.Closed Then cnnOLEDB.Open() End If 'cmd.Parameters.Clear() cmd.Parameters.AddWithValue("@SystemUserName", SystemUserName) Dim sdr As SqlDataReader = cmd.ExecuteReader() If (sdr.Read() = True) Then Dim ofd As New OpenFileDialog If ofd.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Exit Sub Dim nme As String = ofd.FileName Dim safename As String = ofd.SafeFileName safename = safename.Substring(0, safename.LastIndexOf(".")) Import(nme, DataGridViewCustomer, safename) Else MessageBox.Show("You have no access. You are not listed in the Admins list ", Me.Text) End If sdr.Close() End Sub Public Shared Function Import(ByVal FileName As String, ByVal DataGridViewCustomer As DataGridView, ByVal safefilename As String) As Boolean Try Dim MyConnection As System.Data.OleDb.OleDbConnection Dim DtSet As System.Data.DataSet Dim MyCommand As System.Data.OleDb.OleDbDataAdapter MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=Excel 8.0;") MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection) MyCommand.TableMappings.Add("Customers", safefilename) DtSet = New System.Data.DataSet MyCommand.Fill(DtSet) DataGridViewCustomer.DataSource = DtSet.Tables(0) MyConnection.Close() Dim expr As String = "SELECT * FROM [Sheet1$]" Dim SQLconn As New SqlConnection() 'Dim ConnString As String = "Data Source=EB-5CG7476R7V\SQLEXPRESS;Initial Catalog=SupplierSQL_DB;Integrated Security=True" Dim ConnString As String = "Server=tcp:server-name,1433;Initial Catalog=SupplierDB;Persist Security Info=False;User ID=SERVERUSER;Password=SERVERPASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, MyConnection) Dim objDR As OleDbDataReader SQLconn.ConnectionString = ConnString Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(ConnString) bulkCopy.DestinationTableName = safefilename Try MyConnection.Open() objDR = objCmdSelect.ExecuteReader bulkCopy.WriteToServer(objDR) objDR.Close() SQLconn.Close() Catch ex As Exception MsgBox(ex.ToString) End Try End Using Return True Catch ex As Exception Return False End Try End Function