1.11M Members

SQL Server Connection in vb.net

 
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

 
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?

 
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
 
0
 

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

 
0
 

nice

 
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
 
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.

 
0
 

how to put printscreen hir?

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