Мой проект Visual Studio удаляет и редактирует элементы в базе данных ОК, как и должно, но не добавляет новую запись при нажатии сохранить. Я думаю, что он вводит все нормально, однако он не сохранит вставленную запись, как только я нажму кнопку Сохранить.
public partial class Form1 : Form
{
SqlConnection sqlConnection = null;
public void connect()
{
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = @"Data Source=(localdb)\ProjectsV13;Initial Catalog=mydatabase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
sqlConnection.Open();
}
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
connect();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM [Table]";
SqlDataReader dataReader = sqlCommand.ExecuteReader();
dataGridView1.Columns.Add("Id", "ID");
dataGridView1.Columns.Add("Name", "NAME");
dataGridView1.Columns.Add("Email", "EMAIL");
for (int i = 0; dataReader.Read(); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["ID"].Value = dataReader["Id"].ToString();
dataGridView1.Rows[i].Cells["NAME"].Value = dataReader["Name"].ToString();
dataGridView1.Rows[i].Cells["EMAIL"].Value = dataReader["Email"].ToString();
}
sqlConnection.Close();
}
private void Button2_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value.ToString());
string name = dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value.ToString();
string email = dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value.ToString();
connect();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "insert into [Table] (Id, Name, Email) values (" + id + ",'" + name + "','" + email + "')";
sqlCommand.CommandText = "SET IDENTITY_INSERT [Table] ON";
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
MessageBox.Show("Added");
}
private void Button3_Click(object sender, EventArgs e)
{
connect();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "Delete from [Table] where Id =" + dataGridView1.Rows[selectedid].Cells["ID"].Value;
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
dataGridView1.Rows.RemoveAt(selectedid);
MessageBox.Show("Record Deleted");
}
int selectedid = 0;
private void DataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
selectedid = e.RowIndex;
}
private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
string name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string email = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
connect();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "update [Table] set Name = '" + name + "', Email ='" + email + "' Where Id =" + id;
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch (Exception ex)
{
}
}
}
Я довольно плохо знаком с языком C # и надеялся, что кто-то с большим опытом сможет мне помочь, пожалуйста.