如何编写Windows Ce应用程序数据源 [英] how to write Windows Ce application data source

查看:71
本文介绍了如何编写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();

推荐答案

嗨ari,



也许看看 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v = vs.110)的.aspx [ ^ ]



所以我认为在你的情况下这将导致:

(一个反斜杠应该是你的设备上的根路径 - 虽然我不相信你可以把它放在那里坚持 - 但那是另一个故事......)



@数据源= \ MyDatabase#1.sdf;
Hi ari,

Maybe have a look at https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx[^]

So I think in your case this will result in:
(One backslash should be the root-path on your ce device - although I don't believe you can place it there for persistence - but that's another Story...)

@"Data Source=\MyDatabase#1.sdf";


非常感谢johannesnestler为你的答案,,,,

i得到了解决方案,我把它放在这里也许有一天对别人有用....



thank you so much johannesnestler for your answers ,,,,
i got solution and i gone put it here it maybe useful for someone else one day ....

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应用程序数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆