我如何从文本框中向数据库中插入值 [英] how i insert a value from textbox to database

查看:295
本文介绍了我如何从文本框中向数据库中插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我摆脱这种逻辑错误

该代码无法显示输出中的数据

any one can help me out from this logic error

this code can''t show the data in the output

dbp = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Administrator\Documents\mydata.mdb;User Id=admin;Password=;"
        con.ConnectionString = dbp
        con.Open()


        sql = "select * from test";
        
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "mydata")
        MsgBox("Database is now Open")

        con.Close()
        MsgBox("Database is Now Closed")





[Edited]代码包装在"pre"标签中[/Edited]





code is wrapped in "pre" tags[/Edited]

推荐答案

我看不到该代码中的错误,但我可以看到一些不好的做法.

我是说.

如果您使用的是DataAdapter,则无需关闭连接,DataAdapter将对此完全负责.

调用da.Fill(ds,"mydata");
时数据将被填充 之后,数据库连接将关闭.并说数据库现在已打开",然后又关闭了已经关闭的连接.

为了安全地关闭连接,您应该使用.

I can''t see error in that code but I can see some bad practice.

I mean.

If you''re using DataAdapter then no need to close connection, DataAdapter will be fully responsible for that.

Data Will be filled when you are calling da.Fill(ds,"mydata");
After that Database connection will be closed. and you stated that "Database is now open" and Further you closed again connection which is already closed.

For safe side to close connection you should use.

if (con.State == ConnectionState.Open)
 con.close();


好吧,以上代码只是从数据库中获取数据并将其填充到数据集中.没有其他的.您放置一个消息框以显示一些消息.没有编写任何代码来显示获取的数据.

正确地说,这是理解上的逻辑错误.如果需要查看获取的数据,然后将其作为示例,请在页面上放置一个网格,然后将获取的数据集绑定到该网格.检索到的数据将在此网格中可见.
Well, the above code just brings data from database and fills it in a dataset. Nothing else. You put a messagebox to display some message. There is no code written to display the data fetched.

As correctly said, it''s a logical error in understanding. If you need to see the data fetched then for sample, place a grid on the page and then bind the dataset fetched to this grid. Data retrieved would be visible in this grid.


这篇关于我如何从文本框中向数据库中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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