保存到sqlDatabase后将所有项目添加到Listview [英] Add all items to Listview after save to sqlDatabase
本文介绍了保存到sqlDatabase后将所有项目添加到Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
再次给我喂它PingLocalHost,任何人都可以教我或帮助我如何
添加数据并仅在列表视图中显示,并且当用户完成在列表视图中输入所有数据并决定将所有数据保存在列表视图中时,他们可以轻松地单击保存"按钮,并将他们输入的所有数据保存到sql数据库中,在谷歌,但我找不到答案.我试图使用sqlDatareader进行listview,但是我不适合我.拜托,我,我,伙计们. tnx
hi it me again PingLocalHost, can any one teach me or help me how
add data and display in listview only, and when the user is finish to input all data in listview and decide to save all data in listview they easily click a SAVE button and all data they input is save to the sql dataBase , i trued to search in google but i cant find the answer . i tried to use a sqlDatareader to listview but i not work for me . please me me guys. tnx
推荐答案
Google兄弟帮不上忙吗?我不知道!这里有很多示例: ^ ]
好的,我将展示如何执行此操作.
1)创建新项目(Windows应用程序)
2)在Form1上:
-添加ListView并将其名称更改为:LVData2Save
-添加Button并将其名称更改为:CmdSave
3)复制以下代码并将其粘贴到模块Form1类中
记住!这个例子很简单.为了更专业,您需要对其进行更改.阅读有关以下内容的更多信息:创建Windows窗体应用程序 [ ^ ]
看看:
卡尔·普罗斯曼网站 [连接字符串网站 [ ^ ]
Is it true that Brother Google can''t help you? I''m not sure! There are tousdend of examples: inserting data into sql database[^]
Ok, i will show how to do this.
1) Create new project (Windows application)
2) On the Form1:
- add ListView and change it name to: LVData2Save
- add Button and change it name to: CmdSave
3) Copy code below and paste it into module Form1 class
Remember! This example is very simple. To be more proffessional you need to change it. Read more about: creating windows forms applications[^]
Take a look at:
Carl Prothman site[^]
connectionstrings site[^]
Public Class Form1
Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click
Dim i As Integer = 0, retVal As Integer = 0
Dim sSQL As String = String.Empty, sConn As String = String.Empty
Dim oConn As SqlClient.SqlConnection = Nothing, oComm As SqlClient.SqlCommand = Nothing
Try
sConn = "Data Source=YourServerName;" & _
"Initial Catalog=A_TEST;Integrated Security=True"
oConn = New SqlClient.SqlConnection(sConn)
oConn.Open()
For i = 0 To Me.LVData2Save.Items.Count - 1
sSQL = "INSERT INTO [Table_1] (Field1)" & vbCr & _
"VALUES ('" & Me.LVData2Save.Items(i).Text & "')"
oComm = New SqlClient.SqlCommand(sSQL, oConn)
'get count of records affected
retVal = oComm.ExecuteNonQuery()
If retVal = 0 Then
MsgBox("Can't add this element: '" & Me.LVData2Save.Items(i).Text & "'", MsgBoxStyle.Information, "Error (row=" & i.ToString & ")")
'Exit For
End If
Next
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
Finally
oComm.Dispose()
oComm = Nothing
If Not oConn Is Nothing AndAlso oConn.State = ConnectionState.Open Then oConn.Close()
oConn = Nothing
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 0
With Me.LVData2Save
.View = View.Details
.Columns.Add("Data")
.Columns(0).TextAlign = HorizontalAlignment.Left
End With
For i = 1 To 10
Me.LVData2Save.Items.Add("Item " & i.ToString)
Next
End Sub
End Class
这篇关于保存到sqlDatabase后将所有项目添加到Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文