为什么我的数据库没有连接?

时间:2015-12-09 06:46:39

标签: sql-server vb.net

我是vb.net的新手,我正在尝试连接sql server。

我的连接字符串是:

 <add name="ConString" connectionString="Data Source=LV-SC294\SQLEXPRESS;Initial Catalog=master;Persist Security Info=True;User ID=1111;Password=111111" providerName="System.Data.SqlClient"/>

这是我连接到服务器的DBConnection文件,

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.Provider
Imports System.Configuration
Imports System.Web.Services
Imports System.Data.Odbc

Namespace CATTool
Public Class DBConnection
    Protected con As OdbcConnection
    Dim b As Boolean
    Protected rs As Object

    Public Function Open(Optional ByVal connection As String = "ConString") As Boolean
        con = New OdbcConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
        Try
            b = True
            If (con.State.ToString <> "open") Then
                con.Open()
            End If
            Return b

        Catch ex As Exception
            Return False
        End Try
    End Function

最后这里是我调用我的DbConnection的地方..这里我试图建立与服务器的连接,并执行查询以获取行。怎么没有建立连接。它不断抛出信息&#34; Connection没有建立&#34;

 Imports System.Data
 Imports System.Data.SqlClient
 Imports System.Web.Services
 Imports CAT_Tool.CATTool
 Imports Newtonsoft.Json
 Imports Newtonsoft.Json.Linq

 Namespace CAT_Tool


Public Class SummaryController

    Inherits System.Web.Mvc.Controller

    '
    ' GET: /Summary
    Protected DB As DBConnection = New DBConnection()
    Private Property Val As Object
    Function Index() As ActionResult
        Return View()
    End Function
    Function Summary() As ActionResult
        Return View()
    End Function
    <WebMethod()> _
    Public Function Get_Territory()
        MsgBox("Filling Territory")
        Dim Query As String = "select distinct Territory from     dbo.CWS_WEBTOOL_USG"
        Dim dbcon As Boolean = DB.Open()
        If (dbcon = True) Then
            MsgBox("Connection Established")
            Val = DB.selectdata(Query)
            DB.Close()
            Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim rows As New List(Of Dictionary(Of String, Object))()
            Dim row As Dictionary(Of String, Object)
            For Each dr As DataRow In Val.Rows
                row = New Dictionary(Of String, Object)()
                For Each col As DataColumn In Val.Columns
                    row.Add(col.ColumnName, dr(col))
                Next
                rows.Add(row)
            Next
            MsgBox("Returning values")
            MsgBox(rows)
            Return serializer.Serialize(rows)

        Else
            MsgBox("Connection Did'nt Establish")
            Return Content(dbcon.ToString)
        End If
        Return View()
    End Function

我的sql server详细信息如下:

ServerName = LV-SC294 \ SQLEXPRESS 身份验证:SQLServer身份验证

1 个答案:

答案 0 :(得分:0)

我同意@ AllanS.Hansen最好使用System.Data.SqlClient从托管代码访问SQL Server。您的连接字符串是正确的SqlClient连接字符串,尽管通常指定应用程序数据库而不是主系统数据库。

如果要使用ODBC(例如,支持不同DBMS系统的相同代码),则需要更改配置文件中的连接字符串和提供程序。例如,使用最新的SQL Server ODBC Driver

<add name="ConString" connectionString="Driver={ODBC Driver 11 for SQL Server},Server=LV-SC294\SQLEXPRESS;Database=YourDatabase;UID=1111;PWD=111111" providerName="System.Data.Odbc"/>