New table create in MsSql sever database
SqlQuery :-
create table DEPARTMENTS(
Department_id varchar(50),
Department_name varchar(50),
Manager_id varchar(50),
Location_id varchar(50));
Textbox1 = txtDepId
Textbox2 = txtDepName
Textbox3 = txtManId
Textbox4 = txtLocId
Complete Source Code
Imports System.Data.SqlClient
Public Class Form1
Dim conString As String = "Server=DELL-PC\SQLEXPRESS;Initial Catalog=abc;_
Integrated Security=True"
Dim conn As SqlConnection = New SqlConnection(conString)
Dim adap As New SqlDataAdapter
Dim ds As New DataSet
Private Sub Button1_Click(sender As System.Object, e As Global.System.EventArgs) Handles Button1.Click
Try
Dim selCmdText As String = "SELECT *FROM DEPARTMENTS"
Dim selCommand As New SqlCommand(selCmdText, conn)
Dim insCmdText As String = "INSERT INTO DEPARTMENTS(Department_id,Department_name,Manager_id,Location_id)" & "VALUES(@DepId,@DepName,@ManId,@LocId)"
Dim insCommand As New SqlCommand(insCmdText, conn)
Dim adp As New SqlDataAdapter(selCommand)
adp.InsertCommand = insCommand
Dim param As New SqlParameter
param.ParameterName = "@DepId"
param.SqlDbType = SqlDbType.VarChar
param.Value = Integer.Parse(txtDepId.Text)
insCommand.Parameters.Add(param)
param = New SqlParameter
param.ParameterName = "@DepName"
param.SqlDbType = SqlDbType.VarChar
param.Size = 15
param.Value = txtDepName.Text
insCommand.Parameters.Add(param)
param = New SqlParameter
param.ParameterName = "@ManId"
param.SqlDbType = SqlDbType.VarChar
param.Value = Integer.Parse(txtManId.Text)
insCommand.Parameters.Add(param)
param = New SqlParameter
param.ParameterName = "@LocId"
param.SqlDbType = SqlDbType.VarChar
param.Value = Integer.Parse(txtLocId.Text)
insCommand.Parameters.Add(param)
Dim dt As New DataTable
adp.Fill(dt)
Dim newRo As DataRow = dt.NewRow
newRo.Item("Department_id") = Integer.Parse(txtDepId.Text)
newRo.Item("Department_name") = txtDepName.Text
newRo.Item("Manager_id") = Integer.Parse(txtManId.Text)
newRo.Item("Location_id") = Integer.Parse(txtLocId.Text)
dt.Rows.Add(newRo)
adp.Update(dt)
Catch ex As Exception
MsgBox("connection failed" & Err.Description)
End Try
End Sub
End Class
No comments:
Post a Comment