Search VB 2010

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)


2 comments:

  1. Hey there would you mind sharing which blog platform you're using? I'm looking
    to start my own blog soon but I'm having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I'm looking
    for something unique. P.S Sorry for
    being off-topic but I had to ask!

    Review my web page: meilleur du porno (pagesfriends.com)

    ReplyDelete
  2. To many people, this stuff can be mastered with instructors and in special programs but then parents shouldn't worry since they can do it quickly.
    Compact table tennis technique technology folding
    system for easy storage. Nor does it require significant modifications to
    remain open to everyone.

    Feel free to surf to my site :: table tennies guide

    ReplyDelete

Visual Basic Online Course

Visual Basic Courses - Share