1

I have this as my connection string property:

Data Source="c:\users\perdanny\documents\visual studio 2012\Projects\WebApplication1\WebApplication1\App_Data\Users.sdf"

Now, how should I write it in my code:

sqlConnection = new SqlConnection(???);
2
  • Similar question asked before: stackoverflow.com/questions/8014901/… Commented Oct 30, 2012 at 11:10
  • If you've got an sdf file rather than a SQL Server database, chances are you want SQLCeConnection rather than SQLConnection. Commented Oct 30, 2012 at 12:35

3 Answers 3

0

try this:

string strConnection = ConfigurationManager.ConnectionStrings["Name of connection string key"].ConnectionString;  

// Or, for a quick test you could also use
// string strConnection = "Data Source = c:\\users\\perdanny\\documents\\visual studio 2012\\Projects\\WebApplication1\\WebApplication1\\App_Data\\Users.sdf"     

  using (var conn = new SqlCeConnection(string.Format("Data Source={0}", strConnection)))
  {
     conn.Open();

     try
     {
         System.Data.SqlServerCe.SqlCeCommand cmd = System.Data.SqlServerCe.SqlCeCommand;
         cmd.CommandType = System.Data.CommandType.StoredProcedure;
         cmd.CommandText = "Your sql stored proc name";
         cmd.Connection = conn ;
         cmd.ExecuteNonQuery();     
     }
     catch (SqlCeException)
     {
         throw;
     }
     finally
     {
         if (conn.State == ConnectionState.Open) conn.Close();
     }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

string connStr = ConfigurationManager.ConnectionStrings["connectionString"].ToString();

<add name="connectionString" connectionString="Data Source=SQLEXPRESS;Initial Catalog=dbsql;User ID=hello;Password=hello"
      providerName="System.Data.SqlClient" />

Comments

-1

use ConfigurationManager and

look at below code line:

string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.