Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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
Posted
Updated 24-Apr-16 17:02pm
v2

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.
 
Share this answer
 
Comments
Noor Phone 24-Apr-16 12:53pm    
how can i do this ?
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900