linqとc#を使ってデータベースにレコードを挿入するには?Linqを使って新しいレコードを挿入
私の関数は次のようになります。そのは私にこの例外をスロー
private void submitEntry(object sender, RoutedEventArgs e) {
using (var ctx = new ServiceDataContext()) {
try {
String selection1 = comboBox1.SelectedItem.ToString();
String[] spl1 = selection1.Split(' ');
var cond1 = spl1[ 0 ];
var cond2 = spl1[ 1 ];
var query1 = (from p in ctx.products where p.brand == cond1 && p.model == cond2 select p.prodId).FirstOrDefault();
String selection5 = comboBox5.SelectedItem.ToString();
String[] spl5 = selection5.Split(',',' ');
var cond1_5 = spl5[ 0 ];
var cond2_5 = spl5[ 2 ];
var query5 = (from c in ctx.contactPersons where c.lastName == cond1_5 && c.firstName == cond2_5 select c.contId).FirstOrDefault();
var query4 = (from h in ctx.hospitals where h.name == comboBox4.SelectedItem.ToString() select h.hospId).FirstOrDefault();
mainWindow mainClass = new mainWindow();
MessageBox.Show(mainClass.logId.ToString());
entry ent = new entry { prodId = Convert.ToInt32(query1),
prodQty = Convert.ToInt32(textBox.Text),
hospId = Convert.ToInt32(query4),
contId = Convert.ToInt32(query5),
freqMaintenance = Convert.ToInt32(textBox1.Text),
empId = Convert.ToInt32(emp)
};
ctx.entries.InsertOnSubmit(ent);
ctx.SubmitChanges();
} catch {
MessageBox.Show("ERROR 404: Database Not Found");
}
}
}
:Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.Linq.dll
iはMessageBox.Show("1");
を追加することによってそれをテストしようとしましたが、ctx.SubmitChanges();
このエラーはどこで発生しますか? "それは後にapearingを止めるように見える"とはどういう意味ですか? – Enigmativity
comboBox4.SelectedItem.ToString()のlinq query4の外に文字列変数を作成し、その変数に置き換えます。 var value = comboBox4.SelectedItem.ToString(); var query4 =(ctx.hospitalsのhから、h.name == valueはh.hospIdを選択します).FirstOrDefault(); – KrishnaDhungana