c# – 如何使用DataGridView在SQL Server中存储多个记录

前端之家收集整理的这篇文章主要介绍了c# – 如何使用DataGridView在SQL Server中存储多个记录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用C#从DataGridView向sql Server数据库表中插入5条记录.

从我的代码中,它需要输入几个记录,但只插入数据库中的第一条记录.任何人都可以通过单击保存按钮帮助我在数据库中保存5条记录吗?

这是我的代码

DataSet ds = new DataSet();

    sqlConnection cs = new sqlConnection(@"Data Source=DELL-PC;Initial Catalog=Image_DB;Integrated Security=True");

    sqlDataAdapter da = new sqlDataAdapter();

    sqlCommand cmd = new sqlCommand();

    BindingSource Input = new BindingSource();
    DataView dview = new DataView();

    private void Form1_Load(object sender,EventArgs e)
    {
        //create a DataGridView Image Column
        DataGridViewImageColumn dgvImage = new DataGridViewImageColumn();
        //set a header test to DataGridView Image Column
        dgvImage.HeaderText = "Images";
        dgvImage.ImageLayout = DataGridViewImageCellLayout.Stretch;

        DataGridViewTextBoxColumn dgvId = new DataGridViewTextBoxColumn();
        dgvId.HeaderText = "ID";

        DataGridViewTextBoxColumn dgvName = new DataGridViewTextBoxColumn();
        dgvName.HeaderText = "Name";
        dataGridView1.Columns.Add(dgvId);
        dataGridView1.Columns.Add(dgvName);
        dataGridView1.Columns.Add(dgvImage);
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dataGridView1.RowTemplate.Height = 120;
        dataGridView1.AllowUserToAddRows = false;
    }

    // button add data to dataGridView
    // insert image from pictureBox to dataGridView 
    private void btn_Add_Click(object sender,EventArgs e)
    {
        MemoryStream ms = new MemoryStream();
        pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat);
        byte[] img = ms.ToArray();
        dataGridView1.Rows.Add(txt_UserID.Text,txt_Name.Text,img);
    }

    // browse image in pictureBox1 Click
    private void pictureBox1_Click(object sender,EventArgs e)
    {
        OpenFileDialog opf = new OpenFileDialog();
        opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
        if (opf.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(opf.FileName);
        }
    }

    private void btn_Save_Click(object sender,EventArgs e)
    {
        for (int i = 5; i < dataGridView1.Rows.Count; i++)
        {
            string col1 = dataGridView1[0,dataGridView1.CurrentCell.RowIndex].Value.ToString();
            string col2 = dataGridView1[1,dataGridView1.CurrentCell.RowIndex].Value.ToString();
            string col3 = dataGridView1[2,dataGridView1.CurrentCell.RowIndex].Value.ToString();

            string insert_sql = "INSERT INTO Input(UserID,UserName,PassImage) VALUES ('" + col1 + "','" + col2 + "','" + col3 + "')";

            this.getcom(insert_sql);
        }

        MessageBox.Show("Record Added");
    }

    public sqlConnection GetsqlConnection() //connection function
    {
        string str_sqlcon = "Data Source=DELL-PC;Initial Catalog=Image_DB;Integrated Security=True";

        sqlConnection mycon = new sqlConnection(str_sqlcon);
        mycon.Open();

        return mycon;
    }

    public void getcom(string sqlstr) //function for adding rows
    {
        sqlConnection sqlcon = this.GetsqlConnection(); // Watch out same string type as GetsqlConnection function
        sqlCommand sqlcom = new sqlCommand(sqlstr,sqlcon);
        sqlcom.ExecuteNonQuery();
        sqlcom.Dispose();
        sqlcon.Close();
        sqlcon.Dispose();
    }

解决方法

问题出在这个“for”循环中,你没有在for循环中使用i.

更好的尝试这个.

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
    string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
    string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
    string insert_sql = "INSERT INTO Input(UserID,'" + col3 + "')";
    this.getcom(insert_sql);
}

如果需要,更改代码逻辑.

猜你在找的C#相关文章