Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sqlcon(1) '' ต่อ connection
sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
sb.Append("Select PromotionID ,CreateDate From Promotion")
sb.Append(" Where (CreateDate Between '" & DateTimePicker1.Value.ToString("MM/dd/yyy") & "' And '" & DateTimePicker2.Value.ToString("MM/dd/yyy") & "')")
sql = sb.ToString() '' เก็บใน String
pro = Showquerysql(sql, "dtDetailPromotion") '' ยัดข้อมูลเข้า ใน datatable
With DataGridView1 '' โชว์ข้อมูลใน datagridveiw
.DataSource = pro
End With
End Sub
เป็น StringBuilder ครับ เราประการตัวแปรไว้ที่ global ครับ เช่น Code (VB.NET)
Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
public class Form1
dim con as sqlconnection
dim com as sqlcommand
dim sb as stringbuilder '' ตรงนี้ครับ
dim dtTable as datatable
...
...
end class
Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
Module moduleSql
Public com As New SqlCommand
Public con As New SqlConnection
Public sb As New StringBuilder
Public dtTabel as Datable
Public sql As String
...
...
...
end module
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class Form2
Dim dr As SqlDataReader
Dim cn As SqlConnection
Dim cm As SqlCommand
Dim sb As New StringBuilder
Dim strSql As String
Dim strCn As String = "Data Source=vss-server;Initial Catalog=Sale;Integrated Security=True"
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn = New SqlConnection()
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strCn '' string ที่ใช้ต่อ connection
.Open()
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
sb.Append("Select PromotionID ,CreateDate From Promotion")
sb.Append(" Where (CreateDate Between '" & DateTimePicker1.Value.ToString("MM/dd/yyy") & "' And '" & DateTimePicker2.Value.ToString("MM/dd/yyy") & "')")
strSql = sb.ToString() '' เก็บใน String
com = New SqlCommand()
With com
.CommandText = strSql
.CommandType = CommandType.Text
.Connection = cn
dr = .ExecuteReader()
End With
If dr.HasRows Then ''check dr
Dim dtSql As DataTable
dtSql = New DataTable()
dtSql.Load(dr)
With DataGridView1
.DataSource = dtSql
End With
End If
End Sub
End Class
อย่างนี้พอจะเข้าใจไหมครับ ถ้าไม่เข้าใจก็สอบถามใหม่ได้ครับ
tee
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
'Imports System.Windows.Forms
Public Class Form1
Dim dr As SqlDataReader
Dim cn As SqlConnection
Dim cm As SqlCommand
Dim sb As New StringBuilder
Dim strSql As String
Dim strCn As String = "Data Source=bkk314;Initial Catalog=DB_For_Webboard;Integrated Security=True"
Dim dtTable As DataTable
Dim Nowdate As Date
Dim Enddate As Date
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection()
sb.Remove(0, sb.Length)
sb.Append(" SELECT GETDATE() as Gdate") '' get วันที่ปัจจุบัน จาก server
strSql = sb.ToString
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = strSql
.Connection = cn
dr = .ExecuteReader()
End With
If dr.HasRows Then
dtTable = New DataTable
dtTable.Load(dr)
Nowdate = CDate(dtTable.Rows(0).Item("Gdate")).ToString("dd/MM/yyyy") '' DateNow
Enddate = Nowdate.AddDays(7) '' บวกวัน ไปอีก 1 อาทิตย์ เพื่อเทียบวันใกล้หมดอายุ
'**ถ้าอยู่ในช่วงวันที่ปัจจุบันและนับไปอีก 7 วัน ถือว่าใกล้หมดอายุ
End If
connection()
sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
sb.Append("Select productid,productname,price,qty,totalprice,datesales From Product")
sb.Append(" Where (datesales between '" & Nowdate.ToString("MM/dd/yyy") & "' And ('" & Enddate.ToString("MM/dd/yyy") & "'))")
strSql = sb.ToString()
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = strSql
.Connection = cn
dr = .ExecuteReader()
End With
If dr.HasRows Then
dtTable = New DataTable
dtTable.Load(dr)
With DataGridView1
.DataSource = dtTable
End With
End If
End Sub
Private Sub connection()
cn = New SqlConnection()
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strCn '' string ที่ใช้ต่อ connection
.Open()
End With
End Sub
End Class
Public Class FormReportExp
Dim dr As SqlDataReader
Dim cn As SqlConnection
Dim cm As SqlCommand
Dim sb As New StringBuilder
Dim strSql As String
Dim strCn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim dtTable As DataTable
Dim Nowdate As DateTime
Dim Enddate As DateTime
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If MessageBox.Show("ต้องการออกจากรายงานสินค้าใกล้หมดอายุใช่หรือไม่", "ออกจากระบบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Me.Close()
End If
End Sub
Private Sub FormReportExp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim conn As New SqlConnection(conStr)
conn.Open()
Dim sql As String = "SELECT serialno,name,unit,totalstock,exp FROM product "
Dim cmd As New SqlCommand(sql, conn)
Dim adapter As New SqlDataAdapter(cmd)
Dim data As New DataSet()
adapter.Fill(data, "product")
DataGridView1.DataSource = data.Tables("product")
DataGridView1.Columns("serialno").HeaderText = "รหัสสินค้า"
DataGridView1.Columns("name").HeaderText = "ชื่อสินค้า"
DataGridView1.Columns("unit").HeaderText = "หน่วยสินค้า"
DataGridView1.Columns("totalstock").HeaderText = "สินค้าคงคลัง"
DataGridView1.Columns("exp").HeaderText = "วันหมดอายุ"
conn.Close()
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
connection()
sb.Remove(0, sb.Length)
sb.Append(" SELECT GETDATE() as Gdate") '' get วันที่ปัจจุบัน จาก server
strSql = sb.ToString
cm = New SqlCommand()
With cm
.CommandType = CommandType.Text
.CommandText = strSql
.Connection = cn
dr = .ExecuteReader()
End With
If dr.HasRows Then
dtTable = New DataTable
dtTable.Load(dr)
Nowdate = CDate(dtTable.Rows(0).Item("Gdate")).ToString("dd/MM/yyyy") '' DateNow
Enddate = Nowdate.AddDays(7) '' บวกวัน ไปอีก 1 อาทิตย์ เพื่อเทียบวันใกล้หมดอายุ
'**ถ้าอยู่ในช่วงวันที่ปัจจุบันและนับไปอีก 7 วัน ถือว่าใกล้หมดอายุ
End If
connection()
sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
sb.Append("Select serialno,name,unit,totalstock,exp From product")
sb.Append(" Where (exp between '" & Nowdate.ToString("dd/MM/yyy") & "' And ('" & Enddate.ToString("dd/MM/yyy") & "'))")
strSql = sb.ToString()
cm = New SqlCommand()
With cm
.CommandType = CommandType.Text
.CommandText = strSql
.Connection = cn
dr = .ExecuteReader()
End With
If dr.HasRows Then
dtTable = New DataTable
dtTable.Load(dr)
With DataGridView1
.DataSource = dtTable
End With
End If
End Sub
Private Sub connection()
cn = New SqlConnection()
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strCn '' string ที่ใช้ต่อ connection
.Open()
End With
' นับแถวของ datagridview
order_total.Text = " " + DataGridView1.Rows.Count.ToString()
End Sub
End Class