SQL Server 2005 using VB.NET 2005

5 pts.
Tags:
SQL Server 2005
VB.NET
VB.NET 2005
Visual Basic
Visual Basic 2005
How do I retrieve, add and update data from SQL Server 2005 manually using VB.NET 2005?

Answer Wiki

Thanks. We'll let you know when a new response is added.
dim mstrsql as string=""
mstrsql="insert into sqltablename column1,column2 values(tablecolumns txtbox.text,textbox2.text )

''To execute query
If ConSubs.State = ConnectionState.Open = True Then

ConSubs.Close()

End If

ConSubs.Open()

com = New SqlCommand(sQuery, ConSubs)
com.CommandTimeout = 5000
adap.SelectCommand = com
com.ExecuteNonQuery()

———–
To Retrieve

 mstrsql = "select value1,value2 from sql table name where condition "

'Passing SQL Query and SQL Connection into SQL DataAdapter
adap = New SqlDataAdapter(strQuery, conTemp)
adap.SelectCommand.CommandTimeout = 600
'Retriving the Records into Dataset Table
gfnintFillDataSet = adap.Fill(dsTemp, strTbl)

if gfnintFillDataSet > 0 then
datagridview.datasource=dsten\mp.strTbl
endif

Discuss This Question: 8  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Denny Cherry
    Define "manually"
    66,365 pointsBadges:
    report
  • Denny Cherry
    If using the sample code above don't forget to account for SQL Injection.
    66,365 pointsBadges:
    report
  • Bbm
    [...] If you were wondering how to retrieve, add and update data from SQL Server 2005 manually using VB.NET 2005, Pkaur has some sample code, though Mrdenny warns against SQL [...]
    0 pointsBadges:
    report
  • SimpleMind
    1)for connection to sql dbase -->dim con as sqlconnection=new sqlconnection("Data Source= .SQLEXPRESS;INITIAL CATALOG=<sql database name not tablename>;Intregrated Security=SSPI;") note . takes servername of current machine 2)to add into databse dim sqlquery1 as string="Insert into <dbase table name>values(' " & stringtxt.text & " ', " & numericonly.text & ")" dim cmd as new sqlcommand=new sqlcommand(sqlquery1,con) cmd.executenonquery() cmd.dispose() note ;look at diff in ' while passing number only data. 3)To update sqlquery1="update <tablename> set <columnname>=' " & string.text & " ',<columnname>=" & numericonly.text & " " cmd=new sqlcommand(sqlquery1,con) cmd.executenonquery() cmd.dispose() 4)to retrieve it is long process i got to go to peee..........
    30 pointsBadges:
    report
  • SimpleMind
    1)for connection to sql dbase –>
    dim con as sqlconnection=new sqlconnection(”Data Source= .SQLEXPRESS;INITIAL CATALOG=<sql database name not tablename>;Intregrated Security=SSPI;”)
    note . takes servername of current machine 2)to add into databse
    dim sqlquery1 as string="Insert into <dbase table name>values(’ ” & stringtxt.text & ” ‘, ” & numericonly.text & “)”
    dim cmd as new sqlcommand=new sqlcommand(sqlquery1,con)
    cmd.executenonquery()
    cmd.dispose()
    note ;look at diff in ‘ while passing number only data. 3)To update
    sqlquery1="update <tablename> set <columnname>=’ ” & string.text & ” ‘,<columnname>=" & numericonly.text & ” ”
    cmd=new sqlcommand(sqlquery1,con)
    cmd.executenonquery()
    cmd.dispose()
    4)to retrieve
    dim c as integer
    'selecting the entire rows from dbase
    sqlquery1 = " select count(*) from <tablename>"
    cmd=new sqlcommand(sqlquery1,con)
    c=cmd.executescalar
    cmd.dispose()
    
    if c<> 0 then
    datagridview1.rows.clear()
    sqlquery1="select  <col name>,.....,<col name> from <table name> order by <primary column>"
    cmd=new sqlcommand(sqlquery1,con)
    dim dr as sqldatareader=cmd.sxecutereader
    
    while dr.read
    records.rows.add(New String() {dr.item(0).to string,.................dr.item(n).to string })
    enb while
    dr.close()
    cmd.dispose()
    end if
    better make sqlquery1 global variable. Hope this will help you !!
    30 pointsBadges:
    report
  • dil1234
    Could you help me what is" dsTemp, strTbl" 
    40 pointsBadges:
    report
  • dil1234
    I use this code to retrive data but this did n't work can any body help meDim major As String = TextBox1.Text        Dim depa As String = TextBox2.Text        Dim sname As String = TextBox3.Text        Dim tbStudent As DataTable        Dim dcPrimaryKey(0) As DataColumn        tbStudent = sqlDataset.Tables("Major")        Dim c As Integer        'selecting the entire rows from dbase        myCommand = New SqlCommand("SELECT  count(*) FROM Major", myConnection)        myConnection.Open()        'sqlquery1 = " select count(*) from <tablename>"        ' cmd = New SqlCommand(sqlquery1, con)        c = myCommand.ExecuteScalar        Dim drSubject As System.Data.DataRow        drSubject = tbStudent.Rows.Find(major)        If c <> 0 Then            ' datagridview1.rows.clear()            myCommand = New SqlCommand("SELECT  MajorName,Departement,Suject_Name from Major order by MajorName", myConnection)            Dim dr As SqlDataReader = myCommand.ExecuteReader            '''''check from here            While dr.Read                tbStudent.Rows.Add()                TextBox1.Text = drSubject.Item("MajorName")                TextBox2.Text = drSubject.Item("Departement")                TextBox3.Text = drSubject.Item("Suject_Name")                           End While        End If        myConnection.Dispose()
    40 pointsBadges:
    report
  • dil1234
    I use this code to retrive data but this didn’t work can any body could help me...............Dim major As String = TextBox1.Text        Dim depa As String = TextBox2.Text        Dim sname As String = TextBox3.Text        Dim tbStudent As DataTable        Dim dcPrimaryKey(0) As DataColumn        tbStudent = sqlDataset.Tables(“Major”)        Dim c As Integer        ‘selecting the entire rows from dbase        myCommand = New SqlCommand(“SELECT  count(*) FROM Major”, myConnection)        myConnection.Open()        ‘sqlquery1 = ” select count(*) from <tablename>”        ‘ cmd = New SqlCommand(sqlquery1, con)        c = myCommand.ExecuteScalar        Dim drSubject As System.Data.DataRow        drSubject = tbStudent.Rows.Find(major)        If c <> 0 Then            ‘ datagridview1.rows.clear()            myCommand = New SqlCommand(“SELECT  MajorName,Departement,Suject_Name from Major order by MajorName”, myConnection)            Dim dr As SqlDataReader = myCommand.ExecuteReader            ””’check from here            While dr.Read                tbStudent.Rows.Add()                TextBox1.Text = drSubject.Item(“MajorName”)                TextBox2.Text = drSubject.Item(“Departement”)                TextBox3.Text = drSubject.Item(“Suject_Name”)                           End While        End If        myConnection.Dispose()
    40 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following