Hi all,

I would like to seek for some help from you on how to create a connection to a sql server database from an asp.net application using VB codes. I appreciate if you could provide me the relevant information about that with some sample codes.

Thank you.

Dani AI

Generated

As requested: a simple, reliable pattern for connecting an ASP.NET (VB) app to SQL Server. ’s OLE DB example is workable, but for native SQL Server support and clearer code use ADO.NET’s SqlClient. Keep connection strings out of source files, always use parameterized commands, and ensure connections are disposed (Using...End Using).

Add a connection string to Web.config:

<connectionStrings>
  <add name="MyConn"
       connectionString="Server=SERVERNAME;Database=DBNAME;Integrated Security=True;"
       providerName="System.Data.SqlClient" />
</connectionStrings>

Example VB.NET ADO.NET pattern (safe, minimal):

Imports System.Data.SqlClient
Imports System.Configuration

Dim cs As String = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString

Using conn As New SqlConnection(cs)
    Using cmd As New SqlCommand("SELECT FirstName FROM Users WHERE UserId = @id", conn)
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = userId
        conn.Open()
        Dim firstName As String = Convert.ToString(cmd.ExecuteScalar())
    End Using
End Using

Notes and quick troubleshooting

  • Use SqlDataReader for forward-only reads, SqlDataAdapter/DataTable for binding.
  • If using SQL authentication set User ID=...;Password=... in the connection string; prefer Integrated Security when possible.
  • Common failures: wrong server name/instance, TCP/IP disabled, firewall blocking port 1433, or insufficient DB permissions for the app pool identity. Test the same connection in SQL Server Management Studio first.
  • Never concatenate user input into SQL; always use parameters. Encrypt connectionStrings in Web.config or use secure secrets storage for production credentials.

For ASP.NET Core or recent .NET versions, use dependency injection and the Microsoft.Data.SqlClient package or an ORM (Entity Framework Core) — the ADO.NET patterns above still apply conceptually.

Dim strConn As String = "Provider=SQLOLEDB.1;Password=xxxx;Persist Security Info=True;User ID=xxx;Initial Catalog=Your database name;Data Source=Your server Name"
            Dim strsql As String = "Your SQL Command"
            Dim myconn As New OleDbConnection(strConn)
            Dim myCommand As New OleDbDataAdapter(strsql, myconn)

And don't forget to add
Imports System.Data.OleDb
Above your public class(On the top of the page

^^

hi every one,hop your fine and thanks for the above mentioned lessons i really need that,so could you plz make the books of these web devoloping with this lesson it will be very usefull .
thank bashirrafi from afghanistan on of the most the fan of your site

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.