1

我有这个作为我的连接字符串属性:

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

现在,我应该如何在我的代码中编写它:

sqlConnection = new SqlConnection(???);
4

3 回答 3

0

试试这个:

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();
     }
  }
于 2012-10-30T11:16:24.103 回答
0

字符串 connStr = ConfigurationManager.ConnectionStrings["connectionString"].ToString();

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

于 2013-07-30T12:01:55.177 回答
-1

使用 ConfigurationManager 和

看下面的代码行:

string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
于 2012-10-30T11:14:16.000 回答