【OK210试用体验】+使用问题和SQL SERVER数据库使用 - 在线问答 - 电子威廉希尔官方网站 论坛 - 最好最受欢迎电子论坛!

【OK210试用体验】+使用问题和SQL SERVER数据库使用

zqwy3191558 ( 楼主 ) 2015-7-19 00:21:54  只看该作者 倒序浏览
在使用的过程中,我使用了飞凌公司的7寸电阻屏,使用过程中发现在进入待机界面时,有出现条状花屏现象而不是黑屏。希望飞凌公司能尽快处理。
我做的数据采集传输仪,会采集大量的数据进行存储,计算。
如果要对大数据进行统一管理,使用数据库是最好的选择,所以我采用了sql server数据库进行统一管理数据,我采用了本地数据库,现在将我的操作代码分享给大家
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;
using System.Diagnostics;
namespace scy.SqlServer
{
    class CommonDB
    {
        SqlCeConnection Connection = new SqlCeConnection();
        public SqlCeConnection DB_Open()
        {
            string constr = "Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\MyscyDB.sdf;PassWord=sdyyhbscy;Persist Security Info=False";
            //    string constr = "Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\MyscyDB.sdf;Persist Security Info=False";            
            SqlCeConnection Con = new SqlCeConnection(constr);
            try
            {
                if (Connection.State == ConnectionState.Closed)
                {
                    Con.Open();
                    Connection = Con;
                    Debug.WriteLine("数据库成功链接");
                }
                else
                {
                    Debug.WriteLine("数据库已经打开");
                }
            }
            catch
            {
                MySystem.WriteLog writelog = new MySystem.WriteLog();
                writelog.MyWriteDRALog(@"数据库打开失败", "MyDB");
            }
            return Con;
        }
        //由Select语句和表名,获得对应的DataSet 数据集
        public DataSet GetDataSet(string Sql,  int startRecord,int maxRecords,string tablename)
        {
            SqlCeCommand Cmd = new SqlCeCommand(Sql, Connection);
            SqlCeDataAdapter Sda = new SqlCeDataAdapter(Cmd);
            DataSet Ds = new DataSet();
            try
            {
                Sda.Fill(Ds, startRecord, maxRecords, tablename);
                return Ds;
            }
            catch
            {
                MySystem.WriteLog writelog = new MySystem.WriteLog();
                writelog.MyWriteDRALog(@"数据库数据读取错误", "MyDB");
                if (Connection.State != ConnectionState.Closed)
                {
                    Connection.Close();
                }
            }
            return null;

        }
        //由Select语句和表名,获得对应的DataSet 数据集
        public DataSet GetDataSet(string Sql)
        {
            SqlCeCommand Cmd = new SqlCeCommand(Sql, Connection);
            SqlCeDataAdapter Sda = new SqlCeDataAdapter(Cmd);
            DataSet Ds = new DataSet();
            try
            {
                Sda.Fill(Ds);
                return Ds;
            }
            catch
            {
                MySystem.WriteLog writelog = new MySystem.WriteLog();
                writelog.MyWriteDRALog(@"数据库数据读取错误", "MyDB");
                if (Connection.State != ConnectionState.Closed)
                {
                    Connection.Close();
                }
            }
            return null;
        }
        public int GetCountData(string Sql)
        {
            int count = 0;
            SqlCeCommand Cmd = new SqlCeCommand(Sql, Connection);
            try
            {
                count = Cmd.ExecuteNonQuery();
            }
            catch
            {
                MySystem.WriteLog writelog = new MySystem.WriteLog();
                writelog.MyWriteDRALog(@"数据库数据查询数量错误", "MyDB");
                if (Connection.State != ConnectionState.Closed)
                {
                    Connection.Close();
                }
            }
            return count;
        }
        public int update(string Sql, SqlCeParameter[] paras)
        {
            int count = 0;
             SqlCeCommand Cmd = new SqlCeCommand(Sql, Connection);
             foreach (SqlCeParameter p in paras)
             {
                 Cmd.Parameters.Add(p);
             }
             try
             {
                 count = Cmd.ExecuteNonQuery();
             }
             catch
             {
                 MySystem.WriteLog writelog = new MySystem.WriteLog();
                 writelog.MyWriteDRALog(@"数据库数据更新错误", "MyDB");
                 if (Connection.State != ConnectionState.Closed)
                 {
                     Connection.Close();
                 }
             }
             return count;
        }
        public int SaveData(string Sql, SqlCeParameter[] paras,string name)
        {
            int count = 0;
            SqlCeCommand Cmd = new SqlCeCommand(Sql, Connection);
            foreach (SqlCeParameter p in paras)
            {
                Cmd.Parameters.Add(p);
            }
            try
            {
                count = Cmd.ExecuteNonQuery();
            }
            catch
            {
                MySystem.WriteLog writelog = new MySystem.WriteLog();
                writelog.MyWriteDRALog(@"数据库"+ name +"  "+ paras[0].Value + "存入错误", "MyDB");
            }
            return count;
        }
        public int DB_DataDelete(string str)
        {
            int counts = 0;
            MySystem.WriteLog writelog = new MySystem.WriteLog();
            try
            {
                DB_Open();
                string sql = "delete " + str;
                SqlCeCommand cmd = new SqlCeCommand(sql, Connection);
                counts = cmd.ExecuteNonQuery();
                Connection.Close();

                writelog.MyWriteDRALog(@"数据库"+str+"删除"+counts+"条数据", "MyDB");
            }
            catch
            {
                if (Connection.State != ConnectionState.Closed)
                {
                    Connection.Close();
                    writelog.MyWriteDRALog(@"数据库" + str + "删除历史数据错误", "MyDB");
                }
            }
            return counts;
        }
        public int DB_DataDelete(string type, string time)
        {
            int counts = 0;
            MySystem.WriteLog writelog = new MySystem.WriteLog();
            try
            {
                DB_Open();
                string sql = "delete from " + type + " where DataTime < '" + time + "'";
                SqlCeCommand cmd = new SqlCeCommand(sql, Connection);
                counts = cmd.ExecuteNonQuery();
                Connection.Close();

                writelog.MyWriteDRALog(@"数据库" + type + "删除" + counts + "条数据", "MyDB");
            }
            catch
            {
                if (Connection.State != ConnectionState.Closed)
                {
                    Connection.Close();
                    writelog.MyWriteDRALog(@"数据库" + type + "删除历史数据错误", "MyDB");
                }
            }
            return counts;
        }
        public void DB_Close()
        {
            if (Connection != null)
                Connection.Close();
            Debug.WriteLine("数据库成功关闭");
        }
    }
}


奖励1积分

1个回复

燕小飞 发表于 2015-7-20 21:51:40
感谢分享,您提到的问题,我明天去测试一下
回复

举报 使用道具

您需要登录后才可以回帖 登录 | 注册

本版积分规则


关闭

站长推荐上一条 /6 下一条

小黑屋|手机版|Archiver|电子发烧友 ( 湘ICP备2023018690号 )

GMT+8, 2024-9-20 23:44 , Processed in 0.326247 second(s), Total 37, Slave 29 queries .

Powered by 电子发烧友网

© 2015 bbs.elecfans.com

微信扫描
快速回复 返回顶部 返回列表