locked
How can i insert data in mobile aplication using sql database RRS feed

  • Question

  • Hi,I'm beginner and making WMA 6.1.

    I creat local database named FirstDB.sdf(Data Source=C:\Users\Oliver.Danilov\Desktop\dokumenti\panel\panel\FirstDB.sdf) table PhoneBook with this colums(Name, Number, Operator) using  Microsoft SQL Server Compact 3.5

    How can i insert data in this database from my Form1.cs, for example i do this:

     

    I thing that problem is in the first row, i didn't what to write to be corect, try many ways but..

     

    string s = "Provider=sqloledb;Server=???;Initial " + "Catalog=FirstDB.sdf;User Id=;Password=;";

                    SqlConnection con = new SqlConnection(s);

                    //con.ConnectionString = s;

                    con.Open();

                    SqlCommand com = new SqlCommand();

                    com.Connection = con;

                    com.CommandText = "INSERT INTO PnoneBook (Name, Number, Operator) " + "Values ('Oliver', '071588775', 'Vip')";

                    com.ExecuteNonQuery();

                    con.Close();

     

     

    please help, any suggestion is welcome

    Wednesday, May 4, 2011 11:50 AM

Answers

  • If connection did not open then you would get an exception. Examine exception to figure out what went wrong and what needs to be fixed. Common issues include incorrect paths to the database, using desktop paths with driver letters (which you seem to not do), not deploying the database to the device or deploying it to the wrong location.

    If you did not get an exception then it worked. Common mistakes are: looking for changes in the original unchanged copy of the database on desktop and overwriting changed copy with original unchanged one each time application is deployed to the device.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Jesse Jiang Tuesday, May 10, 2011 8:11 AM
    • Marked as answer by Jesse Jiang Tuesday, May 17, 2011 7:19 AM
    Thursday, May 5, 2011 12:48 AM

All replies

  • It seems you've confused different technologies such as SQL Server CE (which is what your database file is for) and SQL Server (which SQL Client connects to).

    You need to use either SqlServerCe instead of SQL Client and copy database to the device (as SQL Server CE is a local in process database) or setup SQL Server on your desktop and connect to it remotely by specifying server name and port number (server must be configured to accept connections on fixed TCP port).

    Also under no circumstances you can use driver letters on devices. If you try your code will fail.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, May 4, 2011 4:51 PM
  • ok thanks, i fix that and write this code

    try

                {

                    SqlCeConnection con = new SqlCeConnection("Data Source =\\Program Files\\panel\\FirstDB.sdf");

                    con.Open();

                    SqlCeCommand com = new SqlCeCommand();

                    com.Connection = con;

                    com.CommandText = "INSERT INTO PnoneBook Values ('Oliver', '071588775', 'Vip')";

                    com.ExecuteNonQuery();

                    con.Close();

                }

                catch (Exception ex)

                {

                    ex.ToString();

                }

     

    when i check database the table is still empty, maybe conection didn't open? without try and catch debuging faild when a click buton on my application and show me eror on the row   con.Open();
    Wednesday, May 4, 2011 10:12 PM
  • If connection did not open then you would get an exception. Examine exception to figure out what went wrong and what needs to be fixed. Common issues include incorrect paths to the database, using desktop paths with driver letters (which you seem to not do), not deploying the database to the device or deploying it to the wrong location.

    If you did not get an exception then it worked. Common mistakes are: looking for changes in the original unchanged copy of the database on desktop and overwriting changed copy with original unchanged one each time application is deployed to the device.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Jesse Jiang Tuesday, May 10, 2011 8:11 AM
    • Marked as answer by Jesse Jiang Tuesday, May 17, 2011 7:19 AM
    Thursday, May 5, 2011 12:48 AM
  • Hi Oliver,

     

    I'm very interested in hearing more about your issue, did you solve your problem?

    If so please share the solution with us and mark the right answers.

    If not please let me know and I will tray to help you find further solutions.

     

    Have a nice day!

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 10, 2011 8:11 AM