2011-02-02 20 views
10

मैं अपने एएसपी.NET एप्लिकेशन में SQL सर्वर कॉम्पैक्ट 4.0 से कनेक्ट करना चाहता हूं।ASP.NET में SQL सर्वर कॉम्पैक्ट 4.0 से कैसे कनेक्ट करें?

यहाँ कोड का उदाहरण है:

protected void Page_Load(object sender, EventArgs e) 
{ 
    string connStr = "Data Source=D:\\MyDB.sdf;"; 
    string sqlStr = "select * from tblMyTable"; 

    var sqlDataSrc = new SqlDataSource(connStr, sqlStr); 

    GridWithUrls.DataSource = sqlDataSrc; 
    GridWithUrls.DataBind(); 
} 

लेकिन मैं अगले त्रुटि है: "जबकि एसक्यूएल server.The सर्वर से कनेक्शन स्थापित करने नहीं मिला था एक नेटवर्क से संबंधित या उदाहरण के विशेष त्रुटि हुई या सत्यापित नहीं था। सत्यापित करें कि इंस्टेंस नाम सही है और SQL सर्वर दूरस्थ कनेक्शन की अनुमति देने के लिए कॉन्फ़िगर किया गया है। (प्रदाता: एसक्यूएल नेटवर्क इंटरफेस, त्रुटि: 26 - सर्वर/इंस्टेंस निर्दिष्ट करने में त्रुटि निर्दिष्ट) "

एसक्लडाटासोर्स के साथ निर्माता है तीन पैरामीटर, उनमें से एक 'प्रदाता नाम' है, तो मैं इसे कैसे निर्दिष्ट करूं निश्चित रूप से एसक्यूएल सर्वर कॉम्पैक्ट प्रदाता का उपयोग करना चाहते हैं? इसके अलावा, मैं जोड़ लिया है System.Data.SqlServerCe संदर्भ ..

उत्तर

22

प्रयास करें:

providerName = "System.Data.SqlServerCe.4.0" 
+0

धन्यवाद, यह आपको समझ में आया – Yara

0

उदाहरण यहाँ पर एक नज़र डालें:

http://connectionstrings.com/sql-server-2005-ce

आप स्थान को निर्दिष्ट करना चाहते हैं स्पष्ट रूप से प्रयास करें:

Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyDB.sdf;Persist Security Info=False; 
0

आप वें खींच सकते हैं ई डेटा को पहले डाटाबेस में:

 using (SqlCeConnection c = new SqlCeConnection(
      Properties.Settings.Default.DataConnectionString)) 
     { 
      c.Open(); 

      // Create new DataAdapter 
      using (SqlCeDataAdapter a = new SqlCeDataAdapter(
       "SELECT * FROM tblMyTable", c)) 
      { 

       // Use DataAdapter to fill DataTable 
       DataTable t = new DataTable(); 
       a.Fill(t); 

       // Render data onto the screen 
       dataGridView1.DataSource = t; 
      } 
     } 
2

एकल डेटाबेस फ़ाइल रखें (उदा। MySite.sdf) App_Data फ़ोल्डर में।

web.config करने के लिए एक connectionStrings प्रविष्टि जोड़ें डेटाबेस के लिए कनेक्शन की अनुमति के लिए:

web.config:

<configuration> 
    <connectionStrings> 
     <add name="db" 
      connectionString="Data Source=|DataDirectory|\MySite.sdf" 
      providerName="System.Data.SqlServerCe.4.0" /> 
    </connectionStrings> 
    ... 
</configuration> 

और फिर आप एक कनेक्शन के रूप कनेक्शन स्ट्रिंग nameडाटाबेस के माध्यम से वांछित बना सकते हैं:

public static DbConnection CreateConnection() 
{ 
    //Get connection string named "db" 
    String csName = "db"; 
    ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings[csName]; 
    if (cs == null) 
     throw new ConfigurationErrorsException("Could not find connection string \"" + csName + "\""); 

    //Get a factory based on the "ProviderName" in the connection string 
    DbProviderFactory factory = DbProviderFactories.GetFactory(cs.ProviderName); 
    if (factory == null) 
     throw new Exception("Unable to locate factory for " + cs.ProviderName); 

    //Have the factory create us a connection object 
    DbConnection conn = factory.CreateConnection(); 

    //Open the connection with the connection string from web.config 
    conn.ConnectionString = cs.ConnectionString; 
    conn.Open(); 

    //Give the ready connection to the user 
    return conn; 
} 

Note: Any code is released into the public domain. No attribution required.

संबंधित मुद्दे