datagridview控件需要更新database..pls给出一个简单的代码来执行此操作 [英] datagridview control need to update database..pls give a simple code for doing this

查看:70
本文介绍了datagridview控件需要更新database..pls给出一个简单的代码来执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview控件,它包含两个datagridviewcombobox列,它们由sql表和一个日期列填充。我需要将这个datagridview详细信息更新到数据库..pls提供了一个简单的代码来执行此操作。



这里我的代码在表单加载事件和按钮插入点击事件



i have a datagridview control which contains two datagridviewcombobox column which are populated by sql table and a date column . I need to update this datagridview details to the database..pls give a simple code for doing this.

Here my code in form load event and button insert click event

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class dgvccombunbound
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("DBCS").ConnectionString)

    Private Sub dgvccombunbound_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.Open()
        Dim cmdname As String = "select Name from tblname"
        Dim daname As SqlDataAdapter = New SqlDataAdapter(cmdname, con)
        Dim dsname As DataSet = New DataSet
        daname.Fill(dsname, "tblname")

        Dim daname1 As SqlDataAdapter = New SqlDataAdapter(cmdname, con)
        Dim dsname1 As DataSet = New DataSet
        daname1.Fill(dsname1, "tblname")
        con.Close()
       
        Dim TextBoxColumn2 As New DataGridViewTextBoxColumn
        TextBoxColumn2.HeaderText = "Date"
        TextBoxColumn2.DataPropertyName = "Date"

        Dim ComboBoxColumn1 As New DataGridViewComboBoxColumn
        With ComboBoxColumn1
            .HeaderText = "Employee"
            .DataPropertyName = "Employee"
            .DataSource = dsname.Tables("tblname").DefaultView
            .DisplayMember = "Name"
        End With
        Dim ComboBoxColumn2 As New DataGridViewComboBoxColumn
        With ComboBoxColumn2
            .HeaderText = "Manager"
            .DataPropertyName = "Manager"
            .DataSource = dsname1.Tables("tblname").DefaultView
            .DisplayMember = "Name"
        End With
       
        DataGridView1.Columns.Add(TextBoxColumn2)
        DataGridView1.Columns.Add(ComboBoxColumn1)
        DataGridView1.Columns.Add(ComboBoxColumn2)
       

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con.Open()
        Dim i As Integer
        
        Dim cellvalue2 As string = ""
        Dim cellvalue3 As String = ""
        Dim cellvalue4 As String = ""

        For i = 0 To Me.DataGridView1.Rows.Count - 2
            
            cellvalue2 = DataGridView1.Rows(i).Cells("Date").Value.ToString
            cellvalue3 = DataGridView1.Rows(i).Cells("Employee").Value.ToString
            cellvalue4 = DataGridView1.Rows(i).Cells("Manager").Value.ToString
            Dim constr As String = "insert into tblemp (Date, Employee, Manager) values(@Date, @Employee,@Manager)"
            Dim cmd As SqlCommand = New SqlCommand(constr, con)
            cmd.Parameters.AddWithValue("@Date", cellvalue2)
            cmd.Parameters.AddWithValue("@Employee", cellvalue3)
            cmd.Parameters.AddWithValue("@Manager", cellvalue4)
            cmd.ExecuteNonQuery()
        Next
    End Sub
End Class





错误获取时尝试通过按钮单击事件将数据插入数据库。错误是1.日期列名为date无法找到参数名称:columnname和其他列相同



error getting when try to insert data to database by button click event. error are 1. date column named date cannot be found parameter name:columnname and same for other columns also

推荐答案

懒惰的方式(找到简单的代码)可能不是学习东西的最佳方式。总体而言,轻松在不久的将来会变得艰难。正确研究这个主题要好得多。首先,考虑数据绑定而不是手动代码数据库更新。你可以从这里开始: https://msdn.microsoft.com /en-us/library/vstudio/fbk67b6z%28v=vs.110%29.aspx [ ^ ]。



如果你还想要直接在您的代码中使用UI和数据库,这不是问题。您需要分别学习所有方面:使用查询从数据库获取数据,使用更新语句, DataGridView 的填充以及从此组件获取数据。你有你需要做的一切:MSDN文档和ADO.NET和这个控件:

https://msdn.microsoft.com/en-us/library/e80y5yhx%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft .com / zh-CN / library / system.windows.forms.datagridview%28v = vs.110%29.aspx [ ^ ]。



-SA
A lazy way (finding simple code) might not be the best way to learn things. Overall, "easy" can turn hard in near future. It's much better to study the subject properly. First of all, instead of "manual code" database update, consider data binding. You can start here: https://msdn.microsoft.com/en-us/library/vstudio/fbk67b6z%28v=vs.110%29.aspx[^].

If you still want to work with UI and database directly in your code, this is not a problem. You need to learn all aspects separately: getting data from your database using queries, using update statements, population of DataGridView and getting data from this component. You have all you need to do it: MSDN documentation and ADO.NET and this control:
https://msdn.microsoft.com/en-us/library/e80y5yhx%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview%28v=vs.110%29.aspx[^].

—SA


这篇关于datagridview控件需要更新database..pls给出一个简单的代码来执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆