Search VB 2010

Showing posts with label Sql Server and VB.Net. Show all posts
Showing posts with label Sql Server and VB.Net. Show all posts

Friday, November 25, 2011

VB.Net SqlServer DateTime Insertion

Dealing with Date/Time in VB.Net and SQLServer

It's been a nightmare since i decided to insert DateTime value into Sql Server 2000 DataBase Table Field Type as SmallDateTime.It took me 3 days and nights trying to work it around.I've did everything i know and everything other told me to do.Finally, i figured it out.
Look ....
Say, you have a Vb.Net 2005 Project with a form contains a DateTimePicker Control called (Q3) and Button Called (B3) Text Property as (Insert), and you have Sql Server 2000 DataBase called (Market) with Table Called (Purchase) includes a Field called (EXdt) Type as (SmallDateTime) Length (4), and you want to update it using Ado.Net Online Mode.
1) Calling The Ado.Net for Sql Server DataBase
Imports System.Data.SqlClient
2) Assigning your variables
Dim CN As New SqlClient.SqlConnection
Dim cPur As New SqlClient.SqlCommand
Dim CntRs As Integer
Dim SqlStr As String
3) Connecting your DataBase 'Market' - Form_Load Event

If CN.State = ConnectionState.Open Then CN.Close()
        CN.ConnectionString = "Data Source=Your_Sql_Server_Name;Initial Catalog=Market;Integrated Security=True"
        CN.Open()
4) Connecting your table 'Purchase' - Form_Load Event

SqlStr = "Select * from Purchase"
        With cPur
            .Connection = CN
            .CommandType = CommandType.Text
            .CommandText = SqlStr
        End With
5) Inserting the date within the DateTimePicker Control (Q3) Into Field (EXdt)
SqlStr = "Insert Into Purchase (ExDT) Values ('" & Q3.Value.ToString("yyyy/MM/dd") & "')"
cPur.CommandText = SqlStr

CntRs = cPur.ExecuteNonQuery()
        MsgBox("( " & CntRs & " ) Record(s) Saved")
Congratulations ....
Download The Source Code Via MediaFire Link

If you have any other problem contact me by Comments - or -
you can simply Google me for more solutions (evry1falls - Your Problem)


Visual Basic Courses - Share