猿问

有没有办法让此代码插入数据库中的所有当前数据?

我想要的是将数据网格视图中所有行和列中的所有数据插入到我的数据库中。我没有收到任何错误,但这不起作用,没有在我的数据库中插入任何值。


    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();

我期望这将我的所有数据网格值插入到表中


慕妹3146593
浏览 154回答 2
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)){&nbsp; &nbsp; con.Open();&nbsp; &nbsp; var cmd = con.CreateCommand();&nbsp; &nbsp; cmd.CommandType = CommandType.Text;&nbsp; &nbsp; cmd.CommandText =&nbsp; &nbsp; &nbsp; &nbsp; "INSERT INTO Tbl_Attendance (Course, Subject, Year, Section, Name, Room, SeatNo, Status, Date) " +&nbsp; &nbsp; &nbsp; &nbsp; "VALUES (@Course, @Subject, @Year, @Section, @Name, @Room, @SeatNo, @Status, @Date)";&nbsp; &nbsp; var courseParam = cmd.Parameters.Add("@Course", SqlDbType.VarChar, 50);&nbsp; &nbsp; var subjectParam = cmd.Parameters.Add("@Subject", SqlDbType.VarChar, 50);&nbsp; &nbsp; var yearParam = cmd.Parameters.Add("@Year", SqlDbType.VarChar, 50);&nbsp; &nbsp; var sectionParam = cmd.Parameters.Add("@Section", SqlDbType.VarChar, 50);&nbsp; &nbsp; var nameParam = cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50);&nbsp; &nbsp; var roomParam = cmd.Parameters.Add("@Room", SqlDbType.VarChar, 50);&nbsp; &nbsp; var seatNoParam = cmd.Parameters.Add("@SeatNo", SqlDbType.VarChar, 50);&nbsp; &nbsp; var statusParam = cmd.Parameters.Add("@Status", SqlDbType.VarChar, 50);&nbsp; &nbsp; var dateParam = cmd.Parameters.Add("@Date", SqlDbType.DateTime);&nbsp; &nbsp; dateParam.Value = DateTime.Now;&nbsp; &nbsp; // If you are going to insert a lot of records, it's advised to call the Prepare method on your SqlCommand.&nbsp; &nbsp; // Un-comment the line below if you want to see how this behaves.&nbsp; &nbsp; // cmd.Prepare();&nbsp; &nbsp; foreach (DataGridViewRow row in dgEdit.Rows)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; courseParam.Value = row.Cells[0].Value;&nbsp; &nbsp; &nbsp; &nbsp; subjectParam.Value = row.Cells[1].Value;&nbsp; &nbsp; &nbsp; &nbsp; yearParam.Value = row.Cells[2].Value;&nbsp; &nbsp; &nbsp; &nbsp; sectionParam.Value = row.Cells[3].Value;&nbsp; &nbsp; &nbsp; &nbsp; nameParam.Value = row.Cells[4].Value;&nbsp; &nbsp; &nbsp; &nbsp; roomParam.Value = row.Cells[5].Value;&nbsp; &nbsp; &nbsp; &nbsp; seatNoParam.Value = row.Cells[6].Value;&nbsp; &nbsp; &nbsp; &nbsp; statusParam.Value = row.Cells[7].Value;&nbsp; &nbsp; &nbsp; &nbsp; cmd.ExecuteNonQuery();&nbsp; &nbsp; }}MessageBox.Show("Updated! please check the report", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);更改背后的原因:如果可能,请在语句中实例化并打开 SqlConnection 连接。这将确保在作用域中完成连接时,始终关闭(释放)连接。using在与任何外部输入交互时,始终在查询中使用参数,以避免Bobby的妈妈被利用!(https://xkcd.com/327/)。这将确保您的代码不容易受到 SQL 注入的影响。如您所见,我添加了一些参数,但是由于您没有提供我当时创建的表架构,因此除了很明显您正在保存.请根据需要随意将 更改为正确的类型。VARCHAR(50)@DateSystem.DateTimeSqlDbType.VarChar我将呼叫移到了范围之外,因此它不会干扰连接处理。MessageBox.Showusing可以对此逻辑执行的另一项增强功能是实现类的使用(必须添加对 System.Transactions 的引用.dll),以确保如果在任何插入过程中出现错误,则没有任何内容进入数据库。System.Transactions.TransactionScopecommitted

至尊宝的传说

您的原型是正确的,但您犯了一个错误,返回一个 Object,必须将其转换为或表中可能匹配的列数据。dgvAtt.Rows[i].Cells[0].ValueToString()假设这是我的形式我创建了一个表“人员”代码源码private void button1_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlCommand cmd = new SqlCommand();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd = con.CreateCommand();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < dgvAtt.Rows.Count - 1; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.CommandType = CommandType.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string Query = "INSERT INTO Person (idPerson, fullnamePerson) VALUES (" + dgvAtt.Rows[i].Cells[0].Value.ToString() + ",'" + dgvAtt.Rows[i].Cells[1].Value.ToString() + "')";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.CommandText = Query;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.ExecuteNonQuery();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Updated! please check the report", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show(ex.Message);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }不要编写任何参数,因为这会给你一个例外。
随时随地看视频慕课网APP
我要回答