私はデータベース内のテーブルに行を挿入することはできません。他人のために働くコードを試してみましたが、不運と思われます。そうするために、ある人が理想を持っていたら、それを私と共有してください。ありがとう。Visual Studio 2017でローカルデータベースに行を挿入するには#
private void button1_Click(object sender, EventArgs e)
{
string id;
SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.ConnectionString = Properties.Settings.Default.MyDBConnection;
sc.Open();
com.Connection = sc;
com.CommandText = ("Select MAX(Id) From Filme");
SqlDataReader drr;
drr = com.ExecuteReader();
if (drr.Read())
{
id = drr.GetValue(0).ToString();
Id = Int32.Parse(id);
}
sc.Close();
++Id;
try
{
sc.Open();
com.CommandText = "INSERT INTO Filme(Id,NumeFilm,Descriere) VALUES (" + Id + ",'" + label1.Text + "','" + label2.Text + "');";
com.ExecuteNonQuery();
MessageBox.Show("Saved");
com.Clone();
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
sc.Close();
}
エラーメッセージを投稿できますか? – Singaravelan
のIDはautoIncrementedですか? –
全体のアプローチは非常にエラーが発生しやすいです:1.次のIDをクエリmax idで検出してください。2. Little Bobby Tablesが挨拶しています –