Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString, strSQL As String
strConnString = "Data Source=.\SQLEXPRESS;Initial Catalog=LAHWINFO;Integrated Security=True"
Dim sb As StringBuilder = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM TotalPrint")
sb.Append(" WHERE (DateSavePrint BETWEEN @date1 AND @date2)")
sb.Append(" SELECT SerialNo FROM TotalPrint")
sb.Append(" WHERE (SerialNo=@SerialNo)")
strSQL = sb.ToString()
objConn.ConnectionString = strConnString
With objCmd
.Parameters.Clear()
.Parameters.Add("@date1", SqlDbType.SmallDateTime).Value = dtpStartSearch.Value.ToShortDateString
.Parameters.Add("@date2", SqlDbType.SmallDateTime).Value = dtpEndSearch.Value.ToShortDateString
.Parameters.Add("@SerialNo", SqlDbType.NVarChar).Value = cboNamePrint.SelectedIndex
.CommandText = strSQL
.CommandType = CommandType.Text
.Connection = objConn
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds, "myDataTable")
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim rpt As New ReportDocument()
Dim directory As String = My.Application.Info.DirectoryPath
'rpt.Load(directory & "\myCrystalReport1.rpt")
rpt.Load("C:\CrystalReport2.rpt")
rpt.SetDataSource(dt)
Me.CrystalReportViewer1.ReportSource = rpt
Me.CrystalReportViewer1.Refresh()
Tag : .NET, Ms SQL Server 2008, VBScript, VS 2010 (.NET 4.x)
Dim sb As StringBuilder = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM TotalPrint")
sb.Append(" WHERE (DateSavePrint BETWEEN @date1 AND @date2)")
sb.Append(" SELECT SerialNo FROM TotalPrint")
sb.Append(" WHERE (SerialNo=@SerialNo)")
strSQL = sb.ToString()
SELECT * FROM TotalPrint WHERE (DateSavePrint BETWEEN @date1 AND @date2) AND (SerialNo=@SerialNo)
งงอะไรอะ งงเบย
Date :
2013-08-30 09:22:02
By :
01000010
No. 4
Guest
Solution 1. Multiple Result set
sb.Append("; SELECT SerialNo FROM TotalPrint")
Code (VB.NET)
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim dt1 As DataTable '*****
Dim strConnString, strSQL As String
strConnString = "Data Source=.\SQLEXPRESS;Initial Catalog=LAHWINFO;Integrated Security=True"
Dim sb As StringBuilder = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM TotalPrint")
sb.Append(" WHERE (DateSavePrint BETWEEN @date1 AND @date2)")
sb.Append("; SELECT SerialNo FROM TotalPrint") '*****
sb.Append(" WHERE (SerialNo=@SerialNo)")
strSQL = sb.ToString()
objConn.ConnectionString = strConnString
With objCmd
.Parameters.Clear()
.Parameters.Add("@date1", SqlDbType.SmallDateTime).Value = dtpStartSearch.Value.ToShortDateString
.Parameters.Add("@date2", SqlDbType.SmallDateTime).Value = dtpEndSearch.Value.ToShortDateString
.Parameters.Add("@SerialNo", SqlDbType.NVarChar).Value = cboNamePrint.SelectedIndex
.CommandText = strSQL
.CommandType = CommandType.Text
.Connection = objConn
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds, "myDataTable")
dt = ds.Tables(0)
dt1 = ds.Tables(1) '*****
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim rpt As New ReportDocument()
Dim directory As String = My.Application.Info.DirectoryPath
'rpt.Load(directory & "\myCrystalReport1.rpt")
rpt.Load("C:\CrystalReport2.rpt")
rpt.SetDataSource(dt)
Me.CrystalReportViewer1.ReportSource = rpt
Me.CrystalReportViewer1.Refresh()
Solution 2. Change Query Code (VB.NET)
Dim sb As StringBuilder = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM TotalPrint")
sb.Append(" WHERE (DateSavePrint BETWEEN @date1 AND @date2)")
sb.Append(" AND (SerialNo=@SerialNo)")
strSQL = sb.ToString()
Dim sb As StringBuilder = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM TotalPrint")
sb.Append(" WHERE (DateSavePrint BETWEEN @date1 AND @date2)")
sb.Append(" AND (SerialNo=@SerialNo)")
strSQL = sb.ToString()
strConnString = "Data Source=.\SQLEXPRESS;Initial Catalog=LAHWINFO;Integrated Security=True"
Dim sb As StringBuilder = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM TotalPrint")
sb.Append(" WHERE (DateSavePrint BETWEEN @date1 AND @date2)")
sb.Append(" SELECT SerialNo FROM TotalPrint")
sb.Append(" WHERE (SerialNo=@SerialNo)")
strSQL = sb.ToString()