c# - 有没有办法让这段代码在数据库中插入所有当前数据?
问题描述
我想要的是从我的数据网格视图中将所有行和列中的所有数据插入到我的数据库中。我没有收到任何错误,但这不起作用,不在我的数据库中插入任何值。
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = con.CreateCommand();
for (int i = 0; i < dgEdit.Rows.Count; i++)
{
DateTime ds = DateTime.Now;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Tb BL_Attendance(Course, Subject,
Year, Section, Name, Room, SeatNo, Status, Date) VALUES('" +
dgvAtt.Rows[i].Cells[0].Value + "', '" +
dgvAtt.Rows[i].Cells[1].Value + "', '" +
dgvAtt.Rows[i].Cells[2].Value + "', '" +
dgvAtt.Rows[i].Cells[3].Value + "', '" +
dgvAtt.Rows[i].Cells[4].Value + "', '" +
dgvAtt.Rows[i].Cells[5].Value + "', '" +
dgvAtt.Rows[i].Cells[6].Value + "', '" +
dgvAtt.Rows[i].Cells[7].Value + "', @Date) ";
cmd.Parameters.AddWithValue("@Date", ds);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Updated! please check the report", "Save",
MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
我期待这会将我所有的数据网格值插入到表中
解决方案
下面的逻辑尝试尽可能接近您的实现,但在处理数据库连接时考虑到最佳实践:
// You will need the following namespaces:
// using System;
// using System.Data;
// using System.Data.SqlClient;
// using System.Windows.Forms;
var connectionString = "Your SQL Server connection string";
using (var con = new SqlConnection(connectionString))
{
con.Open();
var cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"INSERT INTO Tbl_Attendance (Course, Subject, Year, Section, Name, Room, SeatNo, Status, Date) " +
"VALUES (@Course, @Subject, @Year, @Section, @Name, @Room, @SeatNo, @Status, @Date)";
var courseParam = cmd.Parameters.Add("@Course", SqlDbType.VarChar, 50);
var subjectParam = cmd.Parameters.Add("@Subject", SqlDbType.VarChar, 50);
var yearParam = cmd.Parameters.Add("@Year", SqlDbType.VarChar, 50);
var sectionParam = cmd.Parameters.Add("@Section", SqlDbType.VarChar, 50);
var nameParam = cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50);
var roomParam = cmd.Parameters.Add("@Room", SqlDbType.VarChar, 50);
var seatNoParam = cmd.Parameters.Add("@SeatNo", SqlDbType.VarChar, 50);
var statusParam = cmd.Parameters.Add("@Status", SqlDbType.VarChar, 50);
var dateParam = cmd.Parameters.Add("@Date", SqlDbType.DateTime);
dateParam.Value = DateTime.Now;
// If you are going to insert a lot of records, it's advised to call the Prepare method on your SqlCommand.
// Un-comment the line below if you want to see how this behaves.
// cmd.Prepare();
foreach (DataGridViewRow row in dgEdit.Rows)
{
courseParam.Value = row.Cells[0].Value;
subjectParam.Value = row.Cells[1].Value;
yearParam.Value = row.Cells[2].Value;
sectionParam.Value = row.Cells[3].Value;
nameParam.Value = row.Cells[4].Value;
roomParam.Value = row.Cells[5].Value;
seatNoParam.Value = row.Cells[6].Value;
statusParam.Value = row.Cells[7].Value;
cmd.ExecuteNonQuery();
}
}
MessageBox.Show("Updated! please check the report", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
更改背后的原因:
using
如果可能,在语句中实例化并打开 SqlConnection 连接 。这将确保您在范围内完成连接时始终关闭(处置)连接。- 与任何外部输入交互时,始终在查询中使用参数,以避免 Bobby's mom 漏洞!(https://xkcd.com/327/)。这将确保您的代码不易受到 SQL 注入的影响。如您所见,我添加了一些参数,但是因为您没有提供我创建的表架构,
VARCHAR(50)
但@Date
很明显您正在保存一个System.DateTime
. 请随时根据SqlDbType.VarChar
需要将其更改为正确的类型。 - 我将调用移到范围
MessageBox.Show
之外,using
因此它不会干扰连接处理。
您可以对此逻辑进行的另一项增强是实现System.Transactions.TransactionScope
类的使用(您必须添加对 System.Transactions.dll 的引用),以确保如果在任何插入过程中出现错误,则不会进入committed
数据库。
推荐阅读
- objective-c - 目标 c 使用未声明的标识符“FIRApp”crashlytics
- string - 将多个月份字符串替换为月份整数
- c# - 从没有已知结构的 JSON 中检索所有 JSON 键的值
- python - 如何在 Tkinter GUI 中截屏并显示相同的内容?
- android - 有没有办法或内置方法从android材料TextInputLayout(om.google.android.material.textfield.TextInputLayout)获取文本?
- amazon-web-services - 如何使用 nat 网关限制 VPC 中的 Internet 访问?
- python - 为什么这个 python 递归函数按它的顺序返回值?
- php - 获取 2 个日期之间时差的统计信息
- android - 在 Android 中使用机器学习套件的人脸置信度
- python-3.x - xml到多个熊猫数据框