SqlCeCommand keeps giving me an exception

Cornelis

I am connecting to a compact SQL database server through a WCF service and keep getting the following except on the Command.ExecuteNonQuery(). I have tried fixing this but just don't know what's wrong.

The exception:

An exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll but was not handled in user code

The code:

//The connectionString can be found in the properties table of the database
string connString = "Data Source=C:\\Users\\User\\documents\\visual studio 2012\\Projects\\ADO_LINQ\\ADO_LINQ\\App_Data\\MyDatabase.sdf;Persist Security Info = False";
SqlCeConnection myConnection = new SqlCeConnection(connString);
myConnection.Open();

// Create the query
string myQuery = "INSERT INTO Player " +
    " VALUES (" + registrationID + "," + 
        firstName + ", " + 
        lastName + ", " + 
        phoneNumber + ", " + 
        address + ", " + 
        dateOfBirth + ");";

//Initialuze the command
SqlCeCommand myCommand = new SqlCeCommand(myQuery, myConnection);

//Run the command
myCommand.ExecuteNonQuery();

//Close the connection
myConnection.Close();
M.Ali

You are missing Single quotes around your string data types, Assuming only registrationID is Integer data type and all other columns are String data type , your query should look something like ......

// Create the query
String myQuery = "INSERT INTO Player " +
               " VALUES (" + registrationID + ", '"+ firstName +"' , '"+lastName+"' , '"+phoneNumber+ "', '"+ address +"', '"+dateOfBirth+"' );";

A better and safer option would be to use Parametrised query. Something like this.....

String connString = @"Data Source=C:\Users\User\documents\visual studio 2012\Projects\ADO_LINQ\ADO_LINQ\App_Data\MyDatabase.sdf;Persist Security Info = False";

using(SqlCeConnection myConnection = new SqlCeConnection(connString))
{
  // Create the query
 String myQuery = "INSERT INTO Player " +
               " VALUES (@registrationID , @firstName , @lastName , @phoneNumber, @address , @dateOfBirth );";

 //Initialuze the command
  SqlCeCommand myCommand = new SqlCeCommand(myQuery, myConnection);

 // Add parameters 

   myCommand.Parameters.AddWithValue("@registrationID" ,registrationID); 
   myCommand.Parameters.AddWithValue("@firstName" , firstName);
   myCommand.Parameters.AddWithValue("@lastName" , lastName);
   myCommand.Parameters.AddWithValue("@phoneNumber" , phoneNumber);
   myCommand.Parameters.AddWithValue("@address" , address);
   myCommand.Parameters.AddWithValue("@dateOfBirth" , dateOfBirth);

 //Open Connection 

   myConnection.Open();

 //Run the command
  myCommand.ExecuteNonQuery();
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Crystal Reports keeps giving me: Could not load file or assembly

분류에서Dev

PHP function giving me exception when trying to display on HTML

분류에서Dev

why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

분류에서Dev

Nginx Autoindex giving me a 403

분류에서Dev

Ubuntu 13.10 apache keeps giving this error while accessing website

분류에서Dev

PyTorch is giving me a different value for a scalar

분류에서Dev

Checking for repeated characters not giving me the desired output

분류에서Dev

Why is my Makefile giving me problems?

분류에서Dev

Don't understand why my code giving me an IndexError

분류에서Dev

repeater not giving me new value during editing in asp.net

분류에서Dev

Heapify is giving me an incorrect output for a min-heap

분류에서Dev

Woocommerce add_to_cart giving me fatal error

분류에서Dev

Why these expressions are giving me output instead of compile error?

분류에서Dev

Java Closing an IOStream in finally giving error: Unhandled Exception: java.io.IOException

분류에서Dev

Flash plugin up to date but Firefox keeps telling me that I have the old version

분류에서Dev

Displaying image in tooltip is giving me a flickering effect when hovered over the content

분류에서Dev

Why is it giving me an error when i'm calling it from inside the 'student class?

분류에서Dev

Why isn't IPython giving me a full traceback for a module I've written?

분류에서Dev

SqlCeCommand가 계속 예외를 제공합니다.

분류에서Dev

SqlCeCommand 사례를 민감하게 만드는 방법은 무엇입니까?

분류에서Dev

video on canvas keeps resizing

분류에서Dev

Deluge keeps crashing

분류에서Dev

Memory keeps growing

분류에서Dev

SSL keeps waiting for response

분류에서Dev

Javascript Prompt keeps appearing

분류에서Dev

function keeps asking for input

분류에서Dev

Vim keeps unindenting

분류에서Dev

Giving permissions to apache user

분류에서Dev

Handler in android giving error

Related 관련 기사

  1. 1

    Crystal Reports keeps giving me: Could not load file or assembly

  2. 2

    PHP function giving me exception when trying to display on HTML

  3. 3

    why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

  4. 4

    Nginx Autoindex giving me a 403

  5. 5

    Ubuntu 13.10 apache keeps giving this error while accessing website

  6. 6

    PyTorch is giving me a different value for a scalar

  7. 7

    Checking for repeated characters not giving me the desired output

  8. 8

    Why is my Makefile giving me problems?

  9. 9

    Don't understand why my code giving me an IndexError

  10. 10

    repeater not giving me new value during editing in asp.net

  11. 11

    Heapify is giving me an incorrect output for a min-heap

  12. 12

    Woocommerce add_to_cart giving me fatal error

  13. 13

    Why these expressions are giving me output instead of compile error?

  14. 14

    Java Closing an IOStream in finally giving error: Unhandled Exception: java.io.IOException

  15. 15

    Flash plugin up to date but Firefox keeps telling me that I have the old version

  16. 16

    Displaying image in tooltip is giving me a flickering effect when hovered over the content

  17. 17

    Why is it giving me an error when i'm calling it from inside the 'student class?

  18. 18

    Why isn't IPython giving me a full traceback for a module I've written?

  19. 19

    SqlCeCommand가 계속 예외를 제공합니다.

  20. 20

    SqlCeCommand 사례를 민감하게 만드는 방법은 무엇입니까?

  21. 21

    video on canvas keeps resizing

  22. 22

    Deluge keeps crashing

  23. 23

    Memory keeps growing

  24. 24

    SSL keeps waiting for response

  25. 25

    Javascript Prompt keeps appearing

  26. 26

    function keeps asking for input

  27. 27

    Vim keeps unindenting

  28. 28

    Giving permissions to apache user

  29. 29

    Handler in android giving error

뜨겁다태그

보관