帮助我只是不能将行插入到数据库的表中,而且我尝试过的代码可以用于其他人,但似乎我很不吉利,而且我不知道还有什么要做的,如果有人得到了一个ideea请与我分享。谢谢。使用c将行插入到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确定下一个ID 2.小鲍比表问候 –