简体   繁体   中英

SQL Server Compact Edition Data Access with the SqlCeResultSet in Visual Studio 2013

I would like use the SqlCeResultSet like this tutorial in Visual Studio 2013.

This is the code of my class Form1:

private SqlCeConnection _conn;
        public Form1()
        {
            InitializeComponent();
            _conn = new SqlCeConnection(@"Data Source = |DataDirectory|\Northwind.sdf");
            this.dataGridView1.AutoGenerateColumns = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCeCommand cmd = new SqlCeCommand("SELECT [Employee ID], [Last Name], [First Name], Photo FROM Employees",_conn);
            SqlCeResultSet resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
            this.bindingSource1.DataSource = resultSet;
        }

Now I try to follow exactly the tutorial with my Database and when execute the line:

 SqlCeResultSet resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable) 

throw this exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.SqlServerCe.dll Additional information: ExecuteResultSet: Connection property has not been initialized.

For more details this is my Solution Explorer:

在此处输入图片说明

And I have installed:

在此处输入图片说明

Any idea or suggestion would be appreciated.

Thanks in advance.

Updating

Maybe the problem is that SQL Server Compact is discontinued from Visual Studio 2013 see this question . In this case exist any alternative for my code?

You missed step 24 in the tutorial - you need to open the connection:

 SqlCeCommand cmd = new SqlCeCommand("SELECT [Employee ID], [Last Name], [First Name], Photo FROM Employees",_conn);
 _conn.Open();
 SqlCeResultSet resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
 this.bindingSource1.DataSource = resultSet;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM