使用SqlCeConnection.Close()正在挂起应用程序
本文关键字:挂起 应用程序 SqlCeConnection Close 使用 | 更新日期: 2023-09-27 18:21:54
我正在开发一个WinFrom应用程序。我正在使用SDF数据库来存储数据。下面用于从数据库将数据加载到数据网格的代码片段正在挂起应用程序。当我点击数据网格时,"关闭SqlResultSet时调用方法Updatable的尝试无效"正在引发异常。
public partial class Form1 : Form
{
private SqlCeConnection _conn;
public Form1()
{
InitializeComponent();
_conn = new SqlCeConnection(@"Data Source = |DataDirectory|'Database1.sdf");
this.dataGridView1.AutoGenerateColumns = true;
}
private void Form1_Load(object sender, EventArgs e)
{
SqlCeCommand sqlcmd = new SqlCeCommand();
sqlcmd.Connection = _conn;
sqlcmd.CommandText = "SELECT ID, UserName FROM Table1";
_conn.Open();
SqlCeResultSet rs = sqlcmd.ExecuteResultSet(ResultSetOptions.Scrollable);
this.bindingSource1.DataSource = rs;
_conn.Close();
}
}
有人能查一下吗?
试试这个:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.AutoGenerateColumns = true;
}
private void Form1_Load(object sender, EventArgs e)
{
using(SqlCeConnection _conn = new SqlCeConnection(@"Data Source = |DataDirectory|'Database1.sdf")){
_conn.Open();
SqlCeCommand sqlcmd = _conn.CreateCommand();
sqlcmd.CommandText = "SELECT ID, UserName FROM Table1";
SqlCeDataAdapter a = new SqlCeDataAdapter();
a.SelectCommand = sqlcmd;
DataTable t = new DataTable();
a.Fill(t);
this.bindingSource1.DataSource = t;
}
}
}
试试这个
public partial class Form1 : Form
{
private SqlCeConnection _conn;
public Form1()
{
InitializeComponent();
this.dataGridView1.AutoGenerateColumns = true;
}
private void Form1_Load(object sender, EventArgs e)
{
SqlCeDataReader rdr = null;
try
{
using(SqlCeConnection conn = new SqlCeConnection(@"Data Source = |DataDirectory|'Database1.sdf"))
{
conn.Open();
SqlCeCommand sqlcmd = new SqlCeCommand("SELECT ID, UserName FROM Table1", conn);
sqlcmd.Connection.Open();
rdr = sqlcmd.ExecuteReader();
//Custom Object is object with same structure as your data table
List<CustomObject> dataSource = new List<CustomObject>();
while (rdr.Read())
{
var customObject = new CustomObject();
customObject.Id = rdr.GetInt32(0);
//so on
dataSource.Add(customObject);
}
this.bindingSource1.DataSource = dataSource;
rdr.Close();
}
}
catch(Exception ex)
{
//Handle Exception
}
}
}
你也可以查看这篇文章。您将需要为直接绑定数据集保持连接打开。与其这样做,将其映射到某个本地对象并绑定该对象。
希望这对你有用。
使用SQLCE,您可以在程序启动时打开连接,并在程序关闭时处理它。该连接可以毫无问题地为多个请求提供服务器。SqlCeResultset的设想方式是直接查看数据库表,而不将任何数据加载到内存中。因此,当你关闭你的联系时,无处可看,这就是它抱怨的原因。这就是brano解决方案工作的原因,因为数据是使用数据表加载到内存中的。在您的情况下,只需在表单加载时打开一个连接,然后在表单关闭时关闭它。你不必进行大量的内存加载,我也不推荐这里提供的两种解决方案(为什么要在直接访问更快的内存中加载某个东西?)。易于理解的
public partial class Form1 : Form
{
private SqlCeConnection _conn;
public Form1()
{
InitializeComponent();
_conn = new SqlCeConnection(@"Data Source = |DataDirectory|'Database1.sdf");
this.dataGridView1.AutoGenerateColumns = true;
}
private void Form1_Load(object sender, EventArgs e)
{
SqlCeCommand sqlcmd = new SqlCeCommand();
sqlcmd.Connection = _conn;
sqlcmd.CommandText = "SELECT ID, UserName FROM Table1";
_conn.Open();
SqlCeResultSet rs = sqlcmd.ExecuteResultSet(ResultSetOptions.Scrollable);
this.bindingSource1.DataSource = rs;
//dont close the connection
// _conn.Close();
}
protected override Close()
{
if (_conn != null)
_conn.close();
base.Close()
}
}