SQL Server Compact的连接字符串 - Windows主服务器

时间:2013-06-10 23:18:20

标签: c# sql sql-server-ce connection-string

我正在使用SQL Server Compact 4.0并在Windows Home Server 2011上托管该站点,程序是用C#编写的 - Visual Web Developer。

我已经尝试了所有可能的连接字符串变体,但我仍然会遇到此错误:

  

初始化字符串的格式不符合从索引0开始的规范。

这是我目前的字符串

<add name="Inventory" 
     connectionString="Data Source=C:\Program Files\Windows Server\Bin\WebApps\Site\App_Data\PHSInventory.sdf;"  /  >

我也尝试用|data directory|指出它。我在Google上用完了蓝色链接点击。

  

STACK TRACE

     

[ArgumentException:初始化字符串的格式不符合从索引0开始的规范。]      System.Data.SqlServerCe.ConStringUtil.GetKeyValuePair(Char [] connectionString,Int32 currentPosition,String&amp; key,Char [] valuebuf,Int32&amp; vallength,Boolean&amp; isempty)+1182      System.Data.SqlServerCe.ConStringUtil.ParseStringIntoHashtable(String conString,Hashtable values)+188      System.Data.SqlServerCe.ConStringUtil.ParseConnectionString(String&amp; connectionString)+128      System.Data.SqlServerCe.SqlCeConnection.set_ConnectionString(String value)+679      System.Data.SqlServerCe.SqlCeConnection..ctor(String connectionString)+46      System.Data.SqlServerCe.SqlCeDataAdapter..ctor(String selectCommandText,String selectConnectionString)+73      DataAccess.FillDataTable(String SQL)+149      Default.Page_Load(Object sender,EventArgs e)+378      System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)+51      System.Web.UI.Control.OnLoad(EventArgs e)+92      System.Web.UI.Control.LoadRecursive()+54      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+772

数据访问

using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.Configuration;

using System.Data.SqlServerCe;

public class DataAccess
{

    private int count = -1;

    public int Count
    { get { return count; } }

    private string connectionString;
    public string ConnectionName
    {
        set
        {
            if (WebConfigurationManager.ConnectionStrings[value] == null)
            {
                throw new Exception("Cannot find connection string named '" +
                   value + "' in web.config");
            }
            //Retrieve connection string from web.config.
            connectionString =
               WebConfigurationManager.ConnectionStrings[value].ConnectionString;
        }
    }

    public DataAccess(string connectionName)
    {
        ConnectionName = connectionName;
    }

    public DataTable FillDataTable(string SQL)
    {
        // Fill dataTable.
         WriteToTraceLogForDebugging(SQL);
        SqlCeConnection sqlConn = new SqlCeConnection("Data Source=D:\\InventoryData\\PHSInventory.sdf;");
        SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, sqlConn);
        DataTable dt = new DataTable();
        objAdapter.Fill(dt);
        count = dt.Rows.Count;  
        return dt;
    }
    public int ExecuteNonQuery(string SQL)
    {
        WriteToTraceLogForDebugging(SQL);
        SqlCeConnection sqlConn = new SqlCeConnection(connectionString);
        sqlConn.Open();
        SqlCeCommand objCmd = new SqlCeCommand(SQL, sqlConn);
        count = objCmd.ExecuteNonQuery();
        return count;
    }

    public object ExecuteScalar(String SQL)
    {
        WriteToTraceLogForDebugging(SQL);
        SqlCeConnection sqlConn = new SqlCeConnection(connectionString);
        sqlConn.Open();
        SqlCeCommand objCmd = new SqlCeCommand(SQL, sqlConn);
        //calling code needs to cast data (to integer, string, double, etc.) upon receipt. 
        return objCmd.ExecuteScalar();
    }

    private void WriteToTraceLogForDebugging(string sql)
    {
        System.Web.HttpContext.Current.Trace.Write("sql****", sql);
        System.Web.HttpContext.Current.Trace.Write("CONNECTION*****", connectionString);
    }

}

2 个答案:

答案 0 :(得分:3)

您正在使用System.Data.SqlConnection类,对于SQL Server Compact,您必须使用System.Data.SqlServerCe类,如SqlCeConnection。

然后你构建的SqlCeDataAdapter错误,你应该使用:

 SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, sqlConn);

 SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, connectionString);

答案 1 :(得分:2)

您的封闭引号已关闭:

Data Source="C:\Program Files\Windows Server\Bin\WebApps\Site\App_Data\PHSInventory.sdf;"

应该是:

"Data Source=C:\Program Files\Windows Server\Bin\WebApps\Site\App_Data\PHSInventory.sdf;"

请注意,您的初始字符串会尝试在实际连接字符串之前将Data Source作为文字放置:

connectionString=Data Source="

连接字符串的部分应该在哪里。