私はインベントリベースのアプリケーションを構築しています。これは、インストールせずに任意のPCから直接実行できるため、SQL CEを使用しています。私は、新しいデータベースを作成していないかのように:SQL CE Windowsのデータベースの値を更新しません。
try
{
string conString = "Data Source='ITM.sdf';LCID=1033;Password=ABCDEF; Encrypt = TRUE;";
SqlCeEngine engine = new SqlCeEngine(conString);
engine.CreateDatabase();
return true;
}
catch (Exception e)
{
//MessageBox.Show("Database Already Present");
}
データベースが適切に作成され、私は同様にテーブルを作成するために、データベースにアクセスすることができます..私は直面しています問題がある - 私は、Windowsフォームでレコードを挿入し、更新していますボタン上のコードをクリックしてください:
using (SqlCeConnection connection = new SqlCeConnection(DatabaseConnection.connectionstring))
{
connection.Open();
String Name = NameTxt.Text.ToString();
String Phone = PhoneTxt.Text.ToString();
double balance = Double.Parse(BalanceTxt.Text.ToString());
String City = CityTxt.Text.ToString();
string sqlquery = "INSERT INTO Customers (Name,Phone,Balance,City)" + "Values(@name,@phone, @bal, @city)";
SqlCeCommand cmd = new SqlCeCommand(sqlquery, connection);
cmd.Parameters.AddWithValue("@name", Name);
cmd.Parameters.AddWithValue("@phone", Phone);
cmd.Parameters.AddWithValue("@bal", balance);
cmd.Parameters.AddWithValue("@city", City);
int x = cmd.ExecuteNonQuery();
if (x > 0)
{
MessageBox.Show("Data Inserted");
}
else
{
MessageBox.Show("Error Occured. Cannot Insert the data");
}
connection.Close();
}
と私はレコードを挿入し、更新するためのコードを実行するたびにUpdationコードは
using (SqlCeConnection connection = new SqlCeConnection(DatabaseConnection.connectionstring))
{
connection.Open();
int idtoedit = select_id_edit;
String Name = NameEditTxt.Text.ToString();
String Phone = metroTextBox1.Text.ToString();
String City = CityEditTxt.Text.ToString();
string sqlquery = "Update Customers Set Name = @name, Phone = @phone,City = @city where Id = @id";
SqlCeCommand cmd = new SqlCeCommand(sqlquery, connection);
cmd.Parameters.AddWithValue("@name", Name);
cmd.Parameters.AddWithValue("@phone", Phone);
cmd.Parameters.AddWithValue("@id", idtoedit);
cmd.Parameters.AddWithValue("@city", City);
int x = cmd.ExecuteNonQuery();
if (x > 0)
{
MessageBox.Show("Data Updated");
}
else
{
MessageBox.Show("Error Occured. Cannot Insert the data");
}
loadIntoGrid();
connection.Close();
}
です - レコードがデータグリッドに反映されていますデータベーステーブルからアダプタで埋められます。しかし、いったんアプリケーションを再起動すると、値はデータベースに表示されません。百万にもなると、値がデータベースに反映されます。私はこの問題の背後にある理由を理解できません。
私はこれらの記事にrefferedています
C# - ExecuteNonQuery() isn't working with SQL Server CE
Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile
しかし、私はプログラム的にデータベースを作成していますので、 - それは、ビン/ debugディレクトリに直接作成なっていると私は、溶液中でそれを見ることができませんコピーオプションを変更するためのVisual Studioのエクスプローラ
'if(x> 0)'と同時に**を実行している**更新と挿入のクエリの両方を明確にするだけですか? –
いいえ..別々に動作しています –