2012-02-17 16 views
0

ローカルSQL Server CEデータベース(mainDB.sdf)にはUsersという1つのテーブルしかありません。SqlCeDataAdapterを使用してC#でローカルデータベースに新しい行を挿入する

は7列からなる:

  • Name (ntext)
  • Surname (ntext)
  • Nickname (nchar, unique, primary key)
  • Gender (ntext)
  • Status (ntext)
  • City (ntext)
  • 私はオブジェクトを使用Photo (image)

private SqlCeConnection connection; 
private SqlCeDataAdapter adapter; 
private SqlCeCommandBuilder builder; 
private DataSet data; 

はまず、私は、データベースへの接続:

connection = new SqlCeConnection("Data Source = mainDB.sdf"); 
connection.Open(); 
SqlCeCommand cmd = connection.CreateCommand(); 
cmd.CommandText = "SELECT * FROM Users"; 
adapter = new SqlCeDataAdapter(cmd); 
builder = new SqlCeCommandBuilder(); 
builder.DataAdapter = adapter; 
data = new DataSet(); 
adapter.Fill(data); 

私は新しい行を挿入しよう:

MemoryStream ms = new MemoryStream(); 
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 
byte[] im = ms.ToArray(); 
DataRow newRow = data.Tables[0].NewRow(); 
newRow["Name"] = textBox1.Text; 
newRow["Surname"] = textBox2.Text; 
newRow["Nickname"] = textBox3.Text; 
newRow["Gender"] = listBox1.Text.Length > 0 ? listBox1.Text : null; 
newRow["Status"] = radioButton1.Checked ? "Student" : radioButton2.Checked ? "Professional" : null; 
newRow["City"] = comboBox1.Text.Length > 0 ? comboBox1.Text : null; 
newRow["Photo"] = im; 
data.Tables[0].Rows.Add(newRow); 
adapter.Update(data); 
connection.Close(); 

後これ、私はテーブルのユーザーに行くと、新しいデータはありません。 SqlCeCommandBuilderは必要なコマンドを生成する必要があります。私はここで何が欠けていますか?

重要な編集
すべて正常です。 c#はデータベースへの2つのリンクを作成するようです。最初のものはmainDB.sdfと呼ばれるプロジェクトフォルダにあり、空です。しかし、bin\Debugフォルダにもう1つあります。絶対初心者として私はそれを知らなかった。そのDBの前のすべての行をmainDB.sdf1とし、New Queryを選択してSELECT * FROM USERSと入力することができました。最初のmainDB.sdfと同じコマンドには何も表示されません。これが問題でした。

+0

あなたの列の使用に適切なデータ型は。 nTextは非常に大きなテキストのためのものであり、名前のためではありません(これはほとんど10文字より長くなりません)。 –

答えて

1

使用SqlCeCommand

SqlCeConnection connection = new SqlCeConnection("Data Source = mainDB.sdf"); 
connection.Open(); 

using (SqlCeCommand com = new SqlCeCommand("INSERT INTO Users (Name, Surname, Nickname, Gender, Status, City, Photo) Values(@name,@surname,@nickname,@gender,@status,@city,@photo)", connection)) 
{ 
    com.Parameters.AddWithValue("@name", textBox1.Text); 
    com.Parameters.AddWithValue("@surname", textBox2.Text); 
    com.Parameters.AddWithValue("@nickname", textBox3.Text); 
    com.Parameters.AddWithValue("@gender", listBox1.Text.Length > 0 ? listBox1.Text : null); 
    com.Parameters.AddWithValue("@status", radioButton1.Checked ? "Student" : radioButton2.Checked ? "Professional" : null); 
    com.Parameters.AddWithValue("@city", comboBox1.Text.Length > 0 ? comboBox1.Text : null); 
    com.Parameters.AddWithValue("@photo", im); 

    com.ExecuteNonQuery(); 
} 

connection.Close(); 
+0

私はあなたのコードで[DataRow ... adapter.Update(data);]というコードを置き換えようとしました。実際、私には2つの問題があります。まず、そのフィールドに "NULL"が許可されているため、パラメータ "@gender"を持つ "ArgumentNullException"が取得されます。第二に、すべてのフィールドが提供されている場合、私のプログラムを実行した後、私のデータベースに変更が見られません。私は何をしようとすることができますか? – KrK

+0

また、次のようにパラメータを指定することもできます: 'com.Parameters.Add(新しいSqlCeParameter(" @ name "、SqlDbType.NText、100){Value = textBox1.Text});' – papaiatis

+0

接続が有効で、 'cmd.ExecuteNonQuery()'はExceptionをスローしません.SqlCeデータベースに新しいレコードが表示されます。 'ExecuteNonQuery()'は影響を受ける行の数である 'int'を返します。それもチェックしてください。 – papaiatis

関連する問題