如何使用C#在Sql Server Compact数据库中搜索?

时间:2015-11-02 12:35:07

标签: c# sql-server-ce

我创建了在SqlServerCompact数据库中继的应用程序,我希望通过DataGridView过滤数据,如何实现此任务?任何帮助将不胜感激。

我的代码

using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;

namespace DemoLocalDataBase
{
    public partial class frmView : Form
    {
        public static SqlCeDataAdapter sda = null;

        #region Constructor
        public frmView()
        {
            InitializeComponent();
            CurdOperation();
            this.BindGrid();
        }
        #endregion

        #region Create connection to the local database
        /// <summary>
        /// Here i have to use SqlCeConnection Class.
        /// </summary>
        private static void CurdOperation()
        {
            SqlCeConnection con = new SqlCeConnection("Data Source="
                + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "MyDB.sdf"));
            sda = new SqlCeDataAdapter();
            SqlCeCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from MyDemoTable";
            sda.SelectCommand = cmd;

            SqlCeCommandBuilder cb = new SqlCeCommandBuilder(sda);
            sda.InsertCommand = cb.GetInsertCommand();
            sda.UpdateCommand = cb.GetUpdateCommand();
            sda.DeleteCommand = cb.GetDeleteCommand();
        }
        #endregion
        #region PageSepcific Method

        /// <summary>
        /// this is common method created for Add,Update,Delete case.
        /// In this method normaly fill dataset with olddata.
        /// </summary>
        /// <param name="oldData"></param>
        /// <param name="dr"></param>
        private static void FillDataInDataset(out DataSet oldData, out DataRow dr)
        {
            oldData = new DataSet();
            dr = null;
            sda.Fill(oldData);
        }
        /// <summary>
        /// In this method get all data from table and 
        /// bind grid with data.
        /// </summary>
        private void BindGrid()
        {
            DataSet _ds = new DataSet();
            sda.Fill(_ds);
            if (_ds.Tables.Count > 0)
            {
                dgvOldData2.DataSource = _ds.Tables[0];
            }
        #endregion
        }

请给我正确答案,因为我是编程哑剧的初学者。

0 个答案:

没有答案