从数据库填充数据表 [英] Populate data table from database
问题描述
Hello world!
我正在用asp网开发购物车。
产品:Db
购物车:数据表
订单:Db(但订单现在不重要)
连接字符串工作正常,唯一的问题是:
如果我用插入sql命令填充数据表,它可以在db上工作,我不想要它。
我希望sql命令只写在数据表上不要修改数据库。
我尝试过:
这是代码:
Hello world !
i'm developing a shopping cart in asp net.
Products : Db
Shopping Cart : Data Table
Order : Db (but order is not important now)
The connections string work fine, the only problem is this :
If i populate the data table with a insert into sql command, it work on db and i don't want it.
I want that the sql command write only on the data table and don't modify the Database.
What I have tried:
This is the code :
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
'
Using cmd As New SqlCommand("INSERT INTO Carrello (ID,Nome,PrezzoFloat, Quantità) SELECT Id,NomeProdotto, PrezzoProdotto, Quantità FROM aProdotti WHERE ID='2'")
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End If
推荐答案
INSERT查询将数据插入数据库。
您想要选择数据。
An INSERT query will insert data to the database.
You want to SELECT data.
using cmd As New SqlCommand("SELECT Id,NomeProdotto, PrezzoProdotto, Quantità FROM aProdotti WHERE ID='2'")
同时检查列ID的数据类型 - 如果你希望它是一个数字然后使用数字类型而不是varchar。如果它已经是数字类型,那么在 2
Also check the data type of the Column ID - if you want it to be a number then use a numeric type not a varchar. If it already a numeric type then lose the single quotes around the 2
周围丢失单引号。
你的数据表不会填充wirh INSERT语句。
您需要传递select命令以使用数据库中的值填充数据表
您的语句应该
Your datatable with not going to fill wirh INSERT statement.
You need to pass select command to fill your datatable with values from database
You statement should
Select Column1,Column2.... from TableName
我希望你能清楚。
I hope it is clear to you.
这篇关于从数据库填充数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!