2015-05-16 37 views
6

我試圖從DataGridView使用C#向SQL Server數據庫表中插入5條記錄。如何使用DataGridView在SQL Server中存儲多個記錄

從我的代碼它需要輸入幾個記錄,但只插入第一個記錄在數據庫中。任何人都可以通過單擊保存按鈕來幫助我保存數據庫中的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(); 
    } 

回答

5

問題是這樣的內「爲」循環,你是在循環中不使用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, UserName, PassImage) VALUES ('" + col1 + "','" + col2 + "','" + col3 + "')"; 
 
    this.getcom(insert_sql); 
 
} 
 
             

變化的代碼的邏輯,如果需要的話。

+0

非常感謝!它的工作.... – Sumi

+0

你可以告訴,我怎麼能限制用戶輸入正好5條記錄,否則會產生一條消息。 – Sumi

+0

在** for循環**, - >'for(i = 0; i <6; i ++)'中進行更改,這樣只會插入5條記錄。如果你想顯示消息使用datagridview客戶端事件,或者嘗試在** if if(dataGridView1.Rows.count> 5)''中的邏輯'。@ Sumi – Balaji

4
for (int i = 5; i < dataGridView1.Rows.Count; i++) 

更改爲for (int i = 0; i < dataGridView1.Rows.Count; i++)

你是一個經過了這麼分配5i插入它會跳出循環,因爲i將成爲6

還包括「我」,而指定的datagridview row..else它將始終指向一個行並插入5行,但相同的值

相關問題