1,105,538 Community Members

SQL Server Connection in vb.net

Member Avatar
apking
Newbie Poster
2 posts since Dec 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Friends

Iam newbie to vb.net .iam creating a registration form.iam also creating database table in sql server.now how to store record in that table.how to do this?


regards
apking

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
1
 

make a module to connect vb.net with sql server.
add this code to module :

Imports System.Data
Imports System.Data.SqlClient

Module Connect
    Public conn As SqlConnection
   
    Public Function GetConnect()

           conn = New SqlConnection("Server = YourServerName;" & "initial Catalog = yourDatabaseName;" & " Trusted_Connection=yes")

        Return conn
    End Function
End Module

you can call the function on the module to connect with sqlserver on each form. so u can open connection and close.
like this ex:

conn = GetConnect()
conn.Open()
.....
.....
conn.Close()

What a registration form you want to build?
put a print screen?

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

this simple following code to add/store data to sqlserever :

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim check As Integer
        Dim conn As SqlConnection
        Dim cmdmedical As New SqlCommand
        Dim cmdmedical1 As New SqlCommand
        Dim damedical As New SqlDataAdapter
        Dim dsmedical As New DataSet
        Dim dtmedical As New DataTable

        If cmbNis.SelectedIndex = -1 Or cmbBloodType.SelectedIndex = -1 Or txtdisease.Text = "" Or txtAnomalyBody.Text = "" Or txtHeight.Text = "" Or txtBerat.Text = "" Then
            MsgBox("Student Medical Data is not completed", MsgBoxStyle.OKOnly)
        Else
            If MsgBox("Are you sure to store student medical data with Nis : " & cmbNis.SelectedItem & " ?", MsgBoxStyle.OKCancel, "Input confirm") = MsgBoxResult.Cancel Then
                ' do nothing
            Else
                Try
                    conn = GetConnect()
                    conn.Open()
                    cmdmedical = conn.CreateCommand
                    cmdmedical.CommandText = "SELECT * FROM medical WHERE Nis='" & Trim(cmbNis.SelectedItem) & " ' "
                    damedical.SelectCommand = cmdmedical
                    damedical.Fill(dsmedical, "medical")
                    dtmedical = dsmedical.Tables("medical")

                    If (dtmedical.Rows.Count > 0) Then
                        MsgBox("medical dengan Nis " & Trim(cmbNis.Text) & " Sudah ada didata base", MsgBoxStyle.OKOnly, "Message :")
                    Else
                        
                        cmdmedical1 = conn.CreateCommand
                        cmdmedical1.CommandText = "INSERT INTO medical(Idmedical, Nis, BloodType, Disease,AnomalyBody,Height,Weight) VALUES('" & Trim(idmedical.Text) & "','" & Trim(cmbNis.SelectedItem) & "','" & Trim(cmbBloodType.SelectedItem) & "','" & Trim(txtdisease.Text) & "','" & Trim(txtAnomalyBody.Text) & "','" & Trim(txtHeight.Text) & "','" & Trim(txtWeight.Text) & "')"
                        check = cmdmedical1.ExecuteReader.RecordsAffected()
                        If check > 0 Then
                            MsgBox("Student with Nis " & Trim(cmbNis.Text) & " succesfully added", MsgBoxStyle.OKOnly, "Message :")
                        Else
                            MsgBox("Student with Nis " & Trim(cmbNis.Text) & " failure to added", MsgBoxStyle.OKOnly, "Message :")
                        End If
                        Refresh_Form()
                        conn.Close()
                    End If
                Catch ex As Exception
                    MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error!!")
                End Try
            End If
        End If
    End Sub
Member Avatar
Malcolm Momin
Newbie Poster
1 post since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want a full tutorial for sql connection from the beginning that means from the designing part.

Member Avatar
bryt
Newbie Poster
2 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

nice

Member Avatar
Netcode
Veteran Poster
1,049 posts since Jun 2009
Reputation Points: 33 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 8 [?]
 
0
 
imports system.data 
imports system.data.sqlclient 

Public class form1 

private sub button1_click (byval sender as system.object.....)
dim con as sqlconnection = new sqlconnection ( _
"server = server name"; database = dbase name;  & _
user id = server id; password = server password;")

dim objcommand as sqlcommand =  new sql command 

with objcommand 
.connection =  con
.commandtext = "INSERT INTO tablename" & _
"(enter fields with comma seperating them)" & _
"VALUES (values for each field with a comma just as above)"
end with 
con.open()
objcommand.executenonquery()
con.close
Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

We are glad you got it helpful.Please do not resurrect old threads. If you have any questions please ask. You are welcome to start your own threads. Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Locked.

Member Avatar
khia02
Newbie Poster
3 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

how to put printscreen hir?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article