ConnectionString Value Error

本文关键字:Error Value ConnectionString | 更新日期: 2023-09-27 17:58:40

我从本地数据库的属性中复制了ConnectString值。

属性中的连接字符串为:

Data Source=Cyber'SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;

当我直接将ConnectionString复制到ConnectString中时,我会得到一个错误。所以我去掉了"''",没有出现错误。然而,它仍然不起作用。我还注意到,人们通常会将ConnectionString值更改为一个单词,以使其变得容易。然而,我的VB属性部分不允许我更改它

public class SQLConnection
{
    #region MemberVariables
    private SqlConnection mConnection = null;
    private SqlDataAdapter mDataAdapter = null;
    private SqlCommand mCommand = null;
    static string mDbConnString = string.Empty;
    #endregion
    #region PublicMemberVariables
    public SqlConnection Connection
    {
        get
        {
            return mConnection;
        }
        set
        {
            mConnection = value;
        }
    }
    public SqlDataAdapter DataAdapter
    {
        get
        {
            return mDataAdapter;
        }
        set
        {
            mDataAdapter = value;
        }
    }
    public SqlCommand Command
    {
        get
        {
            return mCommand;
        }
        set
        {
            mCommand = value;
        }
    }
    public string ConnectString
    {
        get
        {
            return mDbConnString;
        }
        set
        {
            lock (mDbConnString)
            {
                mDbConnString = value;
            }
            lock (mConnection)
            {
                mConnection.ConnectionString = mDbConnString;
            }
        }
    }
    #endregion
    public void TestConnection()
    {
        ConnectString = "Data Source=Cyber SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True";
        Connection = new SqlConnection(ConnectString);
        Connection.Open();
        MessageBox.Show(Connection.State.ToString());
    }

}

ConnectionString Value Error

更改

Data Source=Cyber'SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;

Data Source=Cyber''SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;