C#通过DataGridView对数据库进行增删改查

C#通过DataGridView对数据库进行增删改查,第1张

(1)增加新的数据

(2)显示选中行数据

(3)修改选中行数据

(1)执行SQL返回受影响的行数的ExecuteNoQuery方法

(2)执行SQL返回查询结果中第一行第一列的值的ExecuteScalar方法

(3)执行SQL返回一个DataTable的ExecuteDataTable方法

(4)执行SQL返回一个SqlDataReader的ExecutedReader方法

Private Sub Button2_Click(ByVal sender As SystemObject, ByVal e As SystemEventArgs) Handles Button2Click

Dim sqlStr As String = "insert into tb values("

Dim i As Integer

For i = 0 To DataGridView1ColumnsCount - 1

sqlStr &= "'" & DataGridView1Rows(DataGridView1RowsCount - 1)Cells(i) & "'"

If i < DataGridView1ColumnsCount - 1 Then sqlStr &= ","

Next

sqlStr &= ")"

Dim con As New SqlConnection(connectionString) '这个自己写去

conOpen()

Dim cmd As New SqlCommand(sqlStr, con)

Try

Dim result As Integer = cmdExecuteNonQuery()

If result > 0 Then

MsgBox "添加数据成功"

Else

MsgBox "添加数据失败"

End If

Catch (ex As Exception)

MsgBox "添加数据过程发生异常,原因:" & exMessage

End Try

End Sub

这个需要重写一些model的方法

当然你需要编辑也要实现自己的delegate

class MyTableDelgate : public QStyledItemDelegate

{

Q_OBJECT

public:

explicit MyTableDelgate(QObject parent = 0);

protected:

//basic function for a read-only delegate, you can draw anything with the painter

void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

//edit 5 function

QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

void setEditorData(QWidget editor, const QModelIndex &index) const;

void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const;

void updateEditorGeometry ( QWidget editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

private:

QLineEdit m_LineEdit;

};

model

class MyTableDelgate : public QStyledItemDelegate

{

Q_OBJECT

public:

explicit MyTableDelgate(QObject parent = 0);

protected:

//basic function for a read-only delegate, you can draw anything with the painter

void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

//edit 5 function

QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

void setEditorData(QWidget editor, const QModelIndex &index) const;

void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const;

void updateEditorGeometry ( QWidget editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

};

usepubs

go

ifexists(selectfromsysobjectswherename='NewView'andtype='v')

dropviewNewView

go

createviewNewView

as

selectfromtitles

go

create view view2(sno,savg,sname)

as select StudentSno,Sname,AVG((cast(StudentCourseGrade as int)))

from Student join StudentCourse

on StudentCourseSno=StudentSno

group by StudentSno,StudentSname

即可,反正Sno已经保证唯一了,再加上Sname也无法再干扰什么了。

以上就是关于C#通过DataGridView对数据库进行增删改查全部的内容,包括:C#通过DataGridView对数据库进行增删改查、VB.NET DATAGRIDVIEW写入数据库、怎么用QTableView修改数据库内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9439183.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存