VB表单 – 允许用户交互从SQL Server中select数据

我是VB新手,不久前刚刚起步,我很高兴能够取得进展。

但是,当连接到SQL服务器时,我对VB表单更加陌生,并且允许用户与它进行交互以查询他们想要的任何数据到excel中。

它开始是这样的,我已经创build了一个用户窗体,它有checkbox(> than,<than),一个文本框(input一个数字)和另外两个checkbox(男,女)和一个combobox(状态)。 我也已经在SQL Server数据库中有数据。

我正在尝试做的事情仍然是尝试让用户通过选中checkbox,在combobox中select并在文本框中input一个数字,然后点击一个button来运行VB程序来导出请求数据导入到Excel中(我的挑战是 – 它可以将其导出到已经创build并保存在目录中的Excel文件中,或者将其导出到新创build的尚未保存的Excel文件中(有点像popup窗口)。

例如 – 用户检查>比,并input数字25(顺便说一句,这是年龄),并检查女性,并在combobox中select纽约,并点击一个button。 在这种情况下,程序应查询出现在纽约州的25岁以上的女性,并将其导出到Excel中,作为popup窗口或已保存在目录中的Excel文件。 我一直在做这方面的研究,但是由于我对forms,连接和提取都很陌生,所以没有取得太多的进展。 我的代码在下面的目录中创build一个Excel文件,并试图将数据查询到保存Excel文件。 我的查询也在下面。 请指教 !

Imports System.IO Imports excel = Microsoft.office.interop.Excel Imports System.Data Imports System.Data.SqlClient Imports System.Data.OleDb Module module1 Dim myconnection As SqlConnection Dim mycommand As SqlCommand Sub main() Dim xlapp = New excel.application xlapp.visible = True Dim xlwb As excel.workbook Dim xlws As excel.worksheet Dim path As String = "C:\users\t\" Dim excel_name As String = "zp" xlwb = xlapp.workbooks.add() xlws = xlwb.sheets("sheet1") xlwb.saves(path & excel_name) xlapp.save() xlapp.quit() Using myconnection As New SqlConnection("data source =afe;initial catalog=zp;integrated securitytrue") myconnection.Open() Dim mycommand As New SqlCommand("insert into openrowset('Microsoft.ace.oledb.12.0','excel 12.0; database=zp:\c:users\dek\rep\zp.xlsx;','SELECT * FROM [Sheet1$]') select * from mem_TBL", myconnection) End Using End Sub End Module 

这是我的用户select查询为例。

 SELECT az, a.ad, a.ag, a.ret, a.tot, a.wgt FROM mtbl a INNER JOIN zTBL b ON az = b.zp WHERE a.age > 25 AND a.ad = "NY" AND a.ret ="female" 

这是我导出到Excel时使用的方法:我创build一个Excel文件的模板,我将生成并保存在一个固定的文件夹中。 当我输出为excel时,我:

  1. 将模板文件复制到一个临时文件夹
  2. 打开临时文件,
  3. 将数据添加到临时文件,
  4. closures它,
  5. 将其保存到目标文件
  6. 删除临时文件

     Private Sub ExportToExcel() Using myconnection As New SqlClient.SqlConnection("data source=afe;initial catalog=zp;integrated securitytrue") myconnection.Open() Dim mycommand As New SqlClient.SqlCommand("SELECT az, a.ad, a.ag, a.ret, a.tot, a.wgt FROM mtbl a INNER JOIN zTBL b ON az = b.zp WHERE a.age > @age AND a.ad = @state AND a.ret = @gender", myconnection) mycommand.Parameters.AddWithValue("@age", 25) mycommand.Parameters.AddWithValue("@state", "NY") mycommand.Parameters.AddWithValue("@gender", "female") Dim dataset As New DataSet Dim adapter As New SqlClient.SqlDataAdapter(mycommand) adapter.Fill(dataset, "data") Dim xlapp = New Microsoft.Office.Interop.Excel.Application xlapp.visible = True Dim xlwb As Microsoft.Office.Interop.Excel.Workbook Dim xlws As Microsoft.Office.Interop.Excel.Worksheet Dim templatePath As String = "<path to template file>" Dim path As String = "C:\users\t\" Dim excel_name As String = "zp" Dim tempFIle As String = templatePath & "\NAME OF YOUR TEMPLATE FILE INCLUDING EXTENSION" xlwb = xlapp.Workbooks.Open(tempFIle) xlws = CType(xlwb.Worksheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet) Dim rowIndex As Integer = 0 For Each row As DataRow In dataset.Tables(0).Rows ' since you alrady have a template, ' you already know the cell mapping of each column ' in your template file. ' Excel uses Row, Column to map cells and is 1-based rowIndex += 1 xlapp.Cells(rowIndex, 1).Value = row("<name of column 1>") xlapp.Cells(rowIndex, 2).Value = row("<name of column 2>") xlapp.Cells(rowIndex, 3).Value = row("<name of column 3>") xlapp.Cells(rowIndex, 4).Value = row("<name of column 4>") '. '. 'xlapp.Cells(rowIndex, N).Value = row("<name of column N>") Next xlapp.DisplayAlerts = False xlwb.SaveAs(path & excel_name) xlwb.Close() xlapp.DisplayAlerts = True System.Runtime.InteropServices.Marshal.ReleaseComObject(xlwb) xlapp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp) System.IO.File.Delete(tempFIle) End Using End Sub 
Interesting Posts