简体   繁体   中英

Convert SQL connection from Asp classic to asp.net

I would like to convert my web pages from Asp classic to Asp.net (VB), What should I do instead of this code?

conn.asp:

<%
    Dim conn,connstr
        connstr = "Provider=sqloledb; Data Source=.\SQLEXPRESS ;Initial Catalog=My_DB ;User Id=sa;Password=12345"
        on error resume next
        set conn=server.createobject("ADODB.CONNECTION")
        conn.open connstr
            if err then
                err.clear
                set conn=nothing
                response.write "Connect Error!"
                response.End         
            End IF

    %>

Thank you for your time!

In C#

using (SqlConnection connection = new SqlConnection("Provider=sqloledb; Data Source=.\SQLEXPRESS ;Initial Catalog=My_DB ;User Id=sa;Password=12345"))
{
    connection.Open();
    // Do work here; connection closed on following line.
}

VB.NET

Using connection As New SqlConnection("Provider=sqloledb; Data Source=.\SQLEXPRESS ;Initial Catalog=My_DB ;User Id=sa;Password=12345")
    connection.Open()
    // Do work here; connection closed on following line. 
End Using 

Various connection strings can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM