Click here to Skip to main content
15,900,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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();
Posted
Updated 5-Mar-15 11:00am
v2

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";
 
Share this answer
 
v2
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 ....

C#
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900