[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

How to set up a sql adapter

I'm using vb.net 2003 and I would like some help setting up a sql data adapter.
0
sirbounty
Asked:
sirbounty
  • 2
2 Solutions
 
YZlatCommented:
Dim conn As SqlConnection
        Dim ds As New DataSet
        Dim query As String

        query = "SELECT * FROM Table"
        Try
            conn = New SqlConnection("your connection string here")

            conn.Open()
            Dim da As SqlDataAdapter = New SqlDataAdapter(query, conn)
         
            ''fill dataset
            da.Fill(ds)
        Catch ex As SqlException
            'Catch ex As SqlException
            Console.Write("SQL ERROR: " & ex.Message)
        Catch ex As Exception
            Console.Write("ERROR: " & ex.Message)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try
0
 
SandeepRRCommented:
Hi sirbounty,
There are 3 steps to set the adapter

define connnections
define the queries for Data retrivals data update delete and insert
define Dataadpter and assign the command to the appropriate daatadapter property

example for select command

private SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
private SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select documentType from LastItemId";
cmd.Connection = conn;
private SqlDataAdapter da = new SqlDataAdapter();
da.selectcommand = cmd;

and now if u want to fill the dataset then


DataSet ds = new Dataset();
da.fill(ds, "MyTable");

and after that u can assign the data to  datagrid

grid.datasource = ds.tables[0];
grid.databind();

Regards
SandeepRR
0
 
SandeepRRCommented:
The same way u can go for update delete and insert command

da.UpdateCommand = updatecommand
da.insertcommand = insertcommand
da.deletecommand = deletecommand

and u can call this 3 methods in one call
da.update();

SandeepRR
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now