显示从SQL服务器到listvew的数据 [英] Display data from SQL server to listvew

查看:62
本文介绍了显示从SQL服务器到listvew的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im geek in vb.net,我已经从具有ms访问数据库的应用程序中创建了vb.net。现在我需要将此应用程序用于多用户,我想使用SQL服务器,我使用此代码显示从访问数据库到列表视图的数据:



我尝试了什么:



 con.ConnectionString =   provider = microsoft.ace.oledb.12.0; data source = | datadirectory | \ noorapp.accdb; 
con.Open()

Dim dt 作为 DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As OleDbDataAdapter( select * from& Year(Now)& 其中cmonth ='& m& 'order by cdate DESC,con)
da.Fill(dt)
Dim myrow As DataRow
For 每个 myrow dt.Rows
ListView1.Items.Add(myrow.Item) ( 0 ))。ToString()
ListView1.Items(ListView1.Items.Count - 1 )。SubItems.Add(myrow.Item( 2 ))
ListView1.Items(ListView1.Items.Count - 1 )。SubItems.Add(myrow.Item( 3 ))
ListView1.Items(ListView1.Items.Count - 1 )。SubItems.Add(myrow.Item( 4 ))
ListView1.Items(ListView1。 Items.Count - 1 )。SubItems.Add(myrow.Item( 5 ))
ListView1.Items(ListView1.Items.Count - 1 )。SubItems.Add(myrow.Item( 6 ))
ListView1.Items(ListView1.Items.Count - < span class =code-digit> 1 )。SubItems.Add(myrow.Item( 7 ))
下一页



如何使这个服务器的sql服务器也能解决?谢谢

解决方案

用SqlConnection和SqlDataAdapter替换OleDbConnection和OleDBDataAdapter,然后适当地设置连接字符串。

除非你做的事情相当奇怪使用命令(除了表名作为数字之外,我看不到任何奇怪的东西 - 我将其更改为Accounts2016,或者为了避免混淆),它应该正常工作。



但不要那样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。


从一开始你的方法就错了。通过串联从UI获取的字符串组成的查询。不仅重复的字符串连接是低效的(因为字符串是不可变的;我是否必须解释为什么它会使重复连接变坏?),但是有更重要的问题:它打开了通向良好的大门已知的漏洞称为 SQL注入



这是它的工作原理: http://xkcd.com/327



你明白了吗?从控件中获取的字符串可以是任何东西,包括......一段SQL代码。



怎么办?只需阅读有关此问题和主要补救措施:参数化语句 http://en.wikipedia.org/ wiki / SQL_injection



使用ADO.NET,使用:http://msdn.microsoft.com/en-us/library/ff648339.aspx



请参阅我过去的答案有更多细节:

在com.ExecuteNonQuery中更新EROR( );

嗨名字没有显示名称?



参见:

http://www.codeproject.com / Articles / 9378 / SQL-Injection-Attacks-and-Some-Tips-on-How-to-Prev

http://www.troyhunt.com/2013/07/everything-you-想知道-sql.html



-SA

Im geek in vb.net, I already make vb.net from application with ms access data base. now i need to use this application for multi user and i want to use SQL server, i use this code to show data from access data base to listview :

What I have tried:

con.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\noorapp.accdb;"
        con.Open()

Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter("select * from " & Year(Now) & " where cmonth='" & m & "' order by cdate DESC", con)
        da.Fill(dt)
        Dim myrow As DataRow
        For Each myrow In dt.Rows
            ListView1.Items.Add(myrow.Item(0)).ToString()
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(3))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(4))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(5))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(6))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(7))
        Next 


any solution to how make this work for sql server also? thanks

解决方案

Replace the OleDbConnection and OleDBDataAdapter with SqlConnection and SqlDataAdapter, then setup your connection string appropriately.
Unless you are doing something fairly odd with the commands (and I can't see anything too odd there other than the table name as a number - I'd change that to "Accounts2016", or whatever to avoid confusion) it should "just work".

But don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

See also:
http://www.codeproject.com/Articles/9378/SQL-Injection-Attacks-and-Some-Tips-on-How-to-Prev
http://www.troyhunt.com/2013/07/everything-you-wanted-to-know-about-sql.html

—SA


这篇关于显示从SQL服务器到listvew的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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