将SQL连接从Asp classic转换为asp.net

时间:2013-06-04 06:34:01

标签: asp.net sql-server sql-server-2008 asp-classic

我想将我的网页从Asp classic转换为Asp.net(VB),我应该怎么做而不是这段代码?

的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

    %>

感谢您的时间!

1 个答案:

答案 0 :(得分:1)

在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 

可以找到各种连接字符串here