Writing sql code in vb.net

I have information entered into a textbox in an ASP.net 3.5 page. when i click submit button i would like this information to be written to sql server database.

Can someone please tell me what I need to do to get this done. The end user shouldn't see anything.

I am using visual web developer 2008. the event handling code is put in a separate file using vb.

Thanks.

-1


a source to share


5 answers


There are many different ways to achieve this goal: dynamic data, LinqToSQL, typed datasets, data access application block, or another ORM. My preferred method is straight sql, which will use code something like this:

Public Sub SaveAnswer(ByVal answer As String)
    Dim sql As String = "INSERT INTO [table1] (ans) VALUES (@Answer)"

    Using cn As New SqlConnection(getConnectionString()), _
          cmd As New SqlCommand(sql)

        cmd.Parameters.Add("@Answer", SqlDbType.VarChar, 50).Value = answer

        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

Private Function getConnectionString() As String
    ''//normally read from a config file for this

    Return "Server=(local)\SQLEXPRESS;Database=testdb;Trusted_Connection=True;"
End Function

      



A few examples taken from this example:

  • It closes the db connection correctly, even if an exception is thrown, via the block Using

  • Parameterized query to prevent sql injection
  • getConnectionString()

    is closed. You have to abstract data access to a single class or assembly, and this is one way to start doing this.
+3


a source


There are many ways to handle this. I would start with a few ADO.NET (.NET library uses to work with databases) resources:



+2


a source


It looks like you are new to ASP.NET. It is best to look for tutorials online for building ASP.NET database management applications. To answer your question right here, you are rewriting the more user-friendly written guides found elsewhere.

You should start with a few targeted Google searches like this one .

My advice should be to start with 2.0 and not 3.5 - although this is an older version, newer versions build on it, so you will be learning the basics that are still useful in 3.5.

+1


a source


Need to learn LINQ to SQL or ADO.NET (lower level)

0


a source


dot net 3.5 has a LINQ feature integrated with it ... you don't need to write sql code ... it's a SQL ORM that does the job for you

create a database (.mdb file) then the dbml file (which is datacontext) drags the tables you created in the data file into dbml ...

then u are ready to code ...

if dbml file name is master ... and table name is table1 and has column column1

then this is how you use it

dim db = new masterdatacontext()
dim c = new table1()
c.column1 = textbox1.text()
db.table1.insertonsubmit(c)
db.submitchanges()

      

thats it ...

0


a source







All Articles