我想要的是将datagrid视图中所有行和列中的所有数据插入数据库。我没有收到任何错误,但这无法在数据库中插入任何值。
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();
我期望这会将我所有的datagrid值插入表中
答案 0 :(得分:2)
下面的逻辑尝试尽可能地接近您的实现,但是在处理数据库连接时要考虑最佳实践:
// 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
语句。这将确保您始终保持连接
在示波器中完成操作后将其关闭(处置)。VARCHAR(50)
之后创建的表模式
@Date
的例外,很明显您要保存一个
System.DateTime
。请随时更改SqlDbType.VarChar
更改为所需的正确类型。MessageBox.Show
范围之外的using
,以免影响连接处理。对此逻辑的另一种增强功能是实现对System.Transactions.TransactionScope
类的使用(必须添加对System.Transactions.dll的引用),以确保在任何插入过程中是否出错。 ,没有任何committed
进入数据库。
答案 1 :(得分:0)
您的原型是正确的,但是您犯了一个错误,dgvAtt.Rows[i].Cells[0].Value
返回了一个对象,必须将其转换为ToString()
或表中可能与之匹配的列数据。
假设这是我的表单
代码源
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand();
cmd = con.CreateCommand();
for (int i = 0; i < dgvAtt.Rows.Count - 1; i++)
{
con.Open();
cmd.CommandType = CommandType.Text;
string Query = "INSERT INTO Person (idPerson, fullnamePerson) VALUES (" + dgvAtt.Rows[i].Cells[0].Value.ToString() + ",'" + dgvAtt.Rows[i].Cells[1].Value.ToString() + "')";
cmd.CommandText = Query;
cmd.ExecuteNonQuery();
con.Close();
}
MessageBox.Show("Updated! please check the report", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
请勿编写任何参数,因为这会给您一个例外。