我有 SQL Server CE 3.5 数据库。我运行以下代码:
private void Load(string sql)
{
if (connection.State != ConnectionState.Open)
connection.Open();
SqlCeDataAdapter sqlCeDataAdapter = new SqlCeDataAdapter(sql, connection);
Stopwatch watch = new Stopwatch();
DataSet dataSet = new DataSet();
try
{
Cursor.Current = Cursors.WaitCursor;
watch.Start();
sqlCeDataAdapter.Fill(dataSet, "items");
sqlCeDataAdapter.Dispose();
var myBind = new BindingSource(dataSet, "items");
grid.DataSource = myBind;
}
finally
{
watch.Stop();
Cursor.Current = Cursors.Default;
MessageBox.Show(watch.ElapsedMilliseconds.ToString());
}
}
Load ("select a1, a2 from table");
SQL Server CE 是不是很长,还是应该加载得更快?
我能做些什么来加快这个选择?
当我将连接与其他表一起使用时,时间会急剧增加......
编辑:
我已经更改了以下新版本的代码,但仍然是 7.5 秒。...
private void Load2 (string sql)
{
if (connection.State != System.Data.ConnectionState.Open)
{
connection.Open();
}
using (SqlCeCommand command = new SqlCeCommand(sql, connection))
{
Stopwatch watch = new Stopwatch();
Cursor.Current = Cursors.WaitCursor;
watch.Start();
using (SqlCeDataReader reader = command.ExecuteReader())
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add("items");
dataSet.Tables["items"].Columns.Add("s1");
dataSet.Tables["items"].Columns.Add("s2");
while (reader.Read())
{
string s1 = reader.GetString(0);
string s2 = reader.GetString(1);
dataSet.Tables["items"].Rows.Add(s1, s2);
}
watch.Stop();
Cursor.Current = Cursors.Default;
MessageBox.Show(watch.ElapsedMilliseconds.ToString());
BindingSource binding = new BindingSource(dataSet, "items");
grid.DataSource = binding;
}
}
}