首先 , 我 真 的 很 抱歉 我 的 英语 发音 。 我 有 一 个 datagridview , 我 把 它 连接 到 sql server , 它 已经 被 浸入 到 我 的 项目 在 visual studio 。 但是 , 当 我 保存 信息 在 datagridview , 它 的 工作 , 但 在 数据 库 中 , 它 不 保存 在 我 的 数据 库 在 sql server 。 你 能 帮助 我 , 请
这 是 我 的 密码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lab3_Bai09
{
public partial class Form1 : Form
{
SqlConnection cnn = null;
string connetionString;
public Form1()
{
InitializeComponent();
connetionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\QLSV.mdf;Integrated Security=True";
cnn = new SqlConnection(connetionString);
cnn.Open();
cnn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
txtB2.Text = txtB1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
txtB1.Text = txtB2.Text;
}
private void Save_Click(object sender, EventArgs e)
{
cnn.Open();
SqlCommand sqlCommand;
SqlDataAdapter adapter = new SqlDataAdapter();
string sql = "";
string sex = "";
if(checkBox1.Checked == true)
{
sex = "Nam";
}
else
{
sex = "Nu";
}
string[] subjectList = txtB1.Text.Split('\n');
int countSubject = subjectList.Length;
sql = $"insert into SINHVIEN(MSSV, HOTEN, CHUYENNGANH, GIOITINH, SOMON) values ('{textBox1.Text}', '{textBox2.Text}', '{comboBox1.Text}', '{sex}', '{countSubject}')";
sqlCommand = new SqlCommand(sql, cnn);
adapter.InsertCommand = new SqlCommand(sql, cnn);
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Đã thêm mới dữ liệu thành công!");
this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
textBox1.Clear();
textBox2.Clear();
checkBox1.Checked = false;
checkBox2.Checked = false;
txtB1.Clear();
txtB2.Clear();
cnn.Close();
}
private void Delete_Click(object sender, EventArgs e)
{
cnn.Open();
DialogResult result = MessageBox.Show("Bạn có muốn xóa dòng đã chọn?", "Delete", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
SqlCommand sqlCommand;
SqlDataAdapter adapter = new SqlDataAdapter();
string sql = "";
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
string c = selectedRow.Cells[0].Value.ToString();
sql = $"delete from SINHVIEN where MSSV = {c}";
sqlCommand = new SqlCommand(sql, cnn);
adapter.InsertCommand = new SqlCommand(sql, cnn);
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Xóa dữ liệu thành công");
this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
}
cnn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'qLSVDataSet1.SINHVIEN' table. You can move, or remove it, as needed.
this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
DialogResult result = MessageBox.Show("Bạn có muốn thoát?", "Exit", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else
{
e.Cancel = true;
}
}
else
{
e.Cancel = true;
}
}
}
}
中 的 每 一 个
1条答案
按热度按时间js81xvg61#
看 起来 您 只是 从 文本 框 和 组合 框 中 提取 值 。 有 一 行
this.sINHVIENTableAdapter1.Fill(this.qLSVDataSet1.SINHVIEN);
, 但 这些 声明 在 哪里 ? 我 没有 看到 您 调用 任何 将 数据 从 该 适配 器 或 数据 集 写 回 数据 库 的 操作 。 在 声明 sqlCommand 后 , 您 将 SQL 命令 的 新 示例 写入 适配 器 , 您 是否 打算 这样 做 ?中 的 每 一 个
我要 说 的 是 , 您 正在 为 SQL 注入 敞开 大门 。 SQL
INSERT
应该 更 像 这样 处理 :格式
如何 将 一 个 数据 网格 视图 绑定 到 一 个 数据 表 :How to bind Dataset to DataGridView in windows application 的 最 大 值 。