你如何通过ASP.net的网站中插入新的条目到Access数据库表? [英] How do you insert new entries into an Access db table through an ASP.net website?

查看:90
本文介绍了你如何通过ASP.net的网站中插入新的条目到Access数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要插入新记录到Access数据库。

我使用Visual Studio 2008和首先创建一个asp.net网站。我可以使用数据视图或GridView控件连接到接入DATABSE的信息,并可以查询特定条目(即建议号-brings了链接到该建议的所有细节)。

那么我可以编辑建议的细节,这将更新Access数据库。

我需要做的是有一个简单的输入新的细节,为新客户的形式。
IE浏览器。输入姓名[ __]输入ADRESS [__ ]。然后对这个更新数据库。通过使用GridView控件或数据视图我可以查看存在表中的所有字段和编辑它们。有没有一种方法,我可以得到一个空白的GridView /数据视图模板(包括表中的所有字段),并填写出,然后更新数据库?
谢谢

I need to insert new records into an Access database.
I'm using Visual Studio 2008 and firstly create a asp.net website. I can connect to the information in Access databse using dataview or gridview and can query a particular entry (ie. Proposal No. -brings up all details linking to that proposal).
I can then edit the details of that proposal and this would update the Access Db.
What I need to do is to have a form that simply enters new details for a new customer. ie. Enter name [__] Enter Adress[__]. Then for this to update the database. By using the gridview or dataview I am able to view all fields that exist in the table and edit them. Is there a way that I can get a blank gridview/dataview template (which includes all the fields in the table) and fill it out to then update the database? Thanks

推荐答案

允许最终用户一个新的记录添加到数据库的常用方法是通过presenting用户提供一组控件填写并/或如一些标记文本框,列表框,复选框,等的选择控制。

The usual way of allowing the end user to add a new record to a database is through presenting the user with a set of controls to fill in and/or select from i.e. a number of labelled textbox, listbox, checkbox, etc. controls.

如何将这些都摆出来是你的,你可以布置它们模仿你的GridView。我建议创建一个用户控件,然后使用在你的页面。

How these are laid out is up to you, and you could lay them out to mimic your gridview. I would recommend creating a user control and then using that in your page.

编辑:

要回答你如何领域作为一个记录添加到数据库评论,你会做类似如下(这是在C#)

To answer your comment about how to add the fields as a record to the database, you would do something like the following (this is in C#)

首先,设置添加一个连接字符串connectionStrings节在你的web.config。它总体上是好的做法,在这里设置起来,如果你将要使用在整个应用程序相同的数据源,因为它可以节省你不必编写连接字符串每次出去

Firstly, set up add a connection string to the connectionstrings section in your web.config. It is generally good practice to set them up here if you're going to be using the same data source throughout your application, as it saves you from having to write the connection string out each time

<connectionStrings> 
<add name="myConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db1.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>

然后在code-落后于你的页面

Then in the code-behind for your page

    protected void AddButton_Click(object sender, EventArgs e)
    {
        AddRecordToDatabase(txtCustomerName.Text, txtPlaceOfBirth.Text); 

        /*
          Where txtCustomerName and txtPlaceOfBirth are the IDs 
          of your Name and Place Of Birth textboxes, respectively.
          You may want to perform some validation on the textboxes before
          performing the insert!
        */
    }

    private void AddRecordToDatabase(string customerName, string placeOfBirth)
    {
        OleDbConnection conn;
        OleDbCommand cmd;

        using (conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
        {
            using (cmd = conn.CreateCommand())
            {
                cmd.CommandText = "INSERT INTO Customers(Name, PlaceOfBirth) VALUES (@name, @place_of_birth)";
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@name", customerName);
                cmd.Parameters.AddWithValue("@place_of_birth", placeOfBirth);

                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }

using语句将部署一旦与完成的对象(它们基本上是尝试,最后块)。

The using statements will dispose of the objects once finished with (they are essentially Try-Finally blocks).

根据访问的版本,它可能会在插入后返回一个值来表示插入是否成功与否(我只熟悉访问97 - 2003年)。如果这是可能的,你可能需要使用的ExecuteScalar代替的ExecuteNonQuery获得的返回值,并相应地作出反应。

Depending on the version of Access, it may be possible to return a value after the insert to indicate whether the insert has succeeded or not (I'm only familiar with Access 97 - 2003). If this is possible, you may want to use ExecuteScalar instead of ExecuteNonQuery to obtain the return value and respond accordingly.

作为建议的一个非常普遍的一块,我不会推荐作为喷气机为ASP.NET后端数据库(Access使用的数据库引擎)网站 - 看看的的答案的原因,这个问题。 SQL Server 2005的前preSS可免费使用*,是网络发展的一个优秀的数据库选择。

As a very general piece of advice, I would not recommend Jet (the database engine that Access uses) as the backend database for an ASP.NET website- have a look at the answers to this question for reasons why. SQL Server 2005 Express is free to use* and is an excellent database choice for web development.

*有一定的局限性,检查微软网站了解详情。

*There are some limitations, check Microsoft Website for details.

这篇关于你如何通过ASP.net的网站中插入新的条目到Access数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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