c# - 我的 Visual Studio 项目正在删除和编辑数据库中的项目 OK,但是在点击保存时它不会添加记录
问题描述
我的 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# 语言很陌生,希望有更多经验的人能够帮助我。
解决方案
推荐阅读
- javascript - 如何使用 Html/Jquery 中的特定路径/驱动器号打开文件浏览器?
- elasticsearch - Elasticsearch - 查找缺少两个字段的文档
- ios - Swift 中带有 UITableView 的基于 Rx 的树结构
- php - JMS 反序列化部分 xml
- java - 切换场景时如何保持FXML文件的变化
- elasticsearch - Elasticsearch 使用默认值更新索引上的新映射
- c# - C#在Json字符串中添加变量
- hibernate - 部分主键列上的 JPA OneToOne 映射
- sql - 如何使用 oracle 查找包含特定字符串值的列名
- android - MVVMCross 6.4.3 还原未按预期工作