0

我正在使用 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|. 我已经用完了谷歌上的蓝色链接来点击。

堆栈跟踪

[ArgumentException: 初始化字符串的格式不符合从索引 0 开始的规范。] System.Data.SqlServerCe.ConStringUtil.GetKeyValuePair(Char[] connectionString, Int32 currentPosition, String& key, Char[] valuebuf, Int32& vallength, Boolean& isempty ) +1182 System.Data.SqlServerCe.ConStringUtil.ParseStringIntoHashtable(String conString, Hashtable values) +188 System.Data.SqlServerCe.ConStringUtil.ParseConnectionString(String& 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 默认。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(布尔includeStagesBeforeAsyncPoint,布尔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);
    }

}
4

2 回答 2

3

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

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

 SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, sqlConn);

或者

 SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, connectionString);
于 2013-06-11T08:03:59.223 回答
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="

where is 应该是连接字符串的一部分

于 2013-06-10T23:24:01.333 回答