本文介绍了如何编写Windows Ce应用程序数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

任何人都可以告诉我如何使用DataDirectory作为数据源

因为我在运行程序时遇到此错误{路径是无效。检查数据库的目录,[Path = | DataDirectory | //localdata.sdf}所以你们可以帮助我

hello everyone ..
can anyone tell me how to use "DataDirectory" for data source
because i get this error when i run the program {The path is not valid.check the directory for the database ,[Path=|DataDirectory|//localdata.sdf} so guys can you plz help me

using System.Data.SqlServerCe;

namespace Smartdevice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            loadcom();
        }
        SqlCeConnection  cn = new SqlCeConnection("Data Source=|DataDirectory|\\MyDatabase#1.sdf");
        private void button1_Click(object sender, EventArgs e)
        {
           SqlCeCommand cmd = new SqlCeCommand("insert into Tt (name)  values(@name)",cn);
            cn.Open();

            cmd.Parameters.AddWithValue("@name",textBox1.Text);
            cmd.ExecuteNonQuery();

            cn.Close();
        }
        private  void loadcom()
        {
            cn.Open();
            SqlCeCommand cmd = new SqlCeCommand("select name from Tt",cn);
            SqlCeDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr["name"].ToString());
            }


            cn.Close();

推荐答案


string stconnect = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string stdataconn= Path.Combine(stconnect, "MyDatabase#1.sdf");
string stsqlc = string.Format("DataSource={0}", stdataconn);
sqlceconnect cn=new sqlceconnect(stqlc);



thanks


thanks


这篇关于如何编写Windows Ce应用程序数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:14