ADO.NET Problem write or wrong

G

Guest

I am simple tryint to learn how to use the ADO.NET code. Can anybody tell me
if I am using it right or not?

Imports System.Data.sqlclient
Imports System.Data

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim conn As New SqlConnection("Server=apw-db;" & _
"DataBase=APW-System; Integrated Security=SSPI")
Dim sqlstr As String = "SELECT * FROM sysName"
Dim da As SqlDataAdapter
Dim ds As New DataSet
da = New SqlDataAdapter(sqlstr, conn)
da.Fill(ds)
Dim dt As DataTable = ds.Tables(0)

MsgBox(dt.Rows.Item("sysName")) 'trying to show what is in field

End Sub
End Class
 
C

Cor Ligthert [MVP]

Andrew,

Nothing wrong with in my idea, however I would do it in its simplest format
in this way.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim conn As New SqlConnection("Server=apw-db;" & _
"DataBase=APW-System; Integrated Security=SSPI")
Dim sqlstr As String = "SELECT * FROM sysName"
Dim da As new SqlDataAdapter(sqlstr, conn)
Dim ds As New DataSet Try
da.Fill(ds)
MessageBox.Show(ds.Tables(0).Rows.Item("sysName")) 'trying to show
what is in field
Catch ex as Exception
MessageBox.Show(ex.ToString)
End Try

I hope this helps,

Cor
 
B

Branco Medeiros

Andrew said:
I am using sql 2005 and vb 2005
<snip>

This worked for me: I first completely disregarded the several wizards
that come with VB 2005 and tried my hand in creating a strongly-typed
version of a database we have here. After struggling (and actually
succeeding) with this approach for a few days, I reckoned I had enough
control and knowledge of ADO.NET to give the designers a try. Currently
I'm using the designers for most new apps -- they're actually cool, if
you don't look under their dresses... =))

The experience with a hand-made strongly-typed db system tought me some
lessons and gave me a broarder perspective of the ADO.NET engine
workings. As they say, your mileage may vary. And it allowed me to
understand enough of it (I guess) to make it easier to extend the
functionality of the wizard generated items when necessary.

HTH.

Regards,

Branco.
 
C

Cor Ligthert [MVP]

Andrew,

Sorry, this can be
Cstr(ds.Tables(0).Rows(0).Item("sysName"))
ds.Tables(0).Rows(0).Item("sysName").ToString
Ctype(ds.Tables(0).Rows(0).Item("sysName"),String)

Most people including me use the ToString,

I have typed it in the message, looking at the major points.

I hope it will go now, again not tested just typed.

Cor
 
C

Cor Ligthert [MVP]

Andrew,
I found one of the problems there was no (0) after Rows in my code, but now
when I execute I get the Exception error:

I thought that I showed you that above. In what row is the exception error?.

Cor
 
G

Guest

I had make a mistake and mistyped what you had. but the error is coming from
row 71 on my app, the same row were I have the da.fill(ds).
 
S

Stephany Young

Instantiate da.


Andrew said:
here is the new code:


Imports System.Data.sqlclient
Imports System.Data

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim conn As New SqlConnection("Server=apw-db;" & _
"DataBase=APW-System; Integrated Security=SSPI")
Dim sqlstr As String = "SELECT * FROM sysMaster WHERE
sysName='IT_1'"
Dim da As SqlDataAdapter
Dim ds As New DataSet

Try
da.Fill(ds)

MessageBox.Show(ds.Tables(0).Rows(0).Item("sysName").ToString)
'trying to show what is in field
Catch ex As Exception
MessageBox.Show(ex.ToString)

End Try

End Sub
End Class
 
G

Guest

thanks for pointing that out, I was having a brian fart. At the time I got
this reply back I was noticing that there was no command execution for the
sql string. Not I am getting a reply back from the server, how ever I am not
use to loging in with windows authinication.
 
S

Stephany Young

When you are using the SqlClient objects, DO NOT include the Provider clause
in the connection string.

If you are using SQl Server authentication then the minimum connection
string is:

Data Source=<server>;Initial Catalog=<database>;User
ID=<username>;Password=<password>

where the tokens (<server>, <database>, <username> and <password>) are
replaced with your own values.

The 'new' method for doing an insert is to instantiate a SqlCommand object
and call it's ExecuteNONQuery method:

Dim _con As New SqlConnection(_connectionstring)

Dim _com As New SqlCommand(_insertsqlstring, _con)

_com.Parameters.Add(_parametername, _parametertype).Value =
_parametervalue
' Add all required parameters

_con.Open()

_com.ExceuteNonQuery()

_con.Close()

I suggest you study the documentation relating to the
System.Data.SqlClient.SqlCommand class along with the documentation for
other related classes.
 
C

Cor Ligthert [MVP]

Imports System.Data.sqlclient
Imports System.Data

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim conn As New SqlConnection("Server=apw-db;" & _
"DataBase=APW-System; Integrated Security=SSPI")
Dim sqlstr As String = "SELECT * FROM sysMaster WHERE
sysName='IT_1'"
Dim da As SqlDataAdapter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top