如何在数据库sql server中添加datagridview时保存数据

cuxqih21  于 2022-11-21  发布在  SQL Server
关注(0)|答案(1)|浏览(250)

首先 , 我 真 的 很 抱歉 我 的 英语 发音 。 我 有 一 个 datagridview , 我 把 它 连接 到 sql server , 它 已经 被 浸入 到 我 的 项目 在 visual studio 。 但是 , 当 我 保存 信息 在 datagridview , 它 的 工作 , 但 在 数据 库 中 , 它 不 保存 在 我 的 数据 库 在 sql server 。 你 能 帮助 我 , 请
这 是 我 的 密码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace Lab3_Bai09
  12. {
  13. public partial class Form1 : Form
  14. {
  15. SqlConnection cnn = null;
  16. string connetionString;
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. connetionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\QLSV.mdf;Integrated Security=True";
  21. cnn = new SqlConnection(connetionString);
  22. cnn.Open();
  23. cnn.Close();
  24. }
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. txtB2.Text = txtB1.Text;
  28. }
  29. private void button2_Click(object sender, EventArgs e)
  30. {
  31. txtB1.Text = txtB2.Text;
  32. }
  33. private void Save_Click(object sender, EventArgs e)
  34. {
  35. cnn.Open();
  36. SqlCommand sqlCommand;
  37. SqlDataAdapter adapter = new SqlDataAdapter();
  38. string sql = "";
  39. string sex = "";
  40. if(checkBox1.Checked == true)
  41. {
  42. sex = "Nam";
  43. }
  44. else
  45. {
  46. sex = "Nu";
  47. }
  48. string[] subjectList = txtB1.Text.Split('\n');
  49. int countSubject = subjectList.Length;
  50. sql = $"insert into SINHVIEN(MSSV, HOTEN, CHUYENNGANH, GIOITINH, SOMON) values ('{textBox1.Text}', '{textBox2.Text}', '{comboBox1.Text}', '{sex}', '{countSubject}')";
  51. sqlCommand = new SqlCommand(sql, cnn);
  52. adapter.InsertCommand = new SqlCommand(sql, cnn);
  53. adapter.InsertCommand.ExecuteNonQuery();
  54. MessageBox.Show("Đã thêm mới dữ liệu thành công!");
  55. this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
  56. textBox1.Clear();
  57. textBox2.Clear();
  58. checkBox1.Checked = false;
  59. checkBox2.Checked = false;
  60. txtB1.Clear();
  61. txtB2.Clear();
  62. cnn.Close();
  63. }
  64. private void Delete_Click(object sender, EventArgs e)
  65. {
  66. cnn.Open();
  67. DialogResult result = MessageBox.Show("Bạn có muốn xóa dòng đã chọn?", "Delete", MessageBoxButtons.YesNoCancel);
  68. if (result == DialogResult.Yes)
  69. {
  70. SqlCommand sqlCommand;
  71. SqlDataAdapter adapter = new SqlDataAdapter();
  72. string sql = "";
  73. int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
  74. DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
  75. string c = selectedRow.Cells[0].Value.ToString();
  76. sql = $"delete from SINHVIEN where MSSV = {c}";
  77. sqlCommand = new SqlCommand(sql, cnn);
  78. adapter.InsertCommand = new SqlCommand(sql, cnn);
  79. adapter.InsertCommand.ExecuteNonQuery();
  80. MessageBox.Show("Xóa dữ liệu thành công");
  81. this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
  82. }
  83. cnn.Close();
  84. }
  85. private void Form1_Load(object sender, EventArgs e)
  86. {
  87. // TODO: This line of code loads data into the 'qLSVDataSet1.SINHVIEN' table. You can move, or remove it, as needed.
  88. this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
  89. }
  90. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  91. {
  92. if (e.CloseReason == CloseReason.UserClosing)
  93. {
  94. DialogResult result = MessageBox.Show("Bạn có muốn thoát?", "Exit", MessageBoxButtons.YesNo);
  95. if (result == DialogResult.Yes)
  96. {
  97. Application.Exit();
  98. }
  99. else
  100. {
  101. e.Cancel = true;
  102. }
  103. }
  104. else
  105. {
  106. e.Cancel = true;
  107. }
  108. }
  109. }
  110. }

中 的 每 一 个

js81xvg6

js81xvg61#

看 起来 您 只是 从 文本 框 和 组合 框 中 提取 值 。 有 一 行 this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN); , 但 这些 声明 在 哪里 ? 我 没有 看到 您 调用 任何 将 数据 从 该 适配 器 或 数据 集 写 回 数据 库 的 操作 。 在 声明 sqlCommand 后 , 您 将 SQL 命令 的 新 示例 写入 适配 器 , 您 是否 打算 这样 做 ?

  1. sqlCommand = new SqlCommand(sql, cnn);
  2. adapter.InsertCommand = new SqlCommand(sql, cnn);
  3. adapter.InsertCommand.ExecuteNonQuery();

中 的 每 一 个
我要 说 的 是 , 您 正在 为 SQL 注入 敞开 大门 。 SQL INSERT 应该 更 像 这样 处理 :

  1. sql = $"insert into SINHVIEN(MSSV, HOTEN, CHUYENNGANH, GIOITINH, SOMON) values (@mssv, @hoten, @chuy, @sex, @countsub)";
  2. sqlCommand = new SqlCommand(sql, cnn);
  3. sqlCommand.Parameters.AddWithValue("@mssv", textBox1.Text);
  4. sqlCommand.Parameters.AddWithValue("@hoten", comboBox1.Text);
  5. sqlCommand.Parameters.AddWithValue("@chuy", textBox2.Text);
  6. sqlCommand.Parameters.AddWithValue("@sex", sex);
  7. sqlCommand.Parameters.AddWithValue("@countSub", countSubject);
  8. adapter.InsertCommand = sqlCommand;
  9. adapter.InsertCommand.ExecuteNonQuery();

格式
如何 将 一 个 数据 网格 视图 绑定 到 一 个 数据 表 :How to bind Dataset to DataGridView in windows application 的 最 大 值 。

展开查看全部

相关问题