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

本文关键字:服务器 连接 字符串 windows Server Compact SQL | 更新日期: 2023-09-27 18:17:52

我使用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, int;vallength Boolean&isempty) + 1182System.Data.SqlServerCe.ConStringUtil。ParseStringIntoHashtable(String conString, Hashtable值)+188System.Data.SqlServerCe.ConStringUtil.ParseConnectionString (String&connectionString) + 128System.Data.SqlServerCe.SqlCeConnection。set_ConnectionString(字符串值)+679System.Data.SqlServerCe.SqlCeConnection . .ctor(String connectionString) +46System.Data.SqlServerCe.SqlCeDataAdapter . .ctor(String selectCommandText, String selectConnectionString) +73DataAccess。FillDataTable(String SQL) +149违约。Page_Load(对象发送者,EventArgs e) +378System.Web.Util.CalliEventHandlerDelegateProxy。回调(对象发送方,EventArgs e) +51System.Web.UI.Control。OnLoad(EventArgs e) +92System.Web.UI.Control.LoadRecursive () + 54System.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);
    }
}

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

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

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

 SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, sqlConn);

 SqlCeDataAdapter objAdapter = new SqlCeDataAdapter(SQL, connectionString);

您的封闭引号已关闭:

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="

应该是连接字符串的部分