locked
Sql Errors. Something with my connection string?? RRS feed

  • Question

  • Hey everyone.

    I have a database of clients that I'd like to search. I can edit and view the database fine. I setup the DataGridView for editing using VS mostly.  Now, I'm trying to write my search functionality, and somewhere along the way it's failing to connect. I am including my source code and the errors it produces.

        public DataTable SearchBasic(string str, string field)
        {
        using (SqlConnection conn = new SqlConnection("Data Source=|DataDirectory|ProjectManDB.sdf;Persist Security Info=False;")) 
        //using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString))
          {
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "SELECT * FROM clientele WHERE ' + @field + ' LIKE '%' + @str + '%'";
            cmd.Parameters.Add(new SqlParameter("@field", SqlDbType.NVarChar)).Value = field;
            cmd.Parameters.Add(new SqlParameter("@str", SqlDbType.NVarChar)).Value = str;
    
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            MessageBox.Show("Columns: " + dt.Columns.Count.ToString());
            try {
              da.Fill(dt);
            }
            catch (Exception e){
              Debug.WriteLine(e.ToString());
            }
    
            return dt;
          }
        }
    

    Now, I'll note here that the connection string is the exact same one used for my DataGridView that I previously setup using VS. Here are the errors:

    A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
    System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
      at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
      at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
      at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
      at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
      at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
      at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
      at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
      at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
      at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
      at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
      at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
      at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
      at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
      at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
      at System.Data.SqlClient.SqlConnection.Open()
      at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
      at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
      at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
      at ProjectMan.AppCode.DataOps.SearchOps.SearchBasic(String str, String field) in C:\Users\King Inky\Documents\Visual Studio 2010\Projects\ProjectMan\ProjectMan\AppCode\DataOps\SearchOps.cs:line 38


    --Ethan Barron
    Thursday, July 29, 2010 10:58 PM

Answers

  • .sdf files are SQL Server Compact Edition files.  However, SqlConnection is for the full SQL Server.  You need System.Data.SqlServerCe.SqlCeConnection.

     

    • Proposed as answer by DK. Da Friday, July 30, 2010 9:58 AM
    • Marked as answer by Figo Fei Monday, August 2, 2010 3:31 AM
    Friday, July 30, 2010 12:12 AM

All replies

  • .sdf files are SQL Server Compact Edition files.  However, SqlConnection is for the full SQL Server.  You need System.Data.SqlServerCe.SqlCeConnection.

     

    • Proposed as answer by DK. Da Friday, July 30, 2010 9:58 AM
    • Marked as answer by Figo Fei Monday, August 2, 2010 3:31 AM
    Friday, July 30, 2010 12:12 AM
  • Hi ,

    Please ensure whether you have the enough privileges to access the goal database.

     

    Thanks

    Larcolais Gong

    Friday, July 30, 2010 9:36 AM
  • I marked the previous reply. Please feel free to unmark it if the problem isn't solved and you have more info to share for further investigation.
    Thanks.

    Figo Fei


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, August 2, 2010 3:31 AM