【C#.NET】dataGridView1的数据导入到SQL数据库中

来源:互联网 发布:十天学会单片机郭天祥2 编辑:程序博客网 时间:2024/04/30 18:46
 //将dataGridView1的数据导入到SQL数据库中   
        public void DataGridViewToSql()
        {            
            SqlConnection conn = new SqlConnection("server=yoti,6060; user id = sa; pwd = 2950001; database = hbposev9");                        
            try
            {
                conn.Open();
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                { 
                    SqlCommand comm = new SqlCommand();
                    string gdh = this.dataGridView1.Rows[i].Cells[0].Value.ToString().Trim();
                    string pn = this.dataGridView1.Rows[i].Cells[1].Value.ToString().Trim();                    
                    string SqlStr = "INSERT testbiao(sheet222,名称) VALUES ('" + gdh + "','" + pn + "')";
                    comm.CommandText = SqlStr;
                    comm.Connection = conn;                    
                    comm.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新失败,失败原因:" + ex.Message);                
            }
            finally
            {
                conn.Close();
            }
        }




//Cells行。Rows列
0 0
原创粉丝点击