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.
LVL 67
sirbountyAsked:
Who is Participating?
 
SandeepRRConnect With a Mentor Commented:
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
 
YZlatConnect With a Mentor Commented:
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:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.