Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to learn how to populate sdf database from sql server compact into datagridview through codes. In sql server (mdf database) should look like this

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Dim cn As SqlConnection
    Private Sub MyConnection()
        Try
            Dim cnSQL As String = "Server=MyServer;Database=Database1;Trusted_Connection=true"
            Me.cn = New SqlConnection(cnSQL)
            cn.Open()
            cn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MyConnection()

        Dim dtSet As DataSet = New DataSet
        Dim dtAdapter As SqlDataAdapter = New SqlDataAdapter("Select * from TableDatabase1", cn)
        dtAdapter.Fill(dtSet)
        Me.DataGridView1.DataSource = dtSet.Tables(0)
    End Sub
End Class


However, that code doesn't work for SDF database. I'm using VB 2010 and Default VS 2010 SQL Server. So, what code should I write for SDF dabase? I want in code only. Thanks in advance.
Posted

With SDF you probably mean a SQL Server CE database file. If that's correct, then I guess you have a problem with your connection string.

SQL Server CE doesn't have a database engine like for example Express edition has. Because of this, CE databases are used by connecting directly to the file.

Your connection string should look something like:
VB
Dim cnSQL As String = "Data Source=C:\SomeFolder\DataFileName.sdf;Persist Security Info=False"
 
Share this answer
 
Comments
derodevil 9-Sep-11 15:04pm    
Yes, that's what I mean, SQL Server Compact Edition. What code should I write to display the data in DataGridView? Can you write some sample code, please? Or you have a link where I can find a tutorial about it?
Wendelius 9-Sep-11 15:27pm    
Here's one example: http://www.daniweb.com/software-development/vbnet/code/217032[^]

Just remember that in that example the connection string isn't correct since you're using CE
derodevil 9-Sep-11 15:43pm    
Your answer is very helpful that sdf database is SSCE. My problem has been solved. I just need to change Imports System.Data.SqlServerCe, SqlCeConnection and SqlCeDataAdapter. Thank you for helping me. I have one more question, is this database more powerful than Access Database? Can I develop it and deploy it to stand alone computer with huge number of data since I don't need client server network database?
Wendelius 9-Sep-11 16:59pm    
Glad it was helpful :) Yes, in my opinion CE is much more powerful than Access. The big issue is that it's based on larger editions of SQL Server, so if you want to migrate to these somehwere in tuhe future, it'll be a whole lot easier from CE than from Access.
i see that you're programming using sdf files.
do you know about .sdf files? its use for databases. i am going to make a sdf viewer but i dont know where to start. can you help me?
i need to make a sdf viewer application using vb.net (window-based)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900